Skip to content

Commit

Permalink
Raise a specific exception for non-existing objects (issue #152)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquev6 committed Mar 29, 2013
1 parent 7b3e4c1 commit 0bc3689
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions github/GithubException.py
Expand Up @@ -46,3 +46,9 @@ class BadCredentialsException(GithubException):
"""
Exception raised in case of bad credentials (when Github API replies with a 401 or 403 HTML status)
"""


class UnknownObjectException(GithubException):
"""
Exception raised a non-existing object is requested (when Github API replies with a 404 HTML status)
"""
2 changes: 2 additions & 0 deletions github/Requester.py
Expand Up @@ -96,6 +96,8 @@ def __check(self, status, responseHeaders, output):
def __createException(self, status, output):
if status == 401 and output["message"] == "Bad credentials":
return GithubException.BadCredentialsException(status, output)
if status == 404 and output["message"] == "Not Found":
return GithubException.UnknownObjectException(status, output)
return GithubException.GithubException(status, output)

def __structuredFromJson(self, data):
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, BadCredentialsException
from GithubException import GithubException, BadCredentialsException, UnknownObjectException
from InputFileContent import InputFileContent
from InputGitAuthor import InputGitAuthor
from InputGitTreeElement import InputGitTreeElement
Expand Down
8 changes: 8 additions & 0 deletions github/tests/Exceptions.py
Expand Up @@ -97,3 +97,11 @@ def testBadCredentials(self):
except github.BadCredentialsException, exception:
raised = True
self.assertTrue(raised)

def testUnknownObject(self):
raised = False
try:
self.g.get_user().get_repo("Xxx")
except github.UnknownObjectException:
raised = True
self.assertTrue(raised)
22 changes: 22 additions & 0 deletions github/tests/ReplayData/SpecificExceptions.testUnknownObject.txt
@@ -0,0 +1,22 @@
https
GET
api.github.com
None
/user
{'Authorization': 'Basic login_and_password_removed'}
null
200
[('status', '200 OK'), ('x-ratelimit-remaining', '4971'), ('content-length', '801'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"0e6c8f1cbb0c4f0eae96d8a76de9a43f"'), ('date', 'Sat, 02 Jun 2012 12:11:46 GMT'), ('content-type', 'application/json; charset=utf-8')]
{"type":"User","total_private_repos":5,"company":"Criteo","gravatar_id":"b68de5ae38616c296fa345d2b9df2225","login":"jacquev6","public_gists":3,"email":"vincent@vincent-jacques.net","owned_private_repos":5,"avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","private_gists":5,"collaborators":0,"created_at":"2010-07-09T06:10:06Z","blog":"http://vincent-jacques.net","location":"Paris, France","url":"https://api.github.com/users/jacquev6","following":24,"disk_usage":16988,"public_repos":10,"name":"Vincent Jacques","hireable":false,"followers":13,"html_url":"https://github.com/jacquev6","id":327146,"plan":{"private_repos":5,"collaborators":1,"space":614400,"name":"micro"},"bio":""}

https
GET
api.github.com
None
/repos/jacquev6/Xxx
{'Authorization': 'Basic login_and_password_removed'}
null
404
[('status', '404 Not Found'), ('x-ratelimit-remaining', '4970'), ('content-length', '23'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"e66a7a6c91e2c26803f3f49feb7a883f"'), ('date', 'Sat, 02 Jun 2012 12:11:47 GMT'), ('content-type', 'application/json; charset=utf-8')]
{"message":"Not Found"}

0 comments on commit 0bc3689

Please sign in to comment.