45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
from json import loads
|
|
|
|
|
|
class ErrorMessages:
|
|
__messages__ = {}
|
|
|
|
@classmethod
|
|
def get_message(cls, message_key, lang):
|
|
return cls.__messages__[lang][message_key]
|
|
|
|
|
|
|
|
class ErrorHandlers:
|
|
def __init__(self, requests, exceptions, response_model, status):
|
|
self.requests = requests # from fastapi.requests import Request
|
|
self.exceptions = exceptions # from fastapi.exceptions import HTTPException
|
|
self.response_model = response_model # from fastapi.responses import JSONResponse
|
|
self.status = status # from fastapi import status
|
|
|
|
def exception_handler_http(self, request, exc):
|
|
exc_detail = getattr(exc, "detail", None)
|
|
try:
|
|
detail = loads(str(exc_detail))
|
|
return self.response_model(
|
|
status_code=exc.status_code,
|
|
content={
|
|
"Data": detail.get("data", {}),
|
|
"Error": detail.get("error_case", "UNKNOWN"),
|
|
"Message": detail.get(
|
|
"message", "An error occurred while processing the request"
|
|
),
|
|
},
|
|
)
|
|
except Exception as e:
|
|
return self.response_model(
|
|
status_code=exc.status_code,
|
|
content={"Error": str(exc_detail), "Message": f"{str(e)}", "Data": {}},
|
|
)
|
|
|
|
def exception_handler_exception(self, request, exc):
|
|
return self.response_model(
|
|
status_code=self.status.HTTP_417_EXPECTATION_FAILED,
|
|
content={"message": exc.__str__()},
|
|
)
|