dronefly-garden/dronefly

gracefully handle non-200 status when no JSON returned

synrg opened this issue · 1 comments

synrg commented

In _get_rate_limited in api.py, we cannot assume a non-200 HTTP response will contain a json payload. In a recent iNaturalist site outage, this resulted in this unhandled exception instead of a nice "Lookup failed" with response code & message text as intended:

ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html; charset=utf-8',
 url=URL('https://api.inaturalist.org/v1/taxa/autocomplete?preferred_place_id=6853&q=down')

The error handling needs to be a bit smarter than that, displaying whatever non-JSON message text it can, or else defaulting to a sensible message if none was supplied.

synrg commented

Fixed in 4a449a6 through 3975ef5 . Tested with a simulated error (break the URL using replace('v1', 'v3') to make it 404) and looks good.