{"id":1652,"date":"2022-08-30T15:18:19","date_gmt":"2022-08-30T15:18:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/facebook-php-sdk-throws-uncatchable-graphmethodexception-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:19","modified_gmt":"2022-08-30T15:18:19","slug":"facebook-php-sdk-throws-uncatchable-graphmethodexception-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/facebook-php-sdk-throws-uncatchable-graphmethodexception-error-collection-of-common-programming-errors\/","title":{"rendered":"Facebook PHP SDK throws uncatchable &ldquo;GraphMethodException&rdquo; error-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;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&#8217;m not using PHP namespaces at all. This other question is also close, but doesn&#8217;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 <em>legal for the purposes of this particular app.<\/em> That means I need to catch the error, not prevent the user from making the search in the first place.<\/p>\n<p>The fatal error&#8217;s output in my development environment looks like this:<\/p>\n<pre>Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in \/path\/to\/apps\/lib\/facebook\/src\/base_facebook.php on line 1271<\/pre>\n<p>So, Facebook&#8217;s Graph API correctly returns an error as a result of the API call, citing &#8220;Unsupported get request.&#8221; However, the Facebook PHP SDK seems to throw this <em>as an uncatchable error,<\/em> and I don&#8217;t know why.<\/p>\n<p>I&#8217;ve tried code like the following <code>catch<\/code> blocks with no success:<\/p>\n<pre><code>try {\n    $response = $facebook-&gt;api(\"\/$some_id_of_object_current_user_has_blocked\");\n} catch (FacebookApiException $e) {\n    \/\/ Why does this never get caught?\n} catch (Exception $e) {\n    \/\/ Similarly, this also never gets caught!\n} catch (GraphMethodException $e) {\n    \/\/ Still can't catch this exception, and I don't grok why. :(\n}\n<\/code><\/pre>\n<p>For the sake of ridiculous completeness, I&#8217;ve also tried namespaces including things like this:<\/p>\n<pre><code>try {\n    $response = $facebook-&gt;api(\"\/$some_id_of_object_current_user_has_blocked\");\n} catch (\\FacebookApiException $e) {\n} catch (\\Exception $e) {\n} catch (\\FacebookApiException\\GraphMethodException $e) {\n} catch (\\GraphMethodException $e) {\n} catch (... $e) {\n}\n<\/code><\/pre>\n<p>Further investigation lead me to try catching this in the <code>base_facebook.php<\/code> file itself, where it seems to get thrown, in the protected <code>Facebook::_graph<\/code> method. And sure enough, it <em>is<\/em> catchable there. The original code at about line 879 of <code>base_facebook.php<\/code> is:<\/p>\n<pre><code>if (is_array($result) &amp;&amp; isset($result['error'])) {\n  $this-&gt;throwAPIException($result);\n  \/\/ @codeCoverageIgnoreStart\n}\n<\/code><\/pre>\n<p>Wrapping this call to <code>throwAPIException()<\/code> with a <code>try...catch<\/code> block works:<\/p>\n<pre><code>if (is_array($result) &amp;&amp; isset($result['error'])) {\n  try {\n    $this-&gt;throwAPIException($result);\n    \/\/ @codeCoverageIgnoreStart\n  } catch (Exception $e) {\n    \/\/ WORKS!\n  }\n}\n<\/code><\/pre>\n<p>So if it works there, why can&#8217;t I catch this exception from my own scripts? Am I missing something fundamental about the way PHP error handling works?<\/p>\n<p>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&#8217;m familiar with Graph API enough to know that there&#8217;s a way for an app to access a list of all users a <em>page<\/em> has blocked, but that&#8217;s specifically not what I&#8217;m looking for.<\/p>\n<p>Thanks for your time.<\/p>\n<ol>\n<li>\n<p>It&#8217;s apparently uncatchable because it relates to the <em>permissions<\/em> your app uses.<\/p>\n<p>In your case, it looks like you were trying to <code>GET<\/code> the same thing as me, which requires the permission: <code>read_stream<\/code><\/p>\n<p>It makes sense that they would make this sort of thing uncatchable &#8211; but you&#8217;d think the facebook devs could do something a little more friendly&#8230;<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:24:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;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&#8217;m not using PHP namespaces at all. This other question is also close, but doesn&#8217;t explain why the error is uncatchable. Further, in my case, I have a Facebook app that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1652","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1652"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1652\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}