-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
errors.py
140 lines (96 loc) · 3.21 KB
/
errors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Tweepy
# Copyright 2009-2022 Joshua Roesslein
# See LICENSE for details.
import requests
class TweepyException(Exception):
"""Base exception for Tweepy
.. versionadded:: 4.0
"""
pass
class HTTPException(TweepyException):
"""HTTPException()
Exception raised when an HTTP request fails
.. versionadded:: 4.0
Attributes
----------
response : requests.Response
Requests Response from the Twitter API
api_errors : list[dict[str, int | str]]
The errors the Twitter API responded with, if any
api_codes : list[int]
The error codes the Twitter API responded with, if any
api_messages : list[str]
The error messages the Twitter API responded with, if any
"""
def __init__(self, response):
self.response = response
self.api_errors = []
self.api_codes = []
self.api_messages = []
try:
response_json = response.json()
except requests.JSONDecodeError:
super().__init__(f"{response.status_code} {response.reason}")
else:
errors = response_json.get("errors", [])
# Use := when support for Python 3.7 is dropped
if "error" in response_json:
errors.append(response_json["error"])
error_text = ""
for error in errors:
self.api_errors.append(error)
if isinstance(error, str):
self.api_messages.append(error)
error_text += '\n' + error
continue
if "code" in error:
self.api_codes.append(error["code"])
if "message" in error:
self.api_messages.append(error["message"])
if "code" in error and "message" in error:
error_text += f"\n{error['code']} - {error['message']}"
elif "message" in error:
error_text += '\n' + error["message"]
# Use := when support for Python 3.7 is dropped
if not error_text and "detail" in response_json:
self.api_messages.append(response_json["detail"])
error_text = '\n' + response_json["detail"]
super().__init__(
f"{response.status_code} {response.reason}{error_text}"
)
class BadRequest(HTTPException):
"""BadRequest()
Exception raised for a 400 HTTP status code
.. versionadded:: 4.0
"""
pass
class Unauthorized(HTTPException):
"""Unauthorized()
Exception raised for a 401 HTTP status code
.. versionadded:: 4.0
"""
pass
class Forbidden(HTTPException):
"""Forbidden()
Exception raised for a 403 HTTP status code
.. versionadded:: 4.0
"""
pass
class NotFound(HTTPException):
"""NotFound()
Exception raised for a 404 HTTP status code
.. versionadded:: 4.0
"""
pass
class TooManyRequests(HTTPException):
"""TooManyRequests()
Exception raised for a 429 HTTP status code
.. versionadded:: 4.0
"""
pass
class TwitterServerError(HTTPException):
"""TwitterServerError()
Exception raised for a 5xx HTTP status code
.. versionadded:: 4.0
"""
pass