dronefly-garden / dronefly

Red Discord Bot V3 cogs for naturalists.
Other
16 stars 3 forks source link

gracefully handle non-200 status when no JSON returned #146

Closed synrg closed 3 years ago

synrg commented 3 years ago

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 3 years ago

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