3

This error is happening when using the Facebook PHP SDK but I actually think it's a general error.

When I run this code, everything works fine and the exception is caught:

$facebook = new Facebook('appId'=>APP_ID,'secret'=>APP_SECRET);
try {
    $user_profile = $facebook->api('/me','GET');
    echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
    $login_url = $facebook->getLoginUrl(); 
    echo 'Please <a href="' . $login_url . '">login.</a>';
}

But when I run this code:

$facebook = new Facebook_Extended('appId'=>APP_ID,'secret'=>APP_SECRET);
$facebook->api_extended();

With the extended class's method looking like this:

// Overrides parent::api()
public function api() {
    try {
        $user_profile = parent::api('/me','GET');
        echo "Name: " . $user_profile['name'];
    } catch(FacebookApiException $e) {
        $login_url = $this->getLoginUrl(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
    } 
}

I get an "Uncaught OAuthException" error.

Any ideas why the exception is not able to be caught in the child class?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
derekbeau
  • 91
  • 7

3 Answers3

6

I didn't realize this was an issue, but this class is in a namespace, so I needed to add a "\" to my Exception:

catch (\FacebookApiException $e)

http://onehackoranother.com/logfile/2009/01/php-5-3-exception-gotcha

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
derekbeau
  • 91
  • 7
  • I was getting the same problem when could not catch exception in a _child_ class that was in the namespace but it was strongly cougt by parent class.. added leading slash and it helped, thanks for tip! – Hellbyte Feb 03 '17 at 01:12
0

The api method is not static, so shouldn't you be using it like this:

$user_profile = $this->api('/me','GET');

If not, you can always add another catch block for a generic exception so you can see what's going wrong.

public function api_extended() {
    try {
        $user_profile = parent::api('/me','GET');
        echo "Name: " . $user_profile['name'];
    } catch(FacebookApiException $e) {
        $login_url = $this->getLoginUrl(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
    } catch(Exception $e) {
        var_dump($e);
    } 
}
shanethehat
  • 15,460
  • 11
  • 57
  • 87
  • Oops, my function is actually overriding "api" (edited original post to reflect), but I did try renaming it to "api_extended" and then calling the other api method with "$this->api" -- still the same error. And I also tried the generic exception idea. Still the same error. – derekbeau Jan 13 '12 at 09:41
0

The stack trace would be more helpful. for the time being try the following

public function api_extended() {
try {
    $user_profile = parent::api('/me','GET');
    echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
    $login_url = $this->getLoginUrl(); 
    echo 'Please <a href="' . $login_url . '">login.</a>';
} catch (Exception $err){
    print_r($err->getTrace());
}

}

vaibhav
  • 550
  • 5
  • 20