Skip to content

Commit

Permalink
fix: reoder exception handling to correctly parse a broken response
Browse files Browse the repository at this point in the history
  • Loading branch information
SlowMo24 committed Jul 31, 2024
1 parent 260ed02 commit ea03351
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixed

- Ordering of exception handling to correctly parse a broken response

## 0.3.2

### Fixed
Expand Down
34 changes: 17 additions & 17 deletions ohsome/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import shapely
from requests import Session
from requests.adapters import HTTPAdapter
from requests.exceptions import RetryError
from requests.exceptions import RetryError, JSONDecodeError
from urllib3 import Retry

from ohsome import OhsomeException, OhsomeResponse
Expand Down Expand Up @@ -363,6 +363,22 @@ def _handle_request(self) -> OhsomeResponse:
response=e.response,
)

except (ValueError, JSONDecodeError) as e:
if response:
error_code, message = extract_error_message_from_invalid_json(
response.text
)
else:
message = str(e)
error_code = None
ohsome_exception = OhsomeException(
message=message,
url=self._url,
error_code=error_code,
params=self._parameters,
response=response,
)

except requests.exceptions.RequestException as e:
if isinstance(e, RetryError):
# retry one last time without retries, this will raise the original error instead of a cryptic retry
Expand All @@ -386,22 +402,6 @@ def _handle_request(self) -> OhsomeResponse:
error_code=440,
)

except ValueError as e:
if response:
error_code, message = extract_error_message_from_invalid_json(
response.text
)
else:
message = str(e)
error_code = None
ohsome_exception = OhsomeException(
message=message,
url=self._url,
error_code=error_code,
params=self._parameters,
response=response,
)

except AttributeError:
ohsome_exception = OhsomeException(
message=f"Seems like {self._url} is not a valid endpoint.",
Expand Down
28 changes: 28 additions & 0 deletions ohsome/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ def test_timeout_error(base_client):
)


def test_borken_response_timeout_error(base_client):
"""
Test whether an OhsomeException is raised, if the ohsome API contains a JSONDecodeError
:return:
"""

bboxes = "8.67066,49.41423,8.68177,49.4204"
time = "2010-01-01/2011-01-01/P1Y"
fltr = "building=* and type:way"
timeout = 30

client = base_client
with pytest.raises(ohsome.OhsomeException) as e_info:
with responses.RequestsMock() as rsps:
rsps.post(
"https://api.ohsome.org/v1/elements/geometry",
body=b'{\n "attribution" : {\n "url" : "https://ohsome.org/copyrights",\n "text" : "\xc2\xa9 OpenStreetMap contributors"\n },\n "apiVersion" : "1.10.3",\n "type" : "FeatureCollection",\n "features" : [{\n "timestamp" : "2024-07-31T10:37:31.603661767",\n "status" : 413,\n "message" : "The given query is too large in respect to the given timeout. Please use a smaller region and/or coarser time period.",\n "requestUrl" : "https://api.ohsome.org/v1/elements/geometry"\n}',
)
client.elements.geometry.post(
bboxes=bboxes, time=time, filter=fltr, timeout=timeout
)
assert (
"The given query is too large in respect to the given timeout. Please use a smaller region and/or coarser "
"time period." in e_info.value.message
)
assert e_info.value.error_code == 413


@pytest.mark.vcr
def test_invalid_url():
"""
Expand Down

0 comments on commit ea03351

Please sign in to comment.