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 @@
import logging
from abc import ABC, abstractmethod
from functools import wraps
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."""
def wrapped(*args, **kwargs):
return f(*args, **kwargs)
except Exception as e:
if logger.getEffectiveLevel() <= logging.DEBUG:
target_log_level = logging.ERROR
target_log_level = logging.WARNING
logger.log(level=target_log_level, msg=e)
return wrapped
class Rule(ABC):
"""Basic rule object"""
def evaluate(self, id: int):
async def evaluate(self, id: int):
"""Main function to execute a rule
from rabotnik.rule import Rule, log_exceptions
class ExceptionRule(Rule):
def evaluate(self, id: int):
raise Exception("test exception")
def test_rule_exception(caplog):
rule = ExceptionRule()
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