From: Harmon Date: Sun, 4 Apr 2021 13:57:11 +0000 (-0500) Subject: Add Unauthorized X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=3ffec76039a9552b1b90a11d3774ccc90793c33d;p=tweepy.git Add Unauthorized --- diff --git a/tweepy/__init__.py b/tweepy/__init__.py index 0abf182..64c0dcf 100644 --- a/tweepy/__init__.py +++ b/tweepy/__init__.py @@ -13,8 +13,13 @@ from tweepy.api import API from tweepy.auth import AppAuthHandler, OAuthHandler from tweepy.cache import Cache, FileCache, MemoryCache from tweepy.cursor import Cursor -from tweepy.errors import HTTPException, NotFound, TooManyRequests, TweepyException -from tweepy.models import DirectMessage, Friendship, ModelFactory, SavedSearch, SearchResults, Status, User +from tweepy.errors import ( + HTTPException, NotFound, TooManyRequests, TweepyException, Unauthorized +) +from tweepy.models import ( + DirectMessage, Friendship, ModelFactory, SavedSearch, SearchResults, + Status, User +) from tweepy.streaming import Stream # Global, unauthenticated instance of API diff --git a/tweepy/api.py b/tweepy/api.py index 19715fb..acaef20 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -12,7 +12,9 @@ from urllib.parse import urlencode import requests -from tweepy.errors import HTTPException, NotFound, TooManyRequests, TweepyException +from tweepy.errors import ( + HTTPException, NotFound, TooManyRequests, TweepyException, Unauthorized +) from tweepy.models import Model from tweepy.parsers import ModelParser, Parser from tweepy.utils import list_to_csv @@ -208,6 +210,8 @@ class API: # If an error was returned, throw an exception self.last_response = resp + if resp.status_code == 401: + raise Unauthorized(resp) if resp.status_code == 404: raise NotFound(resp) if resp.status_code == 429: diff --git a/tweepy/errors.py b/tweepy/errors.py index 93c4777..f27a637 100644 --- a/tweepy/errors.py +++ b/tweepy/errors.py @@ -43,6 +43,11 @@ class HTTPException(TweepyException): ) +class Unauthorized(HTTPException): + """Exception raised for a 401 HTTP status code""" + pass + + class NotFound(HTTPException): """Exception raised for a 404 HTTP status code""" pass