new api service and logic implemented
This commit is contained in:
59
ApiLayers/ErrorHandlers/ErrorHandlers/api_exc_handler.py
Normal file
59
ApiLayers/ErrorHandlers/ErrorHandlers/api_exc_handler.py
Normal 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
|
||||
7
ApiLayers/ErrorHandlers/Exceptions/api_exc.py
Normal file
7
ApiLayers/ErrorHandlers/Exceptions/api_exc.py
Normal file
@@ -0,0 +1,7 @@
|
||||
class HTTPExceptionApi(Exception):
|
||||
|
||||
def __init__(self, error_code: str, lang: str, loc: str = "", sys_msg: str = ""):
|
||||
self.error_code = error_code
|
||||
self.lang = lang
|
||||
self.loc = loc
|
||||
self.sys_msg = sys_msg
|
||||
10
ApiLayers/ErrorHandlers/__init__.py
Normal file
10
ApiLayers/ErrorHandlers/__init__.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from ErrorHandlers.ErrorHandlers.api_exc_handler import (
|
||||
HTTPExceptionApiHandler,
|
||||
)
|
||||
from ErrorHandlers.Exceptions.api_exc import (
|
||||
HTTPExceptionApi,
|
||||
)
|
||||
|
||||
DEFAULT_ERROR = "UNKNOWN_ERROR"
|
||||
|
||||
__all__ = ["HTTPExceptionApiHandler", "HTTPExceptionApi", "DEFAULT_ERROR"]
|
||||
13
ApiLayers/ErrorHandlers/base.py
Normal file
13
ApiLayers/ErrorHandlers/base.py
Normal file
@@ -0,0 +1,13 @@
|
||||
class BaseError:
|
||||
NOT_CREATED: int = 405
|
||||
NOT_DELETED: int = 405
|
||||
NOT_UPDATED: int = 405
|
||||
NOT_LISTED: int = 404
|
||||
NOT_FOUND: int = 404
|
||||
ALREADY_EXISTS: int = 400
|
||||
IS_NOT_CONFIRMED: int = 405
|
||||
NOT_AUTHORIZED: int = 401
|
||||
NOT_VALID: int = 406
|
||||
NOT_ACCEPTABLE: int = 406
|
||||
INVALID_DATA: int = 422
|
||||
UNKNOWN_ERROR: int = 502
|
||||
18
ApiLayers/ErrorHandlers/bases.py
Normal file
18
ApiLayers/ErrorHandlers/bases.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from ErrorHandlers.base import BaseError
|
||||
from ErrorHandlers.statuses import Statuses
|
||||
|
||||
|
||||
class BaseErrorModelClass:
|
||||
list_of_statuses = [Statuses, BaseError]
|
||||
|
||||
@classmethod
|
||||
def retrieve_error_by_codes(cls):
|
||||
language_model_status = {}
|
||||
for list_of_language in cls.list_of_statuses:
|
||||
clean_dict = {
|
||||
key: value
|
||||
for key, value in list_of_language.__dict__.items()
|
||||
if "__" not in str(key)[0:3]
|
||||
}
|
||||
language_model_status.update(clean_dict)
|
||||
return language_model_status
|
||||
58
ApiLayers/ErrorHandlers/statuses.py
Normal file
58
ApiLayers/ErrorHandlers/statuses.py
Normal file
@@ -0,0 +1,58 @@
|
||||
class Statuses:
|
||||
HTTP_100_CONTINUE = 100
|
||||
HTTP_101_SWITCHING_PROTOCOLS = 101
|
||||
HTTP_102_PROCESSING = 102
|
||||
HTTP_103_EARLY_HINTS = 103
|
||||
HTTP_200_OK = 200
|
||||
HTTP_201_CREATED = 201
|
||||
HTTP_202_ACCEPTED = 202
|
||||
HTTP_203_NON_AUTHORITATIVE_INFORMATION = 203
|
||||
HTTP_204_NO_CONTENT = 204
|
||||
HTTP_205_RESET_CONTENT = 205
|
||||
HTTP_206_PARTIAL_CONTENT = 206
|
||||
HTTP_207_MULTI_STATUS = 207
|
||||
HTTP_208_ALREADY_REPORTED = 208
|
||||
HTTP_226_IM_USED = 226
|
||||
HTTP_300_MULTIPLE_CHOICES = 300
|
||||
HTTP_301_MOVED_PERMANENTLY = 301
|
||||
HTTP_302_FOUND = 302
|
||||
HTTP_303_SEE_OTHER = 303
|
||||
HTTP_304_NOT_MODIFIED = 304
|
||||
HTTP_305_USE_PROXY = 305
|
||||
HTTP_306_RESERVED = 306
|
||||
HTTP_307_TEMPORARY_REDIRECT = 307
|
||||
HTTP_308_PERMANENT_REDIRECT = 308
|
||||
HTTP_400_BAD_REQUEST = 400
|
||||
HTTP_401_UNAUTHORIZED = 401
|
||||
HTTP_402_PAYMENT_REQUIRED = 402
|
||||
HTTP_403_FORBIDDEN = 403
|
||||
HTTP_404_NOT_FOUND = 404
|
||||
HTTP_405_METHOD_NOT_ALLOWED = 405
|
||||
HTTP_406_NOT_ACCEPTABLE = 406
|
||||
HTTP_407_PROXY_AUTHENTICATION_REQUIRED = 407
|
||||
HTTP_408_REQUEST_TIMEOUT = 408
|
||||
HTTP_409_CONFLICT = 409
|
||||
HTTP_410_GONE = 410
|
||||
HTTP_411_LENGTH_REQUIRED = 411
|
||||
HTTP_412_PRECONDITION_FAILED = 412
|
||||
HTTP_413_REQUEST_ENTITY_TOO_LARGE = 413
|
||||
HTTP_414_REQUEST_URI_TOO_LONG = 414
|
||||
HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415
|
||||
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE = 416
|
||||
HTTP_417_EXPECTATION_FAILED = 417
|
||||
HTTP_418_IM_A_TEAPOT = 418
|
||||
HTTP_421_MISDIRECTED_REQUEST = 421
|
||||
HTTP_422_UNPROCESSABLE_ENTITY = 422
|
||||
HTTP_423_LOCKED = 423
|
||||
HTTP_424_FAILED_DEPENDENCY = 424
|
||||
HTTP_426_UPGRADE_REQUIRED = 426
|
||||
HTTP_428_PRECONDITION_REQUIRED = 428
|
||||
HTTP_429_TOO_MANY_REQUESTS = 429
|
||||
HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 431
|
||||
HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS = 451
|
||||
HTTP_500_INTERNAL_SERVER_ERROR = 500
|
||||
HTTP_502_BAD_GATEWAY = 502
|
||||
|
||||
@classmethod
|
||||
def retrieve_error_by_code(cls, error_code: str):
|
||||
return getattr(cls, error_code, 502)
|
||||
Reference in New Issue
Block a user