From 7fab82a34a4abc5c8cc5698a51b4075c6a53da99 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Tue, 17 Nov 2026 02:19:55 +0000 Subject: [PATCH 1/2] Pass subscription errors to an on_error callback subscribe_for_vehicle_updates, subscribe_for_departure_schedules and subscribe_for_parallax_messages take an optional, keyword-only on_error, which gets the subscription's error messages. A field that fails GraphQL validation, for example, ends the subscription with an error message and no data; without on_error that's only logged at debug level, so the caller can't tell its subscription has stopped. Co-Authored-By: Claude Opus 5.5 --- src/rivian/rivian.py | 31 ++++++++++++++++++++++++++----- tests/test_ws_monitor.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index ca46809..167d71b 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -1176,8 +1176,15 @@ async def subscribe_for_vehicle_updates( vehicle_id: str, callback: Callable[[dict[str, Any]], None], properties: set[str] | None = None, + *, + on_error: Callable[[dict[str, Any]], None] | None = None, ) -> Callable | None: - """Open a web socket connection to receive updates.""" + """Open a web socket connection to receive updates. + + `on_error` gets the subscription's error messages, such as a field that + fails GraphQL validation (the subscription then ends without data); + without it they're only logged at debug level. + """ if not properties: properties = VEHICLE_STATES_SUBSCRIPTION_PROPERTIES @@ -1187,15 +1194,23 @@ async def subscribe_for_vehicle_updates( "variables": {"vehicleID": vehicle_id}, } return await self._start_subscription( - payload, callback, f"{vehicle_id} vehicle updates" + payload, callback, f"{vehicle_id} vehicle updates", on_error ) async def subscribe_for_departure_schedules( - self, vehicle_id: str, callback: Callable[[dict[str, Any]], None] + self, + vehicle_id: str, + callback: Callable[[dict[str, Any]], None], + *, + on_error: Callable[[dict[str, Any]], None] | None = None, ) -> Callable[[], Awaitable[None]] | None: """Open a web socket connection to receive departure schedules. The current schedules are sent right after subscribing. + + `on_error` gets the subscription's error messages, such as a field that + fails GraphQL validation (the subscription then ends without data); + without it they're only logged at debug level. """ payload = { "operationName": "VehicleDepartureSchedules", @@ -1203,7 +1218,7 @@ async def subscribe_for_departure_schedules( "variables": {"vehicleId": vehicle_id}, } return await self._start_subscription( - payload, callback, f"{vehicle_id} departure schedules" + payload, callback, f"{vehicle_id} departure schedules", on_error ) async def subscribe_for_parallax_messages( @@ -1211,6 +1226,8 @@ async def subscribe_for_parallax_messages( vehicle_id: str, callback: Callable[[dict[str, Any]], None], rvms: list[str] | None = None, + *, + on_error: Callable[[dict[str, Any]], None] | None = None, ) -> Callable[[], Awaitable[None]] | None: """Open a web socket connection to receive Parallax message updates. @@ -1218,6 +1235,10 @@ async def subscribe_for_parallax_messages( the server sends every RVM topic for the vehicle, including ones with no decoder yet (`decode_parallax_message` returns None for those). Pass a list, e.g. `PARALLAX_RVMS`, to only receive those topics. + + `on_error` gets the subscription's error messages, such as a field that + fails GraphQL validation (the subscription then ends without data); + without it they're only logged at debug level. """ variables: dict[str, Any] = {"vehicleId": vehicle_id} if rvms is not None: @@ -1229,7 +1250,7 @@ async def subscribe_for_parallax_messages( } topics = "all Parallax RVMs" if rvms is None else f"{len(rvms)} Parallax RVMs" return await self._start_subscription( - payload, callback, f"{vehicle_id} {topics}" + payload, callback, f"{vehicle_id} {topics}", on_error ) async def subscribe_for_parallax_command_state( diff --git a/tests/test_ws_monitor.py b/tests/test_ws_monitor.py index 655b9d0..51cc391 100644 --- a/tests/test_ws_monitor.py +++ b/tests/test_ws_monitor.py @@ -866,3 +866,31 @@ async def test_cancelled_subscribe_keeps_another_waiting_for_the_ack( assert await waiting assert monitor.connected assert len(monitor._subscriptions) == 1 + + +@pytest.mark.parametrize( + "subscribe", + [ + lambda rivian, on_error: rivian.subscribe_for_vehicle_updates( + "id", lambda _: None, {"gnssLocation"}, on_error=on_error + ), + lambda rivian, on_error: rivian.subscribe_for_departure_schedules( + "id", lambda _: None, on_error=on_error + ), + lambda rivian, on_error: rivian.subscribe_for_parallax_messages( + "id", lambda _: None, on_error=on_error + ), + ], + ids=["vehicle updates", "departure schedules", "parallax messages"], +) +async def test_subscriptions_pass_errors_to_on_error( + server: FakeServer, rivian: Rivian, subscribe: Any +) -> None: + """A subscription's error, e.g. a field failing validation, reaches on_error.""" + server.error_on_subscribe = True + errors: list[dict[str, Any]] = [] + + assert await subscribe(rivian, errors.append) + await wait_for(lambda: errors) + + assert errors[0]["type"] == "error" From ea67f4cf38851976cbccc429608bf7beeb65f9ba Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Tue, 17 Nov 2026 02:26:04 +0000 Subject: [PATCH 2/2] Say on_error can be called more than once A reconnect subscribes again, so a subscription that keeps failing calls on_error each time until it's unsubscribed. Co-Authored-By: Claude Opus 5.5 --- src/rivian/rivian.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index 167d71b..8b69946 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -1183,7 +1183,9 @@ async def subscribe_for_vehicle_updates( `on_error` gets the subscription's error messages, such as a field that fails GraphQL validation (the subscription then ends without data); - without it they're only logged at debug level. + without it they're only logged at debug level. It can be called more than + once, including after each reconnect, which subscribes again, until the + subscription is unsubscribed. """ if not properties: properties = VEHICLE_STATES_SUBSCRIPTION_PROPERTIES @@ -1210,7 +1212,9 @@ async def subscribe_for_departure_schedules( `on_error` gets the subscription's error messages, such as a field that fails GraphQL validation (the subscription then ends without data); - without it they're only logged at debug level. + without it they're only logged at debug level. It can be called more than + once, including after each reconnect, which subscribes again, until the + subscription is unsubscribed. """ payload = { "operationName": "VehicleDepartureSchedules", @@ -1238,7 +1242,9 @@ async def subscribe_for_parallax_messages( `on_error` gets the subscription's error messages, such as a field that fails GraphQL validation (the subscription then ends without data); - without it they're only logged at debug level. + without it they're only logged at debug level. It can be called more than + once, including after each reconnect, which subscribes again, until the + subscription is unsubscribed. """ variables: dict[str, Any] = {"vehicleId": vehicle_id} if rvms is not None: