Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to work with the GithubException object #152

Closed
pconrad opened this issue Mar 27, 2013 · 14 comments
Closed

How to work with the GithubException object #152

pconrad opened this issue Mar 27, 2013 · 14 comments
Assignees
Labels

Comments

@pconrad
Copy link

pconrad commented Mar 27, 2013

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

status
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?

@pconrad
Copy link
Author

pconrad commented Mar 28, 2013

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

@ghost ghost assigned jacquev6 Mar 28, 2013
@jacquev6
Copy link
Member

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/.

@jacquev6
Copy link
Member

I will do that in branch topic/SpecificExceptions

@pconrad
Copy link
Author

pconrad commented Mar 28, 2013

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
notifications@github.comwrote:

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//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)

pconrad@cs.ucsb.edu, www.cs.ucsb.edu/~pconrad

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

@jacquev6
Copy link
Member

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

@pconrad
Copy link
Author

pconrad commented Mar 29, 2013

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
notifications@github.comwrote:

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


Reply to this email directly or view it on GitHubhttps://github.com//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)

pconrad@cs.ucsb.edu, www.cs.ucsb.edu/~pconrad

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

@jacquev6
Copy link
Member

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

@jacquev6
Copy link
Member

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

@jacquev6
Copy link
Member

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.

@jacquev6
Copy link
Member

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

@jacquev6
Copy link
Member

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

@pconrad pconrad added the v1 label Mar 2, 2014
@NicoHood
Copy link

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?

@izgzhen
Copy link

izgzhen commented Jul 26, 2020

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.

@s-t-e-v-e-n-k
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants