stouputils.decorators.handle_error module#

class LogLevels(*values)[source]#

Bases: Enum

Log level for the errors in the decorator handle_error

NONE = 0#

Do nothing

WARNING = 1#

Show as warning

WARNING_TRACEBACK = 2#

Show as warning with traceback

ERROR_TRACEBACK = 3#

Show as error with traceback

RAISE_EXCEPTION = 4#

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,
) 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,
) Callable[[Callable[..., T]], Callable[..., T]]

Decorator that handle an error with different log levels.

Parameters:
  • func (Callable[..., T] | None) – Function to decorate

  • exceptions (tuple[type[BaseException]], ...) – Exceptions to handle

  • message (str) – Message to display with the error. (e.g. “Error during something”)

  • error_log (LogLevels) –

    Log level for the errors

  • sleep_time (float) – Time to sleep after the error (e.g. 0.0 to not sleep, 1.0 to sleep for 1 second)

Examples

>>> @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