Open JesusFragoso opened 2 years ago
The code for the decorator is the following:
class ResponseHandler:
@staticmethod
def custom_response(main_function: Callable) -> Callable:
@wraps(main_function)
async def handler_function(*args: Tuple, **kwargs: Dict) -> Dict:
try:
result, code = await main_function(*args, **kwargs)
except tuple(SERVICE_EXCEPTIONS) as exception:
ResponseHandler._modify_status_code(kwargs["response"], exception.code.value)
return {
"exception_message": exception.message,
"code": exception.code,
}
except IntegrityError as exception:
ResponseHandler._modify_status_code(kwargs["response"], 400)
return {
"exception_message": exception._message(),
"code": 400,
}
return {"data": result, "status_code": code}
return handler_function
@staticmethod
def _modify_status_code(response: Response, status_code: int) -> NoReturn:
response.status_code = status_code
@staticmethod
def class_decorator(my_class):
def wrapper(*args, **kwargs):
members = list(filter(lambda member: "__" not in member[0], inspect.getmembers(my_class)))
for name, method in members:
setattr(my_class, name, ResponseHandler.custom_response(method))
return my_class()
return wrapper
I'm trying to do something like the following:
The
class_decorator
decorator adds custom response for each one of the requests, but when I try to execute one of the services, then this error appears: