diff --git a/royalnet/utils/__init__.py b/royalnet/utils/__init__.py index 88893c75..d6665d9c 100644 --- a/royalnet/utils/__init__.py +++ b/royalnet/utils/__init__.py @@ -3,7 +3,7 @@ from .sleep_until import sleep_until from .formatters import andformat, underscorize, ytdldateformat, numberemojiformat, ordinalformat from .urluuid import to_urluuid, from_urluuid from .multilock import MultiLock -from .sentry import init_sentry, sentry_exc +from .sentry import init_sentry, sentry_exc, sentry_wrap from .log import init_logging from .royaltyping import JSON @@ -20,6 +20,7 @@ __all__ = [ "MultiLock", "init_sentry", "sentry_exc", + "sentry_wrap", "init_logging", "JSON", ] diff --git a/royalnet/utils/sentry.py b/royalnet/utils/sentry.py index f854dc06..194c21c0 100644 --- a/royalnet/utils/sentry.py +++ b/royalnet/utils/sentry.py @@ -3,6 +3,7 @@ import sys import traceback from typing import * from royalnet.version import semantic +import functools try: import sentry_sdk @@ -45,3 +46,16 @@ def sentry_exc(exc: Exception, if __debug__: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) + + +def sentry_wrap(level: str = "ERROR"): + def decorator(func: Callable): + @functools.wraps(func) + def new_func(*args, **kwargs): + try: + return func(*args, **kwargs) + except Exception as exc: + sentry_exc(exc=exc, level=level) + raise + return new_func + return decorator