Facebook PHP SDK throws uncatchable “GraphMethodException” error-Collection of common programming errors
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…
Originally posted 2013-11-27 12:24:56.