Get the error code from tweepy exception instance

帅比萌擦擦* 提交于 2019-11-27 15:59:55

问题


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.


回答1:


How about this?

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



回答2:


Every well-behaved exception derived from the base Exception class has an args attribute (of type tuple) that contains arguments passed to that exception. Most of the time only one argument is passed to an exception and can be accessed using args[0].

The argument Tweepy passes to its exceptions has a structure of type List[dict]. You can get the error code (type int) and the error message (type str) from the argument using this code:

e.args[0][0]['code']
e.args[0][0]['message']

The TweepError exception class also provides several additional helpful attributes api_code, reason and response. They are not documented for some reason even though they are a part of public API.

So you can get the error code (type int) also using this code:

e.api_code


History:

The error code used to be accessed using e.message[0]['code'] which no longer works. The message attribute has been deprecated in Python 2.6 and removed in Python 3.0. Currently you get an error 'TweepError' object has no attribute 'message'.




回答3:


Things have changed quite a bit since 2013. The correct answer as of now is to use e.api_code.




回答4:


To get just the error code use the method monq posted. The following example illustrates how to get both the error code and the message. I had to extract the message from the e.reason string, if anyone has a better method to retrieve just the message, please share.

Note: This code should work for any error code/reason with the following format.

[{'code': 50, 'message': 'User not found.'}]

def getExceptionMessage(msg):
    words = msg.split(' ')

    errorMsg = ""
    for index, word in enumerate(words):
        if index not in [0,1,2]:
            errorMsg = errorMsg + ' ' + word
    errorMsg = errorMsg.rstrip("\'}]")
    errorMsg = errorMsg.lstrip(" \'")

    return errorMsg

And you can call it like so:

try:
    # Some tweepy api call, ex) api.get_user(screen_name = usrScreenName)
except tweepy.TweepError as e:
    print (e.api_code)
    print (getExceptionMessage(e.reason))



回答5:


Here is how I do it:

except tweepy.TweepError as e:
    print e.response.status


来源:https://stackoverflow.com/questions/17157753/get-the-error-code-from-tweepy-exception-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!