Get the error code from tweepy exception instance

José D. picture José D. · Jun 18, 2013 · Viewed 17.9k times · Source

I'm new to python and I'm trying to use a library. It raises an exception, and I am trying to identify which one. This is what I am trying:

except tweepy.TweepError as e:
    print e
    print type(e)
    print e.__dict__
    print e.reason
    print type(e.reason)

This is what I am getting:

[{u'message': u'Sorry, that page does not exist', u'code': 34}]
<class 'tweepy.error.TweepError'>
{'reason': u"[{u'message': u'Sorry, that page does not exist', u'code': 34}]", 'response': <httplib.HTTPResponse instance at 0x00000000029CEAC8>}
[{u'message': u'Sorry, that page does not exist', u'code': 34}]
<type 'unicode'>

Im trying to get to that code. I have tried e.reason.code with no success and I have no idea what to try.

Answer

alecxe picture alecxe · Jun 18, 2013

How about this?

except tweepy.TweepError as e:
    print e.message[0]['code']  # prints 34
    print e.args[0][0]['code']  # prints 34