Skip to content

Commit 9722e2c

Browse files
committed
Fix-up docstrings and add GitHubException
1 parent ea9c834 commit 9722e2c

File tree

1 file changed

+79
-7
lines changed

1 file changed

+79
-7
lines changed

github3/exceptions.py

+79-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,40 @@
22
"""All exceptions for the github3 library."""
33

44

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+
"""
736

837
def __init__(self, resp):
38+
"""Initialize our exception class."""
939
super(GitHubError, self).__init__(resp)
1040
#: Response code that triggered the error
1141
self.response = resp
@@ -35,9 +65,18 @@ def message(self):
3565

3666

3767
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+
"""
3977

4078
def __init__(self, json, exception):
79+
"""Initialize our IncompleteResponse."""
4180
self.response = None
4281
self.code = None
4382
self.json = json
@@ -50,20 +89,40 @@ def __init__(self, json, exception):
5089
) % (exception,)
5190

5291

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+
53105
class ResponseError(GitHubError):
54106
"""The base exception for errors stemming from GitHub responses."""
107+
55108
pass
56109

57110

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+
"""
60118

61119
msg_format = 'An error occurred while making a request to GitHub: {0}'
62120

63121
def __init__(self, exception):
64-
Exception.__init__(self, exception)
65-
self.exception = exception
122+
"""Initialize TransportError exception."""
66123
self.msg = self.msg_format.format(str(exception))
124+
super(TransportError, self).__init__(self, self.msg, exception)
125+
self.exception = exception
67126

68127
def __str__(self):
69128
return '{0}: {1}'.format(type(self.exception), self.msg)
@@ -77,7 +136,9 @@ class ConnectionError(TransportError):
77136

78137
class UnprocessableResponseBody(ResponseError):
79138
"""Exception class for response objects that cannot be handled."""
139+
80140
def __init__(self, message, body):
141+
"""Initialize UnprocessableResponseBody."""
81142
Exception.__init__(self, message)
82143
self.body = body
83144
self.msg = message
@@ -91,6 +152,7 @@ def __str__(self):
91152

92153
class BadRequest(ResponseError):
93154
"""Exception class for 400 responses."""
155+
94156
pass
95157

96158

@@ -102,6 +164,7 @@ class AuthenticationFailed(ResponseError):
102164
- Need one time password (for two-factor authentication)
103165
- You are not authorized to access the resource
104166
"""
167+
105168
pass
106169

107170

@@ -113,21 +176,25 @@ class ForbiddenError(ResponseError):
113176
- Too many requests (you've exceeded the ratelimit)
114177
- Too many login failures
115178
"""
179+
116180
pass
117181

118182

119183
class NotFoundError(ResponseError):
120184
"""Exception class for 404 responses."""
185+
121186
pass
122187

123188

124189
class MethodNotAllowed(ResponseError):
125190
"""Exception class for 405 responses."""
191+
126192
pass
127193

128194

129195
class NotAcceptable(ResponseError):
130196
"""Exception class for 406 responses."""
197+
131198
pass
132199

133200

@@ -138,26 +205,31 @@ class Conflict(ResponseError):
138205
139206
- Head branch was modified (SHA sums do not match)
140207
"""
208+
141209
pass
142210

143211

144212
class UnprocessableEntity(ResponseError):
145213
"""Exception class for 422 responses."""
214+
146215
pass
147216

148217

149218
class ClientError(ResponseError):
150219
"""Catch-all for 400 responses that aren't specific errors."""
220+
151221
pass
152222

153223

154224
class ServerError(ResponseError):
155225
"""Exception class for 5xx responses."""
226+
156227
pass
157228

158229

159230
class UnavailableForLegalReasons(ResponseError):
160231
"""Exception class for 451 responses."""
232+
161233
pass
162234

163235

0 commit comments

Comments
 (0)