Skip to content

Commit

Permalink
Add response object in all MailChimpError exceptions
Browse files Browse the repository at this point in the history
This allows any exception to be examined with more detail even when the response contains JSON data, by consistently including the response object
  • Loading branch information
Clepsyd committed Jul 10, 2020
1 parent d454e62 commit 60b108c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions mailchimp3/mailchimpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

_logger = logging.getLogger('mailchimp3.client')


def _enabled_or_noop(fn):
@functools.wraps(fn)
def wrapper(self, *args, **kwargs):
Expand All @@ -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)


Expand Down

0 comments on commit 60b108c

Please sign in to comment.