stouputils.decorators.error_handling module#

class LogLevels(*values)[source]#

Bases: Enum

Log level for the errors in the decorator handle_error

NONE = 0[source]#

Do nothing

WARNING = 1[source]#

Show as warning

WARNING_TRACEBACK = 2[source]#

Show as warning with traceback

ERROR_TRACEBACK = 3[source]#

Show as error with traceback

RAISE_EXCEPTION = 4[source]#

Raise exception

handle_error(
func: Callable[..., T],
*,
exceptions: tuple[type[BaseException], ...] | type[BaseException] = (Exception,),
message: str = '',
error_log: LogLevels = LogLevels.WARNING_TRACEBACK,
sleep_time: float = 0.0,
callback: Callable[[BaseException], None] | None = None,
) → Callable[..., T][source]#
handle_error(
func: None = None,
*,
exceptions: tuple[type[BaseException], ...] | type[BaseException] = (Exception,),
message: str = '',
error_log: LogLevels = LogLevels.WARNING_TRACEBACK,
sleep_time: float = 0.0,
callback: Callable[[BaseException], None] | None = None,
) → Callable[[Callable[..., T]], Callable[..., T]]

Decorator that handle an error with different log levels.

Parameters:
>>> @handle_error
... def might_fail():
...     raise ValueError("Let's fail")
>>> @handle_error(error_log=LogLevels.WARNING)
... def test():
...     raise ValueError("Let's fail")
>>> # test()    # [WARNING HH:MM:SS] Error during test: (ValueError) Let's fail