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
report_error(
error_log: LogLevels,
heading: str,
exception: BaseException,
sleep_time: float,
callback: Callable[[BaseException], None] | None,
) → None[source]#

React to an exception handle_error() caught at a level that does not raise, from inside its except block.

Parameters:
  • heading – Opening of the message, naming where the exception happened.

  • sleep_time – Seconds to sleep afterwards, skipped at ERROR_TRACEBACK since its prompt already waited.