TwitterAPI 2.2 documentation

Error Handling

«  Authentication   ::   Contents   ::   Paging Results  »

Error Handling

In addition to returning tweet statuses, the iterators for REST API and Streaming API endpoints return error and other messages. It is up to the application to test what type of object has been returned.

REST API Messages

REST API endpoints can return many more types of messages than Streaming API endpoints. The complete list is here. Depending on the endpoint, you may want to handle a particular type of message, such as exceeding a rate limit or posting a duplicate tweet. Testing the error code returned with the message makes this easy. Here is a general pattern for simply printing out any message and error code:

r = api.request('search/tweets', {'q':'pizza'})
for item in r.get_iterator():
    if 'text' in item:
        print item['text']
    elif 'message' in item:
        print '%s (%d)' % (item['message'], item['code'])

Streaming API Messages

Streaming API endpoints return a variety of messages, none are really errors. For example, a ‘limit’ message contains the number of tweets missing from the stream. This happens when you filter for tweets containing a common word. Other useful messages are ‘disconnect’ and ‘delete’. All the message types are referenced here. The pattern is similar to the one preceding:

r = api.request('statuses/filter', {'track':'pizza'})
for item in r.get_iterator():
    if 'text' in item:
        print item['text']
    elif 'limit' in item:
        print '%d tweets missed' % item['limit'].get('track')
    elif 'disconnect' in item:
        print 'disconnecting because %s' % item['disconnect'].get('reason')
        break

Even if you are not interested in handling errors it is necessary to test that the object returned by an iterator is a valid tweet status before using the object.

«  Authentication   ::   Contents   ::   Paging Results  »