Skip to content

Commit

Permalink
Add exception handling to EventBase.emit (#860)
Browse files Browse the repository at this point in the history
* Add exception handling to EventBase.emit

* Switch to logger.exception
  • Loading branch information
raman325 authored Jan 5, 2024
1 parent 0e38b9a commit 0a1c785
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions test/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ def test_once():
mock.emit("test-event", 1)
mock.emit("test-event", 2)
assert len(calls) == 1


def test_exception_on_emit(caplog):
"""Test exception on emit gets handled."""
mock = event.EventBase()
mock.on("test-event", lambda _: 1 / 0)
mock.emit("test-event", 1)
assert "Error handling event: test-event" in caplog.text
5 changes: 4 additions & 1 deletion zwave_js_server/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def event_listener(data: dict) -> None:
def emit(self, event_name: str, data: dict) -> None:
"""Run all callbacks for an event."""
for listener in self._listeners.get(event_name, []).copy():
listener(data)
try:
listener(data)
except Exception: # pylint: disable=broad-exception-caught
LOGGER.exception("Error handling event: %s", event_name)

def _handle_event_protocol(self, event: Event) -> None:
"""Process an event based on event protocol."""
Expand Down

0 comments on commit 0a1c785

Please sign in to comment.