Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIError support for Status Code #214

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions shodan/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion shodan/exception.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down