From 7b3e4c15ed6182963d66ffa9f0522acd0765275c Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Fri, 29 Mar 2013 13:01:06 +0100 Subject: [PATCH] Raise a specific exception for bad credentials (issue #152) --- github/GithubException.py | 6 ++++++ github/Requester.py | 7 ++++++- github/__init__.py | 2 +- github/tests/Exceptions.py | 10 ++++++++++ .../SpecificExceptions.testBadCredentials.txt | 11 +++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 github/tests/ReplayData/SpecificExceptions.testBadCredentials.txt diff --git a/github/GithubException.py b/github/GithubException.py index b8235071..7e040da8 100644 --- a/github/GithubException.py +++ b/github/GithubException.py @@ -40,3 +40,9 @@ class GithubException(Exception): 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) + """ diff --git a/github/Requester.py b/github/Requester.py index 7598e3f2..3cb83d8a 100644 --- a/github/Requester.py +++ b/github/Requester.py @@ -90,9 +90,14 @@ class Requester: 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 diff --git a/github/__init__.py b/github/__init__.py index 3f20d650..f2383b24 100644 --- a/github/__init__.py +++ b/github/__init__.py @@ -24,7 +24,7 @@ All classes inherit from :class:`github.GithubObject.GithubObject`. 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 diff --git a/github/tests/Exceptions.py b/github/tests/Exceptions.py index c068122f..c61851f1 100644 --- a/github/tests/Exceptions.py +++ b/github/tests/Exceptions.py @@ -87,3 +87,13 @@ class Exceptions(Framework.TestCase): # To stay compatible with Python 2.6, we 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) diff --git a/github/tests/ReplayData/SpecificExceptions.testBadCredentials.txt b/github/tests/ReplayData/SpecificExceptions.testBadCredentials.txt new file mode 100644 index 00000000..f772eabe --- /dev/null +++ b/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"} +