new api service and logic implemented

This commit is contained in:
2025-01-23 22:27:25 +03:00
parent d91ecda9df
commit 32022ca521
245 changed files with 28004 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
from typing import Any, Dict, Union, Awaitable
from fastapi import Request, WebSocket
from fastapi.responses import Response
from LanguageModels.Errors.merge_all_error_languages import MergedErrorLanguageModels
from ErrorHandlers.Exceptions.api_exc import HTTPExceptionApi
from ErrorHandlers.bases import BaseErrorModelClass
class HTTPExceptionApiHandler:
def __init__(
self,
response_model: Any,
):
self.RESPONSE_MODEL: Any = response_model
@staticmethod
def retrieve_error_status_code(exc: HTTPExceptionApi) -> int:
error_by_codes = BaseErrorModelClass.retrieve_error_by_codes()
grab_status_code = error_by_codes.get(str(exc.error_code).upper(), 500)
return int(grab_status_code)
@staticmethod
def retrieve_error_message(exc: HTTPExceptionApi, error_languages) -> str:
from ErrorHandlers import DEFAULT_ERROR
return error_languages.get(str(exc.error_code).upper(), DEFAULT_ERROR)
async def handle_exception(
self, request: Union[Request, WebSocket], exc: Exception
) -> Union[Response, Awaitable[None]]:
request_string = (
str(request.url) if isinstance(request, Request) else request.url.path
)
if isinstance(exc, HTTPExceptionApi):
error_languages = MergedErrorLanguageModels.get_language_models(
language=exc.lang
)
status_code = self.retrieve_error_status_code(exc)
error_message = self.retrieve_error_message(exc, error_languages)
return self.RESPONSE_MODEL(
status_code=int(status_code),
content={
"message": error_message,
"lang": exc.lang,
"request": request_string,
"loc": exc.loc,
},
)
return self.RESPONSE_MODEL(
status_code=500,
content={
"message": "Internal Server Error",
"lang": "def",
"request": request_string,
"loc": exc.loc,
},
) # Handle other exceptions with a generic 500 error