Skip to content

Commit

Permalink
notifications: catch errors in apply_matchers
Browse files Browse the repository at this point in the history
Change-Id: I128d8a630d35f3c289fe2402c9173d2acd9cc529
JIRA-Ref: SUP-21074
  • Loading branch information
ottermata committed Jan 9, 2025
1 parent 7e8f5ac commit 1fc4b59
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmk/base/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/cmk/base/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit 1fc4b59

Please sign in to comment.