Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Dynamic Exposure
rabotnik
rabotnik
Commits
4486eb6b
Commit
4486eb6b
authored
Aug 03, 2021
by
Marius Kriegerowski
Browse files
Added Rule exception handling and logging
parent
243de78e
Pipeline
#27093
passed with stages
in 3 minutes and 6 seconds
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
rabotnik/rule.py
View file @
4486eb6b
...
...
@@ -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."""
@
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
):
"""Basic rule object"""
@
log_exceptions
@
abstractmethod
def
evaluate
(
self
,
id
:
int
):
async
def
evaluate
(
self
,
id
:
int
):
"""Main function to execute a rule
Args:
...
...
tests/test_rule.py
0 → 100644
View file @
4486eb6b
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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment