Ajouve
Ajouve

Reputation: 10089

throw APIException Facebook api php

I'd like to get my api exception but I don't understand how to do that. This is my code

public function facebook($link){

    if(!$link || !trim($link) != ""){
        return false;
    }

    $config = array(
            'appId'=>$this->keys['facebook']['id'],
            'secret'=>$this->keys['facebook']['secret'],
            'fileUpload'=>false
    );

    $facebook = new Facebook($config);


    $start = strrpos($link, '/', -1);
    $end = strripos($link, '?', -1);
    $end = ($end)?$end:strlen($link);

    $pageId = ($end == strlen($link))?substr($link, $start + 1):substr($link, $start + 1, $end - strlen($link));

    try {
        $pagefeed = $facebook->api("/" . $pageId . "/feed");
    }
    catch (FacebookApiException $e){
        return false;
    }

    //set datetime
    foreach ($pagefeed['data'] as $key => $post){
        $pagefeed['data'][$key]['datetime'] = new \DateTime($post['created_time']);
    }

    return $pagefeed;
}

So I'd like to return false in case of exception.

I got for exemple:

BaseFacebook ->throwAPIException (array('error' => array('message' => '(#803) Some of the aliases you requested do not exist: lkdsgfkqdjgflkdshbf', 'type' => 'OAuthException', 'code' => '803'))) 

Thanks for your help

Upvotes: 0

Views: 488

Answers (1)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76405

Since you've commented that you're using symfony, and you fixed the type-hint using catch(\Exception $e) you might want to consider adding this to the top of your file:

use \APIException;

To set APIException as an alias to \APIException. Also check this link. Not having used the FB API, I don't know if it's still relevant, but assuming the facebook api is stored in your vendor dir, you'll have to specify the correct namespaces when using the Facebook api.
The reason why \Exception worked is simply because, as the linked page shows, the APIException class extends from the \Exception base-class, so the type-hint works. It's not crucial, but it's generally better to catch the right exception in the right place.

The Exception is thrown, and you catch it using the catch block. So far so good, it is, though, caught in the method's scope, which is GC'ed when that method returns (Garbage Collected). The Exception isntance doesn't exits anymore.
Generally, if you want to access the exception outside of the method (most likely in the code that invoked the method), you just don't catch the exception. Remove the try-catch from the facebook method and do this:

//call method:
try
{
    $return = $instance->facebook($someLink);
}
catch (APIException $e)
{
    $return = false;//exception was thrown, so the return value should be false
    var_dump($e);//you have access to the exception here, too
}

Catching an exception and not doing anything with it (you're catching but returning false, no way of knowing why) is considered bad practice.
If you want to avoid wrapping all those calls to your facebook method in a try-catch, you could do something like this, too:

//in class containing facebook method:
private $lastException = null;
public function getLastException()
{
    return $this->lastException;
}

Now you can change your facebook method's catch block to:

catch(APIException $e)
{
    $this->lastException = $e;
    return false;
}

And do something like:

$return = $instance->facebook($link);
if ($return === false)
{
    var_dump($instance->getLastException());
    exit($instance->getLastException()->getMessage());
}

Upvotes: 2

Related Questions