简体   繁体   中英

Facebook PHP SDK throws uncatchable “GraphMethodException” error

I'm experiencing something eerily similar to this question about an uncatchable PHP error thrown by the Facebook PHP SDK except for the fact that I'm not using PHP namespaces at all. This other question is also close, but doesn't explain why the error is uncatchable. Further, in my case, I have a Facebook app that issues a Facebook Graph API call against an object that the current user has blocked. This is certainly awkward, but legal for the purposes of this particular app. That means I need to catch the error, not prevent the user from making the search in the first place.

The fatal error's output in my development environment looks like this:

Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /path/to/apps/lib/facebook/src/base_facebook.php on line 1271

So, Facebook's Graph API correctly returns an error as a result of the API call, citing "Unsupported get request." However, the Facebook PHP SDK seems to throw this as an uncatchable error, and I don't know why.

I've tried code like the following catch blocks with no success:

try {
    $response = $facebook->api("/$some_id_of_object_current_user_has_blocked");
} catch (FacebookApiException $e) {
    // Why does this never get caught?
} catch (Exception $e) {
    // Similarly, this also never gets caught!
} catch (GraphMethodException $e) {
    // Still can't catch this exception, and I don't grok why. :(
}

For the sake of ridiculous completeness, I've also tried namespaces including things like this:

try {
    $response = $facebook->api("/$some_id_of_object_current_user_has_blocked");
} catch (\FacebookApiException $e) {
} catch (\Exception $e) {
} catch (\FacebookApiException\GraphMethodException $e) {
} catch (\GraphMethodException $e) {
} catch (... $e) {
}

Further investigation lead me to try catching this in the base_facebook.php file itself, where it seems to get thrown, in the protected Facebook::_graph method . And sure enough, it is catchable there. The original code at about line 879 of base_facebook.php is:

if (is_array($result) && isset($result['error'])) {
  $this->throwAPIException($result);
  // @codeCoverageIgnoreStart
}

Wrapping this call to throwAPIException() with a try...catch block works:

if (is_array($result) && isset($result['error'])) {
  try {
    $this->throwAPIException($result);
    // @codeCoverageIgnoreStart
  } catch (Exception $e) {
    // WORKS!
  }
}

So if it works there, why can't I catch this exception from my own scripts? Am I missing something fundamental about the way PHP error handling works?

Alternatively, is there a way for a Facebook app to get a list of all the objects a Facebook user has blocked, such as other Facebook users a user has blocked? I'm familiar with Graph API enough to know that there's a way for an app to access a list of all users a page has blocked , but that's specifically not what I'm looking for.

Thanks for your time.

It's apparently uncatchable because it relates to the permissions your app uses.

In your case, it looks like you were trying to GET the same thing as me, which requires the permission: read_stream

It makes sense that they would make this sort of thing uncatchable - but you'd think the facebook devs could do something a little more friendly...

Adding the try/catch around $this->throwAPIException($result); works in suppressing the error message but I would recommend checking the inputs to your functions to ensure they exist and are valid.

For example, check to see if $ SESSION['fb <your_app_id>_access_token'] exists and is not null before passing this to any functions. If this is not set or is null, none of the functions that rely on it will work and you can catch the potential issue before communicating with Facebook's servers, which speeds up your application by skipping a function you can determine ahead of time will fail, and resolve issues quicker by asking for corrections proactively instead of re-actively.

I also was seeing this error which apparently was caused by my app not having the appropriate permissions, as @rm-vanda stated. Because the app id does not have the permissions, a token is not returned which results in the error you are seeing.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM