doulongsi1831 2013-09-01 03:26
浏览 89

Facebook PHP SDK引发无法捕获的“GraphMethodException”错误

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.

  • 写回答

2条回答 默认 最新

  • douerlin4366 2013-10-09 16:48
    关注

    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...

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行