diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index ca46809..8b69946 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -1176,8 +1176,17 @@ 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. 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 @@ -1187,15 +1196,25 @@ 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. It can be called more than + once, including after each reconnect, which subscribes again, until the + subscription is unsubscribed. """ payload = { "operationName": "VehicleDepartureSchedules", @@ -1203,7 +1222,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 +1230,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 +1239,12 @@ 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. 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: @@ -1229,7 +1256,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"