José D. José D. - 2 months ago 36
Python Question

Get the error code from tweepy exception instance

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

How about this?

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