Get the error code from tweepy exception instance

前端 未结 5 637
猫巷女王i
猫巷女王i 2020-12-14 09:50

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.T         


        
5条回答
  •  再見小時候
    2020-12-14 10:21

    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'.

提交回复
热议问题