Skip to content

Commit

Permalink
Show how to extract error attributes for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Leland committed May 24, 2017
1 parent d9d7e23 commit 689bd4b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions examples/transmissions/handle_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@
except SparkPostAPIException as err:
# http response status code
print(err.status)
# list of formatted errors
print(err.errors)
# python requests library response object
# http://docs.python-requests.org/en/master/api/#requests.Response
print(err.response.json())
# list of formatted errors
print(err.errors)
# you can loop through the errors and extract message, code, description
for e in err.response.json()['errors']:
error_template = "{message} Code: {code} Description: {desc} \n"
print(error_template.format(message=e.get('message', ''),
code=e.get('code', 'none'),
desc=e.get('description', 'none')))

0 comments on commit 689bd4b

Please sign in to comment.