From d9f4eebb2b19dac4730665e62435c7f5519a4f5e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 11 Apr 2020 00:56:05 +0200 Subject: [PATCH] Add sentry_async_wrap decorator --- royalnet/utils/__init__.py | 3 ++- royalnet/utils/sentry.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/royalnet/utils/__init__.py b/royalnet/utils/__init__.py index d6665d9c..f1e7b17f 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, sentry_wrap +from .sentry import init_sentry, sentry_exc, sentry_wrap, sentry_async_wrap from .log import init_logging from .royaltyping import JSON @@ -21,6 +21,7 @@ __all__ = [ "init_sentry", "sentry_exc", "sentry_wrap", + "sentry_async_wrap", "init_logging", "JSON", ] diff --git a/royalnet/utils/sentry.py b/royalnet/utils/sentry.py index 194c21c0..1d9b9b02 100644 --- a/royalnet/utils/sentry.py +++ b/royalnet/utils/sentry.py @@ -59,3 +59,16 @@ def sentry_wrap(level: str = "ERROR"): raise return new_func return decorator + + +def sentry_async_wrap(level: str = "ERROR"): + def decorator(func: Callable): + @functools.wraps(func) + async def new_func(*args, **kwargs): + try: + return await func(*args, **kwargs) + except Exception as exc: + sentry_exc(exc=exc, level=level) + raise + return new_func + return decorator