stouputils.decorators.error_handling module#
- class LogLevels(*values)[source]#
Bases:
EnumLog level for the errors in the decorator handle_error
- 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,
- 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,
Decorator that handle an error with different log levels.
- Parameters:
func – Function to decorate
exceptions – Exceptions to handle
message – Message to display with the error (e.g. “Error during something”)
error_log –
Log level for the errors
LogLevels.NONE- NoneLogLevels.WARNING- Show as warningLogLevels.WARNING_TRACEBACK- Show as warning with tracebackLogLevels.ERROR_TRACEBACK- Show as error with tracebackLogLevels.RAISE_EXCEPTION- Raise exception
sleep_time – Seconds to sleep after the error, 0.0 not to sleep
callback – Called with the exception, None for no callback
>>> @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