mirror of
https://github.com/Garmelon/PFERD.git
synced 2026-04-13 07:55:05 +02:00
Swallow and print errors instead of crashing
This commit is contained in:
parent
a4c518bf4c
commit
afa48c2d2d
2 changed files with 31 additions and 0 deletions
28
PFERD/errors.py
Normal file
28
PFERD/errors.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
"""
|
||||
An error logging decorator.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Any, Callable
|
||||
|
||||
from .logging import FatalException, PrettyLogger
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
PRETTY = PrettyLogger(LOGGER)
|
||||
|
||||
|
||||
def swallow_and_print_errors(function: Callable) -> Callable:
|
||||
"""
|
||||
Decorates a function, swallows all errors, logs them and returns none if one occurred.
|
||||
"""
|
||||
def inner(*args: Any, **kwargs: Any) -> Any:
|
||||
# pylint: disable=broad-except
|
||||
try:
|
||||
return function(*args, **kwargs)
|
||||
except FatalException as error:
|
||||
PRETTY.error(str(error))
|
||||
return None
|
||||
except Exception as error:
|
||||
Console().print_exception()
|
||||
return None
|
||||
return inner
|
||||
Loading…
Add table
Add a link
Reference in a new issue