Skip to content

Commit 5eecc64

Browse files
committed
Move GitHubError to exceptions module
Create new exceptions for better control over error handling
1 parent df210b6 commit 5eecc64

2 files changed

Lines changed: 73 additions & 30 deletions

File tree

github3/exceptions.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
"""All exceptions for the github3 library."""
3+
4+
5+
class GitHubError(Exception):
6+
7+
"""The base exception class."""
8+
9+
def __init__(self, resp):
10+
super(GitHubError, self).__init__(resp)
11+
#: Response code that triggered the error
12+
self.response = resp
13+
self.code = resp.status_code
14+
self.errors = []
15+
try:
16+
error = resp.json()
17+
#: Message associated with the error
18+
self.msg = error.get('message')
19+
#: List of errors provided by GitHub
20+
if error.get('errors'):
21+
self.errors = error.get('errors')
22+
except: # Amazon S3 error
23+
self.msg = resp.content or '[No message]'
24+
25+
def __repr__(self):
26+
return '<GitHubError [{0}]>'.format(self.msg or self.code)
27+
28+
def __str__(self):
29+
return '{0} {1}'.format(self.code, self.msg)
30+
31+
@property
32+
def message(self):
33+
"""The actual message returned by the API."""
34+
return self.msg
35+
36+
37+
class AuthenticationFailed(GitHubError):
38+
39+
"""Exception class for 401 responses."""
40+
41+
pass
42+
43+
44+
class ForbiddenError(GitHubError):
45+
46+
"""Exception class for 403 responses."""
47+
48+
pass
49+
50+
51+
class NotFoundError(GitHubError):
52+
53+
"""Exception class for 404 responses."""
54+
55+
pass
56+
57+
58+
class InvalidRequestError(GitHubError):
59+
60+
"""Exception class for 422 responses."""
61+
62+
pass
63+
64+
65+
class ServerError(GitHubError):
66+
67+
"""Exception class for 5xx responses."""
68+
69+
pass

github3/models.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
from json import dumps, loads
1212
from requests.compat import urlparse, is_py2
13+
from datetime import datetime
14+
from logging import getLogger
15+
1316
from github3.decorators import requires_auth
17+
from github3.exceptions import GitHubError
1418
from github3.session import GitHubSession
1519
from github3.utils import UTC
16-
from datetime import datetime
17-
from logging import getLogger
1820

1921
__timeformat__ = '%Y-%m-%dT%H:%M:%SZ'
2022
__logs__ = getLogger(__package__)
@@ -414,31 +416,3 @@ def _repr(self):
414416

415417
def _update_(self, acct):
416418
self.__init__(acct, self.session)
417-
418-
419-
class GitHubError(Exception):
420-
def __init__(self, resp):
421-
super(GitHubError, self).__init__(resp)
422-
#: Response code that triggered the error
423-
self.response = resp
424-
self.code = resp.status_code
425-
self.errors = []
426-
try:
427-
error = resp.json()
428-
#: Message associated with the error
429-
self.msg = error.get('message')
430-
#: List of errors provided by GitHub
431-
if error.get('errors'):
432-
self.errors = error.get('errors')
433-
except: # Amazon S3 error
434-
self.msg = resp.content or '[No message]'
435-
436-
def __repr__(self):
437-
return '<GitHubError [{0}]>'.format(self.msg or self.code)
438-
439-
def __str__(self):
440-
return '{0} {1}'.format(self.code, self.msg)
441-
442-
@property
443-
def message(self):
444-
return self.msg

0 commit comments

Comments
 (0)