blob: 9b84479cd2964e360f78873a54998deb1558275b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from pygithub3.core.utils import json
from pygithub3.exceptions import NotFound, BadRequest, UnprocessableEntity
class GithubError(object):
""" Handler for API errors """
def __init__(self, response):
self.response = response
self.status_code = response.status_code
try:
self.debug = json.loads(response.content)
except (ValueError, TypeError):
self.debug = {'message': response.content}
def error_404(self):
raise NotFound("404 - %s" % self.debug.get('message'))
def error_400(self):
raise BadRequest("400 - %s" % self.debug.get('message'))
def error_422(self):
errors = self.debug.get('errors', ())
errors = ['Resource: {resource}: {field} => {code}'.format(**error)
for error in errors]
raise 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()
self.response.raise_for_status()
|