Skip to content

Commit

Permalink
Raise a specific exception for bad credentials (issue #152)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquev6 committed Mar 29, 2013
1 parent f5d8e22 commit 7b3e4c1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions github/GithubException.py
Expand Up @@ -40,3 +40,9 @@ def data(self):

def __str__(self):
return str(self.status) + " " + str(self.data)


class BadCredentialsException(GithubException):
"""
Exception raised in case of bad credentials (when Github API replies with a 401 or 403 HTML status)
"""
7 changes: 6 additions & 1 deletion github/Requester.py
Expand Up @@ -90,9 +90,14 @@ def requestMultipartAndCheck(self, verb, url, parameters, input):
def __check(self, status, responseHeaders, output):
output = self.__structuredFromJson(output)
if status >= 400:
raise GithubException.GithubException(status, output)
raise self.__createException(status, output)
return responseHeaders, output

def __createException(self, status, output):
if status == 401 and output["message"] == "Bad credentials":
return GithubException.BadCredentialsException(status, output)
return GithubException.GithubException(status, output)

def __structuredFromJson(self, data):
if len(data) == 0:
return None
Expand Down
2 changes: 1 addition & 1 deletion github/__init__.py
Expand Up @@ -24,7 +24,7 @@
import logging

from MainClass import Github
from GithubException import GithubException
from GithubException import GithubException, BadCredentialsException
from InputFileContent import InputFileContent
from InputGitAuthor import InputGitAuthor
from InputGitTreeElement import InputGitTreeElement
Expand Down
10 changes: 10 additions & 0 deletions github/tests/Exceptions.py
Expand Up @@ -87,3 +87,13 @@ def testBadAuthentication(self):
else:
self.assertEqual(str(exception), "401 {'message': 'Bad credentials'}") # pragma no cover
self.assertTrue(raised)


class SpecificExceptions(Framework.TestCase):
def testBadCredentials(self):
raised = False
try:
github.Github("BadUser", "BadPassword").get_user().login
except github.BadCredentialsException, exception:
raised = True
self.assertTrue(raised)
11 changes: 11 additions & 0 deletions github/tests/ReplayData/SpecificExceptions.testBadCredentials.txt
@@ -0,0 +1,11 @@
https
GET
api.github.com
None
/user
{'Authorization': 'Basic login_and_password_removed'}
null
401
[('status', '401 Unauthorized'), ('content-length', '29'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('etag', '"ca6a3702f840b6bff0bb1bca6be0337c"'), ('date', 'Sat, 02 Jun 2012 12:12:32 GMT'), ('content-type', 'application/json; charset=utf-8')]
{"message":"Bad credentials"}

0 comments on commit 7b3e4c1

Please sign in to comment.