From 60b108cda51bf2f16cf5c666bbe89790d4db8bab Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 10 Jul 2020 13:13:13 +0100 Subject: [PATCH] Add response object in all MailChimpError exceptions This allows any exception to be examined with more detail even when the response contains JSON data, by consistently including the response object --- mailchimp3/mailchimpclient.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/mailchimp3/mailchimpclient.py b/mailchimp3/mailchimpclient.py index b1c29a5..4fe38ef 100644 --- a/mailchimp3/mailchimpclient.py +++ b/mailchimp3/mailchimpclient.py @@ -23,6 +23,7 @@ _logger = logging.getLogger('mailchimp3.client') + def _enabled_or_noop(fn): @functools.wraps(fn) def wrapper(self, *args, **kwargs): @@ -36,11 +37,19 @@ class MailChimpError(Exception): def _raise_response_error(r): - # in case of a 500 error, the response might not be a JSON + """ + Return a MailChimpError to which we pass: + - the response object + - the response's JSON data if found. + """ + error_data = {"response": r} try: - error_data = r.json() + json_data = r.json() except ValueError: - error_data = { "response": r } + # in case of a 500 error, the response might not be a JSON + pass + else: + error_data.update(json_data) raise MailChimpError(error_data)