error raise now locates loc
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
class HTTPExceptionError:
|
||||
|
||||
def __init__(self, lang):
|
||||
|
||||
@@ -1,46 +1,42 @@
|
||||
from typing import Any
|
||||
|
||||
|
||||
# class HTTPExceptionInstance:
|
||||
|
||||
# def __init__(self, statuses, exceptions, exceptions_dict, errors_dict, response_model, error_language_dict):
|
||||
# self.EXCEPTIONS = exceptions # from fastapi.exceptions import HTTPException
|
||||
# self.STATUSES = statuses # from fastapi import status
|
||||
# self.EXCEPTION_DICTS: dict = exceptions_dict
|
||||
# self.ERRORS_DICT: dict = errors_dict
|
||||
# self.ERRORS_LANG: dict = error_language_dict
|
||||
# self.RESPONSE_MODEL: Any = response_model
|
||||
|
||||
|
||||
class HTTPExceptionEvyos(Exception):
|
||||
|
||||
def __init__(self, error_code: str, lang: str):
|
||||
self.error_code = error_code
|
||||
self.lang = lang
|
||||
|
||||
|
||||
|
||||
class HTTPExceptionEvyosHandler:
|
||||
|
||||
def __init__(self, statuses, exceptions, exceptions_dict, errors_dict, response_model, error_language_dict):
|
||||
self.EXCEPTIONS = exceptions # from fastapi.exceptions import HTTPException
|
||||
self.STATUSES = statuses # from fastapi import status
|
||||
self.EXCEPTION_DICTS: dict = exceptions_dict
|
||||
self.ERRORS_DICT: dict = errors_dict
|
||||
self.ERRORS_LANG: dict = error_language_dict
|
||||
self.RESPONSE_MODEL: Any = response_model
|
||||
|
||||
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: HTTPExceptionEvyos):
|
||||
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"))
|
||||
return getattr(
|
||||
self.STATUSES,
|
||||
str(grab_status_code),
|
||||
getattr(self.STATUSES, "HTTP_500_INTERNAL_SERVER_ERROR"),
|
||||
)
|
||||
|
||||
def retrieve_error_message(self, exc: HTTPExceptionEvyos):
|
||||
message_by_lang = self.ERRORS_LANG.get(str(exc.lang).lower(), {})
|
||||
message_by_code = message_by_lang.get(str(exc.error_code).upper(), "Unknown error")
|
||||
return message_by_code
|
||||
return message_by_lang.get(str(exc.error_code).upper(), "Unknown error")
|
||||
|
||||
def handle_exception(self, request, exc: HTTPExceptionEvyos):
|
||||
headers = getattr(request, "headers", {})
|
||||
status_code = self.retrieve_error_status_code(exc)
|
||||
error_message = self.retrieve_error_message(exc)
|
||||
return self.RESPONSE_MODEL(
|
||||
@@ -48,13 +44,14 @@ class HTTPExceptionEvyosHandler:
|
||||
content={"message": error_message, "lang": exc.lang},
|
||||
)
|
||||
|
||||
|
||||
class HTTPExceptionAnyHandler:
|
||||
|
||||
def __init__(self, response_model):
|
||||
self.RESPONSE_MODEL: Any = response_model
|
||||
|
||||
|
||||
def any_exception_handler(self, request, exc: Exception):
|
||||
return self.RESPONSE_MODEL(
|
||||
status_code=200,
|
||||
content={"message": str(exc), "lang": None, "status_code": 417},
|
||||
content={"message": str(exc), "lang": "en"},
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@ ERRORS_LANG = {
|
||||
},
|
||||
"en": {
|
||||
**BASE_ERROR_LANGUAGE["en"],
|
||||
}
|
||||
},
|
||||
}
|
||||
ERRORS_DICT = {
|
||||
**BASE_ERRORS,
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
BASE_ERRORS = {
|
||||
"NOT_CREATED": 405,
|
||||
"NOT_DELETED": 405,
|
||||
@@ -46,59 +45,59 @@ BASE_ERROR_LANGUAGE = {
|
||||
}
|
||||
|
||||
from fastapi import status
|
||||
EXCEPTION_DICTS = {
|
||||
"100": "HTTP_100_CONTINUE",
|
||||
"101": "HTTP_101_SWITCHING_PROTOCOLS",
|
||||
"102": "HTTP_102_PROCESSING",
|
||||
"103": "HTTP_103_EARLY_HINTS",
|
||||
"200": "HTTP_200_OK",
|
||||
"201": "HTTP_201_CREATED",
|
||||
"202": "HTTP_202_ACCEPTED",
|
||||
"203": "HTTP_203_NON_AUTHORITATIVE_INFORMATION",
|
||||
"204": "HTTP_204_NO_CONTENT",
|
||||
"205": "HTTP_205_RESET_CONTENT",
|
||||
"206": "HTTP_206_PARTIAL_CONTENT",
|
||||
"207": "HTTP_207_MULTI_STATUS",
|
||||
"208": "HTTP_208_ALREADY_REPORTED",
|
||||
"226": "HTTP_226_IM_USED",
|
||||
"300": "HTTP_300_MULTIPLE_CHOICES",
|
||||
"301": "HTTP_301_MOVED_PERMANENTLY",
|
||||
"302": "HTTP_302_FOUND",
|
||||
"303": "HTTP_303_SEE_OTHER",
|
||||
"304": "HTTP_304_NOT_MODIFIED",
|
||||
"305": "HTTP_305_USE_PROXY",
|
||||
"306": "HTTP_306_RESERVED",
|
||||
"307": "HTTP_307_TEMPORARY_REDIRECT",
|
||||
"308": "HTTP_308_PERMANENT_REDIRECT",
|
||||
"400": "HTTP_400_BAD_REQUEST",
|
||||
"401": "HTTP_401_UNAUTHORIZED",
|
||||
"402": "HTTP_402_PAYMENT_REQUIRED",
|
||||
"403": "HTTP_403_FORBIDDEN",
|
||||
"404": "HTTP_404_NOT_FOUND",
|
||||
"405": "HTTP_405_METHOD_NOT_ALLOWED",
|
||||
"406": "HTTP_406_NOT_ACCEPTABLE",
|
||||
"407": "HTTP_407_PROXY_AUTHENTICATION_REQUIRED",
|
||||
"408": "HTTP_408_REQUEST_TIMEOUT",
|
||||
"409": "HTTP_409_CONFLICT",
|
||||
"410": "HTTP_410_GONE",
|
||||
"411": "HTTP_411_LENGTH_REQUIRED",
|
||||
"412": "HTTP_412_PRECONDITION_FAILED",
|
||||
"413": "HTTP_413_REQUEST_ENTITY_TOO_LARGE",
|
||||
"414": "HTTP_414_REQUEST_URI_TOO_LONG",
|
||||
"415": "HTTP_415_UNSUPPORTED_MEDIA_TYPE",
|
||||
"416": "HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE",
|
||||
"417": "HTTP_417_EXPECTATION_FAILED",
|
||||
"418": "HTTP_418_IM_A_TEAPOT",
|
||||
"421": "HTTP_421_MISDIRECTED_REQUEST",
|
||||
"422": "HTTP_422_UNPROCESSABLE_ENTITY",
|
||||
"423": "HTTP_423_LOCKED",
|
||||
"424": "HTTP_424_FAILED_DEPENDENCY",
|
||||
"426": "HTTP_426_UPGRADE_REQUIRED",
|
||||
"428": "HTTP_428_PRECONDITION_REQUIRED",
|
||||
"429": "HTTP_429_TOO_MANY_REQUESTS",
|
||||
"431": "HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE",
|
||||
"451": "HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS",
|
||||
"500": "HTTP_500_INTERNAL_SERVER_ERROR",
|
||||
"502": "HTTP_502_BAD_GATEWAY",
|
||||
}
|
||||
|
||||
EXCEPTION_DICTS = {
|
||||
"100": "HTTP_100_CONTINUE",
|
||||
"101": "HTTP_101_SWITCHING_PROTOCOLS",
|
||||
"102": "HTTP_102_PROCESSING",
|
||||
"103": "HTTP_103_EARLY_HINTS",
|
||||
"200": "HTTP_200_OK",
|
||||
"201": "HTTP_201_CREATED",
|
||||
"202": "HTTP_202_ACCEPTED",
|
||||
"203": "HTTP_203_NON_AUTHORITATIVE_INFORMATION",
|
||||
"204": "HTTP_204_NO_CONTENT",
|
||||
"205": "HTTP_205_RESET_CONTENT",
|
||||
"206": "HTTP_206_PARTIAL_CONTENT",
|
||||
"207": "HTTP_207_MULTI_STATUS",
|
||||
"208": "HTTP_208_ALREADY_REPORTED",
|
||||
"226": "HTTP_226_IM_USED",
|
||||
"300": "HTTP_300_MULTIPLE_CHOICES",
|
||||
"301": "HTTP_301_MOVED_PERMANENTLY",
|
||||
"302": "HTTP_302_FOUND",
|
||||
"303": "HTTP_303_SEE_OTHER",
|
||||
"304": "HTTP_304_NOT_MODIFIED",
|
||||
"305": "HTTP_305_USE_PROXY",
|
||||
"306": "HTTP_306_RESERVED",
|
||||
"307": "HTTP_307_TEMPORARY_REDIRECT",
|
||||
"308": "HTTP_308_PERMANENT_REDIRECT",
|
||||
"400": "HTTP_400_BAD_REQUEST",
|
||||
"401": "HTTP_401_UNAUTHORIZED",
|
||||
"402": "HTTP_402_PAYMENT_REQUIRED",
|
||||
"403": "HTTP_403_FORBIDDEN",
|
||||
"404": "HTTP_404_NOT_FOUND",
|
||||
"405": "HTTP_405_METHOD_NOT_ALLOWED",
|
||||
"406": "HTTP_406_NOT_ACCEPTABLE",
|
||||
"407": "HTTP_407_PROXY_AUTHENTICATION_REQUIRED",
|
||||
"408": "HTTP_408_REQUEST_TIMEOUT",
|
||||
"409": "HTTP_409_CONFLICT",
|
||||
"410": "HTTP_410_GONE",
|
||||
"411": "HTTP_411_LENGTH_REQUIRED",
|
||||
"412": "HTTP_412_PRECONDITION_FAILED",
|
||||
"413": "HTTP_413_REQUEST_ENTITY_TOO_LARGE",
|
||||
"414": "HTTP_414_REQUEST_URI_TOO_LONG",
|
||||
"415": "HTTP_415_UNSUPPORTED_MEDIA_TYPE",
|
||||
"416": "HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE",
|
||||
"417": "HTTP_417_EXPECTATION_FAILED",
|
||||
"418": "HTTP_418_IM_A_TEAPOT",
|
||||
"421": "HTTP_421_MISDIRECTED_REQUEST",
|
||||
"422": "HTTP_422_UNPROCESSABLE_ENTITY",
|
||||
"423": "HTTP_423_LOCKED",
|
||||
"424": "HTTP_424_FAILED_DEPENDENCY",
|
||||
"426": "HTTP_426_UPGRADE_REQUIRED",
|
||||
"428": "HTTP_428_PRECONDITION_REQUIRED",
|
||||
"429": "HTTP_429_TOO_MANY_REQUESTS",
|
||||
"431": "HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE",
|
||||
"451": "HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS",
|
||||
"500": "HTTP_500_INTERNAL_SERVER_ERROR",
|
||||
"502": "HTTP_502_BAD_GATEWAY",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user