diff --git a/.changes/unreleased/Fixes-20241010-164116.yaml b/.changes/unreleased/Fixes-20241010-164116.yaml new file mode 100644 index 0000000..60dbc5d --- /dev/null +++ b/.changes/unreleased/Fixes-20241010-164116.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Suppress logging event dictionary parsing exceptions +time: 2024-10-10T16:41:16.06107-04:00 +custom: + Author: gshank + Issue: "202" diff --git a/dbt_common/events/base_types.py b/dbt_common/events/base_types.py index 78b0368..781b2a0 100644 --- a/dbt_common/events/base_types.py +++ b/dbt_common/events/base_types.py @@ -2,7 +2,6 @@ import os import threading from dbt_common.events import types_pb2 -import sys from google.protobuf.json_format import ParseDict, MessageToDict, MessageToJson from google.protobuf.message import Message from dbt_common.events.helpers import get_json_string_utcnow @@ -65,14 +64,14 @@ def __init__(self, *args, **kwargs) -> None: kwargs["msg"] = str(kwargs["msg"]) try: self.pb_msg = ParseDict(kwargs, msg_cls()) - except Exception: + except Exception as exc: # Imports need to be here to avoid circular imports from dbt_common.events.types import Note from dbt_common.events.functions import fire_event - error_msg = f"[{class_name}]: Unable to parse dict {kwargs}" + error_msg = f"[{class_name}]: Unable to parse logging event dictionary. {exc}. Dictionary: {kwargs}" # If we're testing throw an error so that we notice failures - if "pytest" in sys.modules: + if os.getenv("PYTEST_CURRENT_TEST"): raise Exception(error_msg) else: fire_event(Note(msg=error_msg), level=EventLevel.WARN) diff --git a/tests/unit/test_events.py b/tests/unit/test_events.py index f89a2b2..06cb7c4 100644 --- a/tests/unit/test_events.py +++ b/tests/unit/test_events.py @@ -1,4 +1,5 @@ import re +import os import pytest @@ -127,11 +128,12 @@ def test_bad_serialization(): that bad serializations are properly handled, the best we can do is test that the exception handling path is used. """ - with pytest.raises(Exception) as excinfo: types.Note(param_event_doesnt_have="This should break") + assert 'has no field named "param_event_doesnt_have" at "Note"' in str(excinfo.value) - assert ( - str(excinfo.value) - == "[Note]: Unable to parse dict {'param_event_doesnt_have': 'This should break'}" - ) + # With this unset, it shouldn't throw an exception + saved = os.environ["PYTEST_CURRENT_TEST"] + del os.environ["PYTEST_CURRENT_TEST"] + types.Note(param_event_doesnt_have="This should not break") + os.environ["PYTEST_CURRENT_TEST"] = saved