diff --git a/cmk/base/events.py b/cmk/base/events.py index 49e7ce7ffbb..78a0f01ff8a 100644 --- a/cmk/base/events.py +++ b/cmk/base/events.py @@ -12,6 +12,7 @@ import socket import sys import time +import traceback from collections.abc import Callable, Iterable, Mapping from typing import Any, cast from urllib.parse import quote, urlencode @@ -485,7 +486,10 @@ def apply_matchers( all_timeperiods: TimeperiodSpecs, ) -> str | None: for matcher in matchers: - result = matcher(rule, context, analyse, all_timeperiods) + try: + result = matcher(rule, context, analyse, all_timeperiods) + except Exception: + return f"Error in matcher: {traceback.format_exc()}" if result is not None: return result return None diff --git a/tests/unit/cmk/base/test_events.py b/tests/unit/cmk/base/test_events.py index 4f496af1965..650898facb4 100644 --- a/tests/unit/cmk/base/test_events.py +++ b/tests/unit/cmk/base/test_events.py @@ -24,6 +24,7 @@ from cmk.base.events import ( _update_enriched_context_from_notify_host_file, add_to_event_context, + apply_matchers, convert_proxy_params, event_match_hosttags, raw_context_from_string, @@ -484,3 +485,32 @@ def test_convert_proxy_params( ) -> None: params_dict = convert_proxy_params(params) assert params_dict["proxy_url"] == expected + + +def test_apply_matchers_catches_errors() -> None: + rule = EventRule( + rule_id=NotificationRuleID("1"), + allow_disable=False, + contact_all=False, + contact_all_with_email=False, + contact_object=False, + description="Test rule", + disabled=False, + notify_plugin=("mail", NotificationParameterID("parameter_id")), + ) + + def raise_error() -> None: + raise ValueError("This is a test") + + why_not = apply_matchers( + [ + lambda *args, **kw: raise_error(), + ], + rule, + context={}, + analyse=False, + all_timeperiods={}, + ) + + assert isinstance(why_not, str) + assert "ValueError: This is a test" in why_not