From 689bd4b9503946c5238f5c3fe07cae2caebd1197 Mon Sep 17 00:00:00 2001 From: Rich Leland Date: Wed, 24 May 2017 10:26:58 -0400 Subject: [PATCH] Show how to extract error attributes for exceptions --- examples/transmissions/handle_exception.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/transmissions/handle_exception.py b/examples/transmissions/handle_exception.py index 0bf57fa..063fcbf 100644 --- a/examples/transmissions/handle_exception.py +++ b/examples/transmissions/handle_exception.py @@ -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')))