Skip to content

Commit

Permalink
Geocodeio: fix error handler for non-adapter exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
KostyaEsmukov committed Apr 3, 2021
1 parent a03d21c commit a763d16
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions geopy/geocoders/geocodio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import partial
from urllib.parse import urlencode

from geopy.adapters import AdapterHTTPError
from geopy.exc import GeocoderQueryError, GeocoderQuotaExceeded
from geopy.geocoders.base import DEFAULT_SENTINEL, Geocoder
from geopy.location import Location
Expand Down Expand Up @@ -222,22 +223,25 @@ def _geocoder_exception_handler(self, error):
``403`` status code for exceeded quotas instead of the ``429`` code mapped in
:const:`~geopy.geocoders.base.ERROR_CODE_MAP`
"""
if not isinstance(error, AdapterHTTPError):
return
if error.status_code is None or error.text is None:
return
if error.status_code == 422:
error_message = self._get_error_message(error)
raise GeocoderQueryError(error_message)
raise GeocoderQueryError(error_message) from error
if error.status_code == 403:
error_message = self._get_error_message(error)
quota_exceeded_snippet = "You can't make this request as it is " \
"above your daily maximum."
if quota_exceeded_snippet in error_message:
raise GeocoderQuotaExceeded(error_message)
raise GeocoderQuotaExceeded(error_message) from error

@staticmethod
def _get_error_message(error):
def _get_error_message(self, error):
"""Try to extract an error message from the 'error' property of a JSON response.
"""
try:
error_message = json.loads(error.text).get('error')
except json.JSONDecodeError:
error_message = None
return error_message or 'There was an unknown issue with the query.'
return error_message or error.text

0 comments on commit a763d16

Please sign in to comment.