From 8b4ca15a3617fc8d302bfc30afd23716b560ec61 Mon Sep 17 00:00:00 2001 From: patrick-zippenfenig Date: Thu, 28 Aug 2025 11:27:15 +0200 Subject: [PATCH 1/3] fix: Capture in-stream errors --- openmeteo_requests/Client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/openmeteo_requests/Client.py b/openmeteo_requests/Client.py index 0d04ddc..d587645 100644 --- a/openmeteo_requests/Client.py +++ b/openmeteo_requests/Client.py @@ -33,9 +33,12 @@ def _process_response( total = len(data) pos, step = 0, 4 while pos < total: + length = int.from_bytes(data[pos : pos + step], byteorder="little") + # In stream error messages start with "Unexpected" + if length == 0x78656E55: + raise OpenMeteoRequestsError(data[pos : total].decode("utf-8")) message = handler.GetRootAs(data, pos + step) messages.append(message) - length = int.from_bytes(data[pos : pos + step], byteorder="little") pos += length + step return messages From 627cfebbd8f54327c87fdc614bb2f5c60b8dc21a Mon Sep 17 00:00:00 2001 From: patrick-zippenfenig Date: Thu, 28 Aug 2025 11:29:17 +0200 Subject: [PATCH 2/3] linting --- openmeteo_requests/Client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmeteo_requests/Client.py b/openmeteo_requests/Client.py index d587645..928f62c 100644 --- a/openmeteo_requests/Client.py +++ b/openmeteo_requests/Client.py @@ -36,7 +36,7 @@ def _process_response( length = int.from_bytes(data[pos : pos + step], byteorder="little") # In stream error messages start with "Unexpected" if length == 0x78656E55: - raise OpenMeteoRequestsError(data[pos : total].decode("utf-8")) + raise OpenMeteoRequestsError(data[pos:total].decode("utf-8")) message = handler.GetRootAs(data, pos + step) messages.append(message) pos += length + step From 6b732b853a1e15ef9b90319b4fe057fd8c473b38 Mon Sep 17 00:00:00 2001 From: patrick-zippenfenig Date: Thu, 28 Aug 2025 11:47:02 +0200 Subject: [PATCH 3/3] add test --- tests/test_clients.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/test_clients.py b/tests/test_clients.py index 755ec2e..073b27d 100644 --- a/tests/test_clients.py +++ b/tests/test_clients.py @@ -5,10 +5,11 @@ from typing import Any import pytest -from niquests import AsyncSession, Session +from niquests import AsyncSession, Response, Session from openmeteo_sdk.Variable import Variable from openmeteo_requests import AsyncClient, Client, OpenMeteoRequestsError +from openmeteo_requests.Client import _process_response def _process_fetchall_basic_responses(responses: list) -> None: @@ -99,6 +100,16 @@ def test_sequential_requests_with_common_session(self, url: str, params: _Params # with pytest.raises(OpenMeteoRequestsError): _process_fetchall_basic_responses(client.weather_api(url=url, params=params)) + def test_error_stream( + self, + ) -> None: + response = Response() + # ruff: noqa: SLF001 + response._content = b"Unexpected error while streaming data: timeoutReached" + with pytest.raises(OpenMeteoRequestsError) as error: + _process_response(response) + assert str(error.value) == "Unexpected error while streaming data: timeoutReached" + @pytest.mark.asyncio class TestAsyncClient: