Facebook PHP SDK Throws Inexplicable OAuthException

I am trying to publish the action of the open graph of the Facebook API, but I get an OAuth exception (#3501) User is already associated to the <object> . That all is well and good, I expect Facebook to throw this exception. I get some other exceptions related to user authentication (possibly with old / obsolete sessions, regardless).

My question is: has anyone else experienced that this exception is not possible in php? In this particular example (for publishing actions in a graph), I completely wrap the api call in the try / catch statement; but I still get a fatal error.

 <?php try { //publishing to open graph $this->fb->api('/me/app:action', 'POST', array( 'object' => 'http://www.domain.com/path/to/graph/object', )); } catch (Exception $e) { /* We may get here if the user has already posted this action before... or if our session somehow went sour or bc facebook is down... or one of any other 1000 reasons the graph api is currently sucking... in any case it doesn't much matter, this is not a mission critical thing to worry about; if we don't post the graph action - we don't post the graph action..nbd. */ } 

The above code is a snippet that publishes the graph's action (generalized because its contents are not important for this example).

I understand that the Exception that the PHP PHP SDK throws is FacebookApiException , but this class extends Exception. I cannot understand for life why in the name of everything is logical, I cannot catch my exception like this.

Has anyone experienced this problem? Is this a bug in the FB PHP SDK? Did I miss something else here? Thanks for your help!

Also, for reference, the relevant parts of the FB PHP SDK are here:

FacebookAPIException Definition (base_facebook.php line 30)

Throwing OAuthException (base_facebook.php line 1105

Change 5/1/12

After several more studies, it turns out that this “Exception” is not really considered an exception at all. Typical exceptions print the stack trace back to the method call, which results in an exception. These "OAuthExceptions" do not. In addition, typical exceptions highlight their error line somewhat differently, for example:

 PHP Fatal error: Uncaught exception 'Exception' with message 'foo' /path/to/file.php:10 

or

 PHP Fatal error: Uncaught exception 'MyException' with message 'stupid php' /path/to/file:10 #0 /path/to/file.php(17): doTest() #1 {main} thrown in /path/to/file.php on line 10 

In this particular case, we are not getting any of this, and it looks more like a typical fatal error:

 PHP Fatal error: Uncaught OAuthException: (#3501) User is already associated \ to the <object> object on a unique action type <action>. Original Action ID: \ 123123123 thrown in /path/to/app/libs/fb/base_facebook.php on line 1107, \ referer: http://www.domain.com/path/to/page 

I can't understand why this excluded Exception is so weird / erratic.

Decision:

I understood the answer to my question; I added it below - this is a developer error, not an error. The answer is below.

Also, this may very well be part of the error if you want to say that you can refer to the class definition as a hint type to the catch definition that was not (or was not available) in the current namespace) is an error.

+6
source share
1 answer

So, something that is not described above is that I am using PHP namespaces. This is a big getchta, as namespaces are relatively new to php, it very easily overlooks how I feel. Despite this, this is a pretty dumb miss / mistake.

If you are in a specific namespace (that is, not in the root namespace (\)), you do not have direct access to the Exception class. Instead of php throwing a warning that it does not know what the class is, it simply ignores the fact that it does not know what it is and does not raise an exception.

Solution 1:

import exception class:

 <?php use \Exception; // ...codes try { //...codes } catch (Exception $e) { //...codes } 

Solution 2:

provide the full path to the exception class:

 <?php try { //..... } catch (\Exception $e) { // voila. } 
+7
source

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


All Articles