Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch JSONDecodeError #270

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion accuweather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ async def _async_get_data(self, url: str) -> Any:
raise InvalidApiKeyError("Invalid API key")

if resp.status != HTTPStatus.OK.value:
error_text = orjson.loads(await resp.text())
try:
error_text = orjson.loads(await resp.text())
except orjson.JSONDecodeError as exc:
raise ApiError(f"Can't decode API response: {exc}") from exc
if error_text["Message"] == REQUESTS_EXCEEDED:
raise RequestsExceededError(
"The allowed number of requests has been exceeded"
Expand Down
20 changes: 20 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,23 @@ async def test_requests_exceeded_error():
await accuweather.async_get_location()

await session.close()


@pytest.mark.asyncio
async def test_json_decode_error():
"""Test with JSON decode error."""
session = aiohttp.ClientSession()

with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=32-character-string-1234567890qw&q=52.0677904%252C19.4795644&language=en-us",
payload=None,
status=502,
)
accuweather = AccuWeather(
VALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
with pytest.raises(ApiError, match="Can't decode API response"):
await accuweather.async_get_location()

await session.close()