aboutsummaryrefslogtreecommitdiffstats
path: root/github3/errors.py
diff options
context:
space:
mode:
authorAntti Kaihola <akaihol+github@ambitone.com>2011-11-09 13:04:48 +0200
committerAntti Kaihola <akaihol+github@ambitone.com>2011-11-09 13:04:48 +0200
commitf1a03e9fc8ce87ca1beeae5a92082bb32d342df3 (patch)
tree6bca689287379b249aff19fbae075182dc22f95a /github3/errors.py
parentAdded create_gist() example to readme (diff)
parentFixing bugs. Crazy night :S (diff)
downloadpython-github3-f1a03e9fc8ce87ca1beeae5a92082bb32d342df3.tar.xz
python-github3-f1a03e9fc8ce87ca1beeae5a92082bb32d342df3.zip
Merged create_gist() into current HEAD of copitux/develop
Diffstat (limited to 'github3/errors.py')
-rw-r--r--github3/errors.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/github3/errors.py b/github3/errors.py
new file mode 100644
index 0000000..09e616b
--- /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
+ try:
+ self.debug = self._parser.loads(response.content)
+ except ValueError:
+ self.debug = {'message': response.content}
+
+ 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()