Commit 4486eb6b authored by Marius Kriegerowski's avatar Marius Kriegerowski
Browse files

Added Rule exception handling and logging

parent 243de78e
Pipeline #27093 passed with stages
in 3 minutes and 6 seconds
...@@ -19,15 +19,37 @@ ...@@ -19,15 +19,37 @@
import logging import logging
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from functools import wraps
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def log_exceptions(f):
"""Methods wrapped by this decorator add full error tracebacks to the root logger if
the log level is less/equal than 10 (DEBUG). Otherwise errors will appear as abbreviated
warning logs."""
@wraps(f)
def wrapped(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
if logger.getEffectiveLevel() <= logging.DEBUG:
target_log_level = logging.ERROR
else:
target_log_level = logging.WARNING
logger.log(level=target_log_level, msg=e)
return wrapped
class Rule(ABC): class Rule(ABC):
"""Basic rule object""" """Basic rule object"""
@log_exceptions
@abstractmethod @abstractmethod
def evaluate(self, id: int): async def evaluate(self, id: int):
"""Main function to execute a rule """Main function to execute a rule
Args: Args:
......
from rabotnik.rule import Rule, log_exceptions
class ExceptionRule(Rule):
@log_exceptions
def evaluate(self, id: int):
raise Exception("test exception")
def test_rule_exception(caplog):
rule = ExceptionRule()
rule.evaluate(0)
assert "test exception" in caplog.text
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment