diff options
author | 2011-11-09 00:35:13 +0100 | |
---|---|---|
committer | 2011-11-09 00:35:13 +0100 | |
commit | ed34830ab64837d16b2943887045a44f0760ee8e (patch) | |
tree | f91c1bebb8b11cccc5bab07dfb54ea5031923888 /github3/errors.py | |
parent | Fix/update typo. avatar_url in User (diff) | |
download | python-github3-ed34830ab64837d16b2943887045a44f0760ee8e.tar.xz python-github3-ed34830ab64837d16b2943887045a44f0760ee8e.zip |
New design. Merge develop branch
Diffstat (limited to 'github3/errors.py')
-rw-r--r-- | github3/errors.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/github3/errors.py b/github3/errors.py new file mode 100644 index 0000000..96693be --- /dev/null +++ b/github3/errors.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +# +# author: David Medina + +import json +import github3.exceptions as exceptions + +class GithubError(object): + """ Handler for API errors """ + + def __init__(self, response): + self._parser = json + self.status_code = response.status_code + if response.content: + self.debug = self._parser.loads(response.content) + else: + self.debug = {} + + def error_400(self): + return exceptions.BadRequest("400 - %s" % self.debug.get('message')) + + def error_404(self): + return exceptions.NotFound("404 - %s" % self.debug.get('message')) + + def error_422(self): + errors = self.debug.get('errors') + if errors: + errors = ['{resource}: {code} => {field}'.format(**error) + for error in errors] + return exceptions.UnprocessableEntity( + '422 - %s %s' % (self.debug.get('message'), errors)) + + def process(self): + raise_error = getattr(self, 'error_%s' % self.status_code, False) + if raise_error: + raise raise_error() |