The Wayback Machine - https://web.archive.org/web/20220509163959/https://github.com/tweepy/tweepy/issues/1254
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TweepError.api_code doesn't return the error code #1254

Closed
TomVolkmann opened this issue Aug 5, 2019 · 1 comment
Closed

TweepError.api_code doesn't return the error code #1254

TomVolkmann opened this issue Aug 5, 2019 · 1 comment
Labels
Invalid Question

Comments

@TomVolkmann
Copy link

@TomVolkmann TomVolkmann commented Aug 5, 2019

I want to get the error code via err.api_code, but it just returns "None".
But in err.args is the actual error code.

Some people are using it, like here

This is my code:

try:
  tweets = twitter_client.get_user_timeline_tweets(USER_TWEETS_TO_CAPTURE,user_id)
except TweepError as err:
  if err.api_code == 401:
     logger.error(f"you are not authorized to access this user {user_id}")
  else:
     print(err.api_code)
     print(err.args)
     logger.error(f"Exception occurred with {user_id} {err.api_code}")

Expected Behaviour
you are not authorized to access this user {user_id}

Actual Behaviour
None
('Twitter error response: status code = 401',)
Exception occurred with {user_id} None

I read here
That the 401 error is not supported yet, but an alternative would be to get the error code via:

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

But that throws me this error:

print(err.args[0][0]['code'])
TypeError: string indices must be integers

Am I missing something?

@Harmon758 Harmon758 added the Question label Aug 30, 2019
@Harmon758
Copy link

@Harmon758 Harmon758 commented Aug 30, 2019

TweepError.api_code corresponds to the Twitter API error code in the response. If there isn't one, then it will be None. This is different from the HTTP status code of the response.
See https://developer.twitter.com/en/docs/basics/response-codes.

BaseException.args simply returns a tuple of arguments given to the exception constructor. In the case of TweepError, only TweepError.reason is passed to that constructor:

tweepy/tweepy/error.py

Lines 8 to 15 in bc2deca

class TweepError(Exception):
"""Tweepy exception"""
def __init__(self, reason, response=None, api_code=None):
self.reason = six.text_type(reason)
self.response = response
self.api_code = api_code
super(TweepError, self).__init__(reason)

This should also be apparent where you printed e.args and it was a tuple containing a single string.
With e.args[0][0]['code'], you're accessing the first character of the string in the tuple, 'T', and attempting to index that character by 'code', i.e. 'T'['code']. This results in the TypeError, and as it says, string indices have to be integers. I'd advise against copying and pasting code from StackOverflow that you don't understand, especially from answers that are more than 6 years old.

To access the HTTP status code from the TweepError, you can simply use TweepError.response.status_code, as TweepError.response will be the requests.Response object corresponding with the response from Twitter's API if there was one.

@Harmon758 Harmon758 closed this Aug 30, 2019
@Harmon758 Harmon758 added the Invalid label Aug 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid Question
Projects
None yet
Development

No branches or pull requests

2 participants