如何捕捉和更改从facebooksdk抛出的异常消息


How can I catch and change the thrown exception message from facebook sdk?

我从facebookSDK得到这个异常:

Fatal error: Uncaught OAuthException: (#506) Duplicate status message thrown in [...] src/base_facebook.php on line 1033

如何在异常打印此消息之前捕获它?我想把它改成这样:

应用程序的响应仍然相同,因此我们无法再次将其发布到您的墙上:)

顺便说一句,有一个方法(下面)可以处理所有的异常,我只希望这个新消息适用于特定的情况。

protected function throwAPIException($result) {
    $e = new FacebookApiException($result);
    switch ($e->getType()) {
      // OAuth 2.0 Draft 00 style
      case 'OAuthException':
        // OAuth 2.0 Draft 10 style
      case 'invalid_token':
        $message = $e->getMessage();
      if ((strpos($message, 'Error validating access token') !== false) ||
          (strpos($message, 'Invalid OAuth access token') !== false)) {
        $this->setAccessToken(null);
        $this->user = 0;
        $this->clearAllPersistentData();
      }
    }
    throw $e;
  }

谢谢。

您将抛出Exception的代码放入try块中,然后用catch块捕获它:

try
{
  // Code goes here
}
catch (OAuthException $e)
{
  // Stuff to do with exception
}