Take the 2-minute tour ×

Using https://github.com/abraham/twitteroauth:

function getTwitterFeed($token_array){
    $oauth_token = $token_array['access_token'];
    $oauth_token_secret = $token_array['access_token_secret'];
    $response = $connection->get("statuses/user_timeline");

    //...do stuff with the response

I want to catch errors or exceptions for authentication problems (invalid token or token secret) and/or "rate limit exceeded."

I can't find anything on error handling for this library anywhere. How can I accomplish this?

share|improve this question

1 Answer 1

Look into the Exceptions part of the PHP manual, the library uses them extensively.

Basically they will look like this:

try {
    // your code here
} catch (OAuthException $e) {
    // your error handling here

the OauthException class is the what the library uses for every throw.


Unfortunately the errors returned from the actual twitter API not converted into exceptions by the library so you will have to check the return values from get() and other calls, and look for the "error" key, errors will look something like this:

   public 'error' => string 'Could not authenticate you.' (length=27)
   public 'request' => string '/1/account/verify_credentials.json?aauth_consumer_key=CONSUMER_KEY_HERE&oauth_nonce=cfbf6a55b26683750a166f14aeb5ed84&oauth_signature=c96MciQcODQD5jUAkyrAmSxXa0g%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1342379970&oauth_token=alma&oauth_version=1.0' (length=258)

also it will set the API instance's http_code code property to the response's http status, if that's not 200 it will indicate error.


I've created a fork of the library that will generate exceptions for every request that returns non 200 HTTP status, the exception's code will be the http status that twitter returns and message is the message (if exists), twitter's http error code listing will help decode the errors.

Also introduced a new Exception subclass for convenience named TwitterOauthException, every exception that thrown by the library subclasses this one.

share|improve this answer
Do I need to declare OauthException somewhere? I've intentionally passed bad tokens as a test and not a single error gets thrown. –  Inator Jul 15 '12 at 18:29
Nope, that class is declared in the twitter lib's OAuth.php file. You can try to catch every exception with the class Exception since every throwable exception must extend that class, it will catch everything that gets thrown. –  complex857 Jul 15 '12 at 18:45
I apologize, looks like I've spoken too soon. The errors returned from the actual twitter API returned as stdObjects and not converted to exceptions by the lib, edited the answer. –  complex857 Jul 15 '12 at 19:22
So do I look for $response['error'] then parse it based on a string compare? Will I get a response regardless of failed credentials? –  Inator Jul 15 '12 at 19:51
In my understanding yes, but i would probably wrap the client lib and extend its http method to throw exceptions when response's http status is not 200 OK. I've created a fork that does just that. –  complex857 Jul 16 '12 at 6:50

Your Answer


