diff --git a/test/test_event.py b/test/test_event.py index 0c4696ae4..47157920b 100644 --- a/test/test_event.py +++ b/test/test_event.py @@ -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 diff --git a/zwave_js_server/event.py b/zwave_js_server/event.py index 9d88d1c30..9c1d9a8c5 100644 --- a/zwave_js_server/event.py +++ b/zwave_js_server/event.py @@ -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."""