Advertisement
Korotkodul

A. Совместимость с API

Oct 20th, 2024 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. from functools import wraps
  2. from typing import Callable, TypeVar
  3. T = TypeVar("T")
  4.  
  5. def api_computable_exceptions(
  6.     exception_mapping: dict[type[Exception], type[Exception]]
  7. ) -> Callable[[T], T]:
  8.     def take_in_func(func):
  9.         @wraps(func)
  10.         def wrapper(*args, **kwargs):
  11.             try:
  12.                 return func(*args, **kwargs)
  13.             except Exception as exp:
  14.                 if type(exp) in exception_mapping:
  15.                     raise exception_mapping[type(exp)]
  16.                 else:
  17.                     raise exp
  18.         return wrapper
  19.     return take_in_func
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement