2
2
"""All exceptions for the github3 library."""
3
3
4
4
5
- class GitHubError (Exception ):
6
- """The base exception class."""
5
+ class GitHubException (Exception ):
6
+ """The base exception class.
7
+
8
+ .. versionadded:: 1.0.0
9
+
10
+ Necessary to handle pre-response exceptions
11
+
12
+ """
13
+
14
+ pass
15
+
16
+
17
+ class GitHubError (GitHubException ):
18
+ """The base exception class for all response-related exceptions.
19
+
20
+ .. versionchanged:: 1.0.0
21
+
22
+ This now inherits from :class:`~github3.exceptions.GitHubException`
23
+
24
+ .. attribute:: response
25
+
26
+ The response object that triggered the exception
27
+
28
+ .. attribute:: code
29
+
30
+ The response's status code
31
+
32
+ .. attribute:: errors
33
+
34
+ The list of errors (if present) returned by GitHub's API
35
+ """
7
36
8
37
def __init__ (self , resp ):
38
+ """Initialize our exception class."""
9
39
super (GitHubError , self ).__init__ (resp )
10
40
#: Response code that triggered the error
11
41
self .response = resp
@@ -35,9 +65,18 @@ def message(self):
35
65
36
66
37
67
class IncompleteResponse (GitHubError ):
38
- """Exception for a response that doesn't have everything it should."""
68
+ """Exception for a response that doesn't have everything it should.
69
+
70
+ This has the same attributes as :class:`~github3.exceptions.GitHubError`
71
+ as well as
72
+
73
+ .. attribute:: exception
74
+
75
+ The original exception causing the IncompleteResponse exception
76
+ """
39
77
40
78
def __init__ (self , json , exception ):
79
+ """Initialize our IncompleteResponse."""
41
80
self .response = None
42
81
self .code = None
43
82
self .json = json
@@ -50,20 +89,40 @@ def __init__(self, json, exception):
50
89
) % (exception ,)
51
90
52
91
92
+ class NotRefreshable (GitHubException ):
93
+ """Exception to indicate that an object is not refreshable."""
94
+
95
+ message_format = ('"{}" is not refreshable because the GitHub API does '
96
+ 'not provide a URL to retrieve its contents from.' )
97
+
98
+ def __init__ (self , object_name ):
99
+ """Initialize our NotRefreshable exception."""
100
+ super (NotRefreshable , self ).__init__ (
101
+ self .message_format .format (object_name )
102
+ )
103
+
104
+
53
105
class ResponseError (GitHubError ):
54
106
"""The base exception for errors stemming from GitHub responses."""
107
+
55
108
pass
56
109
57
110
58
- class TransportError (GitHubError ):
59
- """Catch-all exception for errors coming from Requests."""
111
+ class TransportError (GitHubException ):
112
+ """Catch-all exception for errors coming from Requests.
113
+
114
+ .. versionchanged:: 1.0.0
115
+
116
+ Now inherits from :class:`~github3.exceptions.GitHubException`.
117
+ """
60
118
61
119
msg_format = 'An error occurred while making a request to GitHub: {0}'
62
120
63
121
def __init__ (self , exception ):
64
- Exception .__init__ (self , exception )
65
- self .exception = exception
122
+ """Initialize TransportError exception."""
66
123
self .msg = self .msg_format .format (str (exception ))
124
+ super (TransportError , self ).__init__ (self , self .msg , exception )
125
+ self .exception = exception
67
126
68
127
def __str__ (self ):
69
128
return '{0}: {1}' .format (type (self .exception ), self .msg )
@@ -77,7 +136,9 @@ class ConnectionError(TransportError):
77
136
78
137
class UnprocessableResponseBody (ResponseError ):
79
138
"""Exception class for response objects that cannot be handled."""
139
+
80
140
def __init__ (self , message , body ):
141
+ """Initialize UnprocessableResponseBody."""
81
142
Exception .__init__ (self , message )
82
143
self .body = body
83
144
self .msg = message
@@ -91,6 +152,7 @@ def __str__(self):
91
152
92
153
class BadRequest (ResponseError ):
93
154
"""Exception class for 400 responses."""
155
+
94
156
pass
95
157
96
158
@@ -102,6 +164,7 @@ class AuthenticationFailed(ResponseError):
102
164
- Need one time password (for two-factor authentication)
103
165
- You are not authorized to access the resource
104
166
"""
167
+
105
168
pass
106
169
107
170
@@ -113,21 +176,25 @@ class ForbiddenError(ResponseError):
113
176
- Too many requests (you've exceeded the ratelimit)
114
177
- Too many login failures
115
178
"""
179
+
116
180
pass
117
181
118
182
119
183
class NotFoundError (ResponseError ):
120
184
"""Exception class for 404 responses."""
185
+
121
186
pass
122
187
123
188
124
189
class MethodNotAllowed (ResponseError ):
125
190
"""Exception class for 405 responses."""
191
+
126
192
pass
127
193
128
194
129
195
class NotAcceptable (ResponseError ):
130
196
"""Exception class for 406 responses."""
197
+
131
198
pass
132
199
133
200
@@ -138,26 +205,31 @@ class Conflict(ResponseError):
138
205
139
206
- Head branch was modified (SHA sums do not match)
140
207
"""
208
+
141
209
pass
142
210
143
211
144
212
class UnprocessableEntity (ResponseError ):
145
213
"""Exception class for 422 responses."""
214
+
146
215
pass
147
216
148
217
149
218
class ClientError (ResponseError ):
150
219
"""Catch-all for 400 responses that aren't specific errors."""
220
+
151
221
pass
152
222
153
223
154
224
class ServerError (ResponseError ):
155
225
"""Exception class for 5xx responses."""
226
+
156
227
pass
157
228
158
229
159
230
class UnavailableForLegalReasons (ResponseError ):
160
231
"""Exception class for 451 responses."""
232
+
161
233
pass
162
234
163
235
0 commit comments