Pygithub: How to work with the GithubException object

Created on 28 Mar 2013  ·  14Comments  ·  Source: PyGithub/PyGithub

I have a question about how to work with the GithubException object

Sample Code:
try:
team = org.create_team(teamName,
[],
"push");
except GithubException as e:
print (e)

When I print (e) I get:

 422 {u'message': u'Validation Failed', u'errors': [{u'field': u'name', u'code': u'already_exists', u'resource': u'Team'}]}

What I'd like to be able to do is see the list of attributes and member functions of the GithubException object so that I can pull out various stuff to check for particular events... but I can'f find this in the documentation for the PyGithub API. Maybe I just need to understand better the relationship between the PyGithub API and the underlying github RESTful API.

From the source, it appears that there are two properties of the GithubException object:

https://github.com/jacquev6/PyGithub/blob/master/github/GithubException.py

data

and putting that together with the output, it appears the 422 is the status (perhaps returned by the API?), and the data is a dictionary object (perhaps an encoding of the JSON that got returned?) but these are only guesses.

It appears that the only place GithubException occurs in the source is in Requester.py---and this tends to confirm my guesses.

Is there somewhere in the documentation for the github API itself where one can find the keys that one would expect to find in the JSON object, i.e. in this case "message", "errors", and then within "errors", "field", "code", "and "resource"?

From trial and error guesswork, I settled on code like this, but this feels "hacky---I'd be more comfortable if I knew whether I was doing this right...

team = False   # Sentinel to see if it succeeded or failed
try:
   team = org.create_team(teamName,
                     [],
                     "push");
   print(" team {0} created...".format(teamName),end='')
except GithubException as e:
   if (e.data['errors'][0]['code']=='already_exists'):
      print(" team {0} already exists...".format(teamName),end='') 
   else:
      print (e)

if (team != False):
   # do something with team...

Am I on the right track?

question

Most helpful comment

Hi,
I try to catch an exception:

github.GithubException.BadCredentialsException: 401 {'message': 'Bad credentials', 'documentation_url': 'https://developer.github.com/v3'}

Now I i try to access it directly I get the following error:

AttributeError: type object 'GithubException' has no attribute 'BadCredentialsException'

How can I catch this specific exception and not only a general githubexception?

All 14 comments

Note that User @jacquev6 partially addressed this in a comment on issue #82 .

This is the right track indeed. Here are a few remarks.

You're right about status and data: they are the HTTP status and the decoded json payload. I've done that this way because I had no idea of the types of errors that could occur, so I wasn't able to create specific properties as in all other classes in PyGithub. (I know that a NamedUser always has a name, but there is no such knowledge about exceptions/errors)

So, whenever the Github API v3 returns a HTTP status above 400, I raise a GithubException with this status and the payload.

Checking the Github API v3 documentation today, I see a few words about errors: http://developer.github.com/v3/#client-errors but in fact there are more possible errors. For example, 404 errors for non-existing objects, 401 for bad authentication, etc.

So, what I can do now that I know the errors a bit more is to create sub-classes of GithubException, and raise them when I see a specific type of error. This would allow the client to except specific classes of error, and let the other classes go up the stack.

I will do that in version 1.14.0 and take this as an opportunity to document it in http://jacquev6.github.com/PyGithub/.

I will do that in branch topic/SpecificExceptions

This sounds terrific. Again, thanks for all of your hard work, and
excellent software design skills. The product has been extraordinarily
useful and easy to figure out---and addressing this will make it even
better.

On Thu, Mar 28, 2013 at 12:59 PM, Vincent Jacques
[email protected]:

I will do that in branch topic/SpecificExceptionshttps://github.com/jacquev6/PyGithub/tree/topic/SpecificExceptions


Reply to this email directly or view it on GitHubhttps://github.com/jacquev6/PyGithub/issues/152#issuecomment-15611026
.

Phill Conrad, Lecturer (SOE)*, Dept. of Computer Science
University of California, Santa Barbara
Joint Appointment: College of Creative Studies (www.ccs.ucsb.edu)

[email protected], www.cs.ucsb.edu/~pconrad

*SOE: a UC teaching faculty appointment, corresponding in rank and job
security to a tenured associate professor

Thanks \o/ It's always very nice to ear a satisfied client!

Yes, I think I shall give you a raise---double the salary I'm paying you
now. :-)

On Thu, Mar 28, 2013 at 1:33 PM, Vincent Jacques
[email protected]:

Thanks \o/ It's always very nice to ear a satisfied client!


Reply to this email directly or view it on GitHubhttps://github.com/jacquev6/PyGithub/issues/152#issuecomment-15612761
.

Phill Conrad, Lecturer (SOE)*, Dept. of Computer Science
University of California, Santa Barbara
Joint Appointment: College of Creative Studies (www.ccs.ucsb.edu)

[email protected], www.cs.ucsb.edu/~pconrad

*SOE: a UC teaching faculty appointment, corresponding in rank and job
security to a tenured associate professor

Huhu, that would be both my biggest and smallest raise ever!

Note to myself:

  • Check test github.tests.Organization.testMembers: there is a 403 status
  • Add test case for specific exn when bad auth with token

Part of this issue was deliver in 1.14.0, but I keep it for version 1.15.0 to add a few other specific exception types.

New specific exception to create: HTTP 403 "bad user agent" (see #160)

I'm closing this issue now, but I will continue to add specific types of exception when I encounter them.

Hi,
I try to catch an exception:

github.GithubException.BadCredentialsException: 401 {'message': 'Bad credentials', 'documentation_url': 'https://developer.github.com/v3'}

Now I i try to access it directly I get the following error:

AttributeError: type object 'GithubException' has no attribute 'BadCredentialsException'

How can I catch this specific exception and not only a general githubexception?

Hi,
I try to catch an exception:

github.GithubException.BadCredentialsException: 401 {'message': 'Bad credentials', 'documentation_url': 'https://developer.github.com/v3'}

Now I i try to access it directly I get the following error:

AttributeError: type object 'GithubException' has no attribute 'BadCredentialsException'

How can I catch this specific exception and not only a general githubexception?

@NicoHood use github.BadCredentialsException -- which is more clear from source code.

GithubException is a class as well as a module, the full module path to the class is github.GithubException.GithubException, which you can't import BadCredentialsException from. All of the exceptions are exported via github, so from github import BadCredentialsException will work fine.

Was this page helpful?
0 / 5 - 0 ratings