Service Postgres added
This commit is contained in:
10
ErrorHandlers/ApiErrorHandlers/__init__.py
Normal file
10
ErrorHandlers/ApiErrorHandlers/__init__.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from ErrorHandlers.ApiErrorHandlers.api_exc_handler import (
|
||||
HTTPExceptionApiHandler,
|
||||
HTTPExceptionApi,
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"HTTPExceptionApiHandler",
|
||||
"HTTPExceptionApi",
|
||||
]
|
||||
45
ErrorHandlers/ApiErrorHandlers/api_exc_handler.py
Normal file
45
ErrorHandlers/ApiErrorHandlers/api_exc_handler.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
|
||||
class HTTPExceptionApi(Exception):
|
||||
|
||||
def __init__(self, error_code: str, lang: str):
|
||||
self.error_code = error_code
|
||||
self.lang = lang
|
||||
|
||||
|
||||
class HTTPExceptionApiHandler:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
**kwargs,
|
||||
):
|
||||
self.EXCEPTIONS = kwargs.get(
|
||||
"exceptions"
|
||||
) # from fastapi.exceptions import HTTPException
|
||||
self.STATUSES = kwargs.get("statuses") # from fastapi import status
|
||||
self.EXCEPTION_DICTS: Dict = kwargs.get("exceptions_dict")
|
||||
self.ERRORS_DICT: Dict = kwargs.get("errors_dict")
|
||||
self.ERRORS_LANG: Dict = kwargs.get("error_language_dict")
|
||||
self.RESPONSE_MODEL: Any = kwargs.get("response_model")
|
||||
|
||||
def retrieve_error_status_code(self, exc: HTTPExceptionApi):
|
||||
grab_status = self.ERRORS_DICT.get(str(exc.error_code).upper(), "")
|
||||
grab_status_code = self.EXCEPTION_DICTS.get(str(grab_status).upper(), "500")
|
||||
return getattr(
|
||||
self.STATUSES,
|
||||
str(grab_status_code),
|
||||
getattr(self.STATUSES, "HTTP_500_INTERNAL_SERVER_ERROR"),
|
||||
)
|
||||
|
||||
def retrieve_error_message(self, exc: HTTPExceptionApi):
|
||||
message_by_lang = self.ERRORS_LANG.get(str(exc.lang).lower(), {})
|
||||
return message_by_lang.get(str(exc.error_code).upper(), "Unknown error")
|
||||
|
||||
def handle_exception(self, request, exc: HTTPExceptionApi):
|
||||
status_code = self.retrieve_error_status_code(exc)
|
||||
error_message = self.retrieve_error_message(exc)
|
||||
return self.RESPONSE_MODEL(
|
||||
status_code=int(status_code),
|
||||
content={"message": error_message, "lang": exc.lang, "request": request},
|
||||
)
|
||||
Reference in New Issue
Block a user