diff --git a/shodan/client.py b/shodan/client.py index 21c70af..8c1036b 100644 --- a/shodan/client.py +++ b/shodan/client.py @@ -384,24 +384,24 @@ def _request(self, function, params, service='shodan', method='get', json_data=N # Otherwise lets raise the error message error = u'{}'.format(e) - raise APIError(error) + raise APIError(error, data.status_code) elif data.status_code == 403: - raise APIError('Access denied (403 Forbidden)') + raise APIError('Access denied (403 Forbidden)', data.status_code) elif data.status_code == 502: - raise APIError('Bad Gateway (502)') + raise APIError('Bad Gateway (502)', data.status_code) # Parse the text into JSON try: - data = data.json() + parsed_data = data.json() except ValueError: - raise APIError('Unable to parse JSON response') + raise APIError('Unable to parse JSON response', data.status_code) # Raise an exception if an error occurred - if type(data) == dict and 'error' in data: - raise APIError(data['error']) + if isinstance(parsed_data, dict) and 'error' in parsed_data: + raise APIError(parsed_data['error'], data.status_code) # Return the data - return data + return parsed_data def count(self, query, facets=None): """Returns the total number of search results for the query. diff --git a/shodan/exception.py b/shodan/exception.py index 75b158e..2534904 100644 --- a/shodan/exception.py +++ b/shodan/exception.py @@ -1,7 +1,9 @@ class APIError(Exception): """This exception gets raised whenever a non-200 status code was returned by the Shodan API.""" - def __init__(self, value): + + def __init__(self, value, status_code=0): self.value = value + self.status_code = status_code def __str__(self): return self.value