From 2e05a644b6c03847360ea1aeae0d40469eab9a9a Mon Sep 17 00:00:00 2001 From: Leonhard S Date: Sat, 15 May 2021 11:34:50 +0200 Subject: [PATCH 1/2] Ignore invalid ESS payloads with warning --- auraxium/event/_client.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/auraxium/event/_client.py b/auraxium/event/_client.py index d922cdd..dea3b63 100644 --- a/auraxium/event/_client.py +++ b/auraxium/event/_client.py @@ -5,6 +5,7 @@ from typing import (Any, Callable, Coroutine, Dict, Iterator, List, Optional, Type, TypeVar, Union, cast, overload) import backoff +import pydantic import websockets @@ -376,7 +377,16 @@ def _process_payload(self, response: str) -> None: # Event messages if service == 'event': if data['type'] == 'serviceMessage': - event = _event_factory(cast(CensusData, data['payload'])) + try: + event = _event_factory(cast(CensusData, data['payload'])) + except pydantic.ValidationError: + _log.warning( + 'Ignoring unsupported payload: %s\n' + 'This message means that the Auraxium data model must ' + 'be updated. Please ensure you are on the latest ' + 'version of the Auraxium library and report this ' + 'message to the project maintainers.', data['payload']) + return _log.debug('%s event received, dispatching...', event.event_name) self.dispatch(event) From aaa2891f6cd5afa158a189a8985354d3cf9bc6a1 Mon Sep 17 00:00:00 2001 From: Leonhard S Date: Sat, 15 May 2021 11:35:47 +0200 Subject: [PATCH 2/2] Warn about bad API payloads These currently still result in an error, but they will also log the full offending payload to facilitate troubleshooting. --- auraxium/base.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/auraxium/base.py b/auraxium/base.py index 76181a9..36875c5 100644 --- a/auraxium/base.py +++ b/auraxium/base.py @@ -79,6 +79,12 @@ def __init__(self, data: CensusData, client: RequestClient) -> None: try: self.data = self._model(**data) except pydantic.ValidationError as err: + _log.warning( + 'Encountered unsupported payload: %s\n' + 'This message means that the Auraxium data model must ' + 'be updated. Please ensure you are on the latest ' + 'version of the Auraxium library and report this ' + 'message to the project maintainers.', data) raise PayloadError( f'Unable to instantiate {self.__class__.__name__} instance ' f'from given payload: {err}', data) from err