Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def handle_api_error(cls, response):
if not (200 <= response.status_code < 300):
raise ApiError(response)
def __init__(self, response, message=None):
super(ApiError, self).__init__(message)
status_code = response.status_code
try:
json = response.json()
except ValueError:
raise Exception('Status: <{}> JSON response from API could not be decoded.'.format(status_code)) # FIXME: is this useful? should i just let it raise its own exception?
try:
error = json['error'] # FIXME: not confident that this is the best way to access errors
raise Exception(error.get('message', ''))
except KeyError:
raise Exception('Invalid response from API: <{}> {}'.format(status_code, json))