Skip to content

Commit

Permalink
upd exc_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitrii Proskurin committed Oct 3, 2023
1 parent dd8517f commit 05f72ec
Showing 1 changed file with 6 additions and 30 deletions.
36 changes: 6 additions & 30 deletions core/utils/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,21 @@ def exc_handler(on_error_obj_method_name=None, handled_exceptions=None):
handled_exceptions = tuple(handled_exceptions) if handled_exceptions else (Exception,)

def exc_handler_decorator(funct):
if asyncio.iscoroutinefunction(funct):
@wraps(funct)
async def _wrapper(obj, *args, **kwarg):
result = None
try:
result = await funct(obj, *args, **kwarg)
except handled_exceptions:
try:
on_error = (
getattr(obj, on_error_obj_method_name)
if on_error_obj_method_name else (lambda *x, **y: None)
)
result = (
on_error(*args, **kwarg)
if not asyncio.iscoroutinefunction(on_error)
else await on_error(*args, **kwarg)
)
except Exception:
print(sys.exc_info())
return result

return _wrapper
elif inspect.isasyncgenfunction(funct):
if inspect.isasyncgenfunction(funct):
@wraps(funct)
async def _wrapper(obj, *args, **kwarg):
try:
async for yield_ in funct(obj, *args, **kwarg):
yield yield_
async for it in funct(obj, *args, **kwarg):
yield it
except handled_exceptions:
try:
on_error = (
getattr(obj, on_error_obj_method_name)
if on_error_obj_method_name else (lambda *x, **y: None)
)
if asyncio.iscoroutinefunction(on_error):
yield await on_error(*args, **kwarg)
elif inspect.isasyncgenfunction(on_error):
async for yield_ in on_error(*args, **kwarg):
yield yield_
if inspect.isasyncgenfunction(on_error):
async for it in on_error(*args, **kwarg):
yield it
else:
yield on_error(*args, **kwarg)
except Exception:
Expand Down

0 comments on commit 05f72ec

Please sign in to comment.