achillean / shodan-python

The official Python library for Shodan
https://developer.shodan.io
Other
2.47k stars 552 forks source link

Return response body for errors #193

Open praseodym opened 1 year ago

praseodym commented 1 year ago

When the Shodan HTTP API returns an error status code, such as 403 Forbidden, this library will only return a APIError object.

However, the HTTP response will usually also include data in the response body, such as {"error": "Requires membership or higher to access"}. This data is currently ignored and not available through the library.

It would be useful if this response body is also made available by the library, for example to show a more informative error message to the end user.

achillean commented 1 year ago

You should be able to access the specific error message from the APIException.value property

praseodym commented 1 year ago

The contents of the APIException.value property are hardcoded in the library, so e.g. for status code 403 the error message will always be Access denied (403 Forbidden): https://github.com/achillean/shodan-python/blob/9ed74964c1447f6781afd254a087563cd5ebee20/shodan/client.py#L376-L379

For some status codes, the Shodan response body will contain more detailed information that is useful to have and return to the end user.

achillean commented 1 year ago

I think that's because for some of those responses the body isn't JSON but you're right, we should use the JSON error message if available as that is typically more precise.