Skip to content

Commit

Permalink
Merge pull request #77 from jarekwg/master
Browse files Browse the repository at this point in the history
Better handle BadRequestException's from the JSON API
  • Loading branch information
aidanlister committed Jun 9, 2015
2 parents 6234d8d + 7b4e7b6 commit 4dba6f5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions xero/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ class XeroBadRequest(XeroException):
# HTTP 400: Bad Request
def __init__(self, response):
if response.headers['content-type'].startswith('application/json'):
super(XeroBadRequest, self).__init__(response, response.text)
data = json.loads(response.text)
msg = "%s: %s" % (data['Type'], data['Message'])
self.errors = [err['Message']
for elem in data['Elements']
for err in elem['ValidationErrors']
]
super(XeroBadRequest, self).__init__(response, msg=msg)

elif response.headers['content-type'].startswith('text/html'):
payload = parse_qs(response.text)
self.problem = payload['oauth_problem'][0]
self.errors = [
payload['oauth_problem'][0],
]
super(XeroBadRequest, self).__init__(response, payload['oauth_problem_advice'][0])

else:
Expand Down

0 comments on commit 4dba6f5

Please sign in to comment.