How to catch FacebookApiExceptions when using FacebookApp.ApiAsync in WP7?

I am currently using Facebook's C # SDK v4.2.1, and I'm trying to post something to the user's wall. It worked fine until it received a FacebookOAuthException (OAuthException) Error validating access token. , and I cannot catch this exception, and this will cause my application to crash.

I use this call FacebookApp.ApiAsync("/me/feed", ...) . Since this is happening async, I'm not sure where I have to put my try-catch block in order to catch this error, but without success

This is what I use:

  private void shareFBButton_Click(object sender, System.Windows.RoutedEventArgs e) { // ... code for preparing strings to post ... try { // setup FacebookApp and params ... app.ApiAsync("/me/feed", args, HttpMethod.Post, (o) => { if (o.Error != null) { Debug.WriteLine("ERROR sharing on Facebook: " + o.Error.Message); } else { Debug.WriteLine("FB post success!"); } }, null); } catch (Exception ex) { Debug.WriteLine("ERROR sharing on Facebook: " + ex.Message); } } 

So can someone tell me where should I put my try-catch block so that I can catch an OAuthException ?

EDIT:

After further investigation, FacebookOAuthExcpetion is thrown out of the Facebook SDK CK after the SDK catches WebException and FacebookApiException. For more information, see "Pavel Surmenok." This is exactly what is happening.

Right now, the only solution to catching FacebookApiException (the base class of all SDK exceptions for Facebook) is the trick in the App.UnhandledException method. Check the type of e.ExceptionObject and if it is set to FacebookApiException, e.Handled to true and the application will no longer exit.

+4
source share
4 answers

I reproduced this problem. As I can see, the exception is thrown in the FacebookApp.ResponseCallback method. It contains a try block with two catch sections (one for FacebookApiException and one for WebException). At the end of each catch section, the exception is recovered and never handled (which causes your application to crash). So, the debugger tells you about this (re) exception. Later, in the finally section, they create FacebookAsyncResult with a reference to this exception in the Error property. I think your solution (to eliminate this exception in App.UnhandledException) is the most suitable. By the way, I wonder why the SDK developers decided to abandon the exceptions in FacebookApp.ResponseCallback.

+2
source

I found a solution for my problem. Perhaps I should rephrase my question.

"How to catch the exception that occurred in the background thread?"

This is exactly what happens in my original question. The exception is throwing inside the C # Facebook SDK on the background thread because Api calls are made asynchronously.

Most of you may already know this, but I didn’t because I am new to WP7 development.

Decision:

In the App.UnhandledException event handler, just set the e.Handled flag to true. Then the application will not lose its temper.

  private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { // catch Facebook API exceptions // if handled is set to true, app won't exit if (e.ExceptionObject is FacebookApiException) { e.Handled = true; // notify user of error ... return; } if (System.Diagnostics.Debugger.IsAttached) { // An unhandled exception has occurred; break into the debugger System.Diagnostics.Debugger.Break(); } } 

Not sure if this is the right way to catch an API exception, but it works just fine.

+3
source

The debugger usually does a good job of indicating where the exception came from. In the debugger, you can view exception details and look at Nessted InnerExceptions to find the root cause.

However, if an exception is thrown from the appApiAsync application, then the catch handler that you already have will catch any exceptions. When looking at things in the SDK (I just looked briefly), there are certain circumstances in which exceptions are caught by the callback in the Error property, which you also check.

If you look at the SDK code, it would seem that the exception raised is actually a FacebookOAuthException; what's up If so, then it looks like this exception is never provided to the callback, but is always thrown.

If you can give more details on what the type of exception is and where it was thrown / caught, I could give a more useful answer.

0
source

Trying to catch an exception in App.UnhandledException does not work because it is in a different thread. But you can play with the "error reason" property from authResult before executing the request, and therefore you will avoid the exception.

 private void FacebookLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) { FacebookAuthenticationResult authResult; if (FacebookAuthenticationResult.TryParse(e.Uri, out authResult)) { if (authResult.ErrorReason == "user_denied") { // do something. } else { fbApp.Session = authResult.ToSession(); loginSucceeded(); } } 
0
source

Source: https://habr.com/ru/post/1336301/


All Articles