Skip to content

Commit

Permalink
Re-enable suppression of logging event dictionary parsing errors (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank authored Oct 10, 2024
1 parent 8f2b1d5 commit 1e93d0d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20241010-164116.yaml
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 3 additions & 4 deletions dbt_common/events/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/test_events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import os

import pytest

Expand Down Expand Up @@ -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

0 comments on commit 1e93d0d

Please sign in to comment.