@safe¶
Wraps a function in a try/except, with logging, error callbacks, and optional re-raising.
from pedros import safe, get_logger
logger = get_logger("safe_demo")
@safe(re_raise=False)
def risky_operation():
logger.info("Executing risky_operation...")
raise ValueError("Something went wrong")
risky_operation() # Logs the error (ERROR level) but doesn't crash
Options¶
catch: exception type or tuple of types to catch (default:Exception)log_level: level used when logging a caught exception, or"NONE"to suppress logging entirely (default:"ERROR")re_raise: re-raise the exception after handling it (default:True)on_error: callback invoked with the caught exceptionon_finally: callback invoked in afinallyblock, regardless of outcome
@safe always logs through the pedros logger, see the
logging guide for how to view or
silence it.