This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from librato/bugfix/rate_limit_error_parsing
Fix issue with parsing 'error' key vs 'errors' key from API
- Loading branch information
Showing
2 changed files
with
38 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,30 +63,41 @@ def error_message(self): | |
# } | ||
# } | ||
# | ||
# | ||
# Rate limiting example: | ||
# { | ||
# u'request_time': 1467306906, | ||
# u'error': u'You have hit the API limit for measurements | ||
# [measure:raw_rate]. Contact: [email protected] to adjust this limit.' | ||
# } | ||
def _parse_error_message(self): | ||
if isinstance(self.error_payload, str): | ||
# Payload is just a string | ||
return self.error_payload | ||
elif isinstance(self.error_payload, dict): | ||
payload = self.error_payload['errors'] | ||
messages = [] | ||
for key in payload: | ||
error_list = payload[key] | ||
if isinstance(error_list, str): | ||
# The error message is a scalar string, just tack it on | ||
msg = "%s: %s" % (key, error_list) | ||
messages.append(msg) | ||
elif isinstance(error_list, list): | ||
for error_message in error_list: | ||
msg = "%s: %s" % (key, error_message) | ||
messages.append(msg) | ||
elif isinstance(error_list, dict): | ||
for k in error_list: | ||
# e.g. "params: measure_time: " | ||
msg = "%s: %s: " % (key, k) | ||
msg += self._flatten_error_message(error_list[k]) | ||
# The API could return 'errors' or just 'error' with a flat message | ||
if 'error' in self.error_payload: | ||
return self.error_payload['error'] | ||
else: | ||
payload = self.error_payload['errors'] | ||
messages = [] | ||
for key in payload: | ||
error_list = payload[key] | ||
if isinstance(error_list, str): | ||
# The error message is a scalar string, just tack it on | ||
msg = "%s: %s" % (key, error_list) | ||
messages.append(msg) | ||
return ", ".join(messages) | ||
elif isinstance(error_list, list): | ||
for error_message in error_list: | ||
msg = "%s: %s" % (key, error_message) | ||
messages.append(msg) | ||
elif isinstance(error_list, dict): | ||
for k in error_list: | ||
# e.g. "params: measure_time: " | ||
msg = "%s: %s: " % (key, k) | ||
msg += self._flatten_error_message(error_list[k]) | ||
messages.append(msg) | ||
return ", ".join(messages) | ||
|
||
def _flatten_error_message(self, error_msg): | ||
if isinstance(error_msg, str): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters