updated handler exceptions
This commit is contained in:
@@ -6,6 +6,7 @@ from api_objects.auth.token_objects import (
|
||||
OccupantToken,
|
||||
ApplicationToken,
|
||||
)
|
||||
from api_objects.errors.errorHandlers import HTTPExceptionEvyos
|
||||
|
||||
__all__ = [
|
||||
"OccupantTokenObject",
|
||||
@@ -14,4 +15,5 @@ __all__ = [
|
||||
"CompanyToken",
|
||||
"OccupantToken",
|
||||
"ApplicationToken",
|
||||
"HTTPExceptionEvyos",
|
||||
]
|
||||
|
||||
@@ -23,7 +23,7 @@ class ApplicationToken(BaseModel):
|
||||
|
||||
domain: Optional[str] = "app.evyos.com.tr"
|
||||
lang: Optional[str] = "TR"
|
||||
timezone: Optional[str] = "Europe/Istanbul"
|
||||
timezone: Optional[str] = "GMT+3"
|
||||
|
||||
user_type: int = UserType.occupant.value
|
||||
credentials: dict = None
|
||||
|
||||
@@ -1,74 +1,6 @@
|
||||
from .errors_dictionary import ErrorMessages
|
||||
|
||||
|
||||
class AlchemyError:
|
||||
ERRORS_DICT = {
|
||||
"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",
|
||||
}
|
||||
ERRORS_KEYS = {
|
||||
"delete": "DeletedRecord",
|
||||
"update": "UpdatedRecord",
|
||||
"create": "CreatedRecord",
|
||||
"list": "ListedRecords",
|
||||
"not_found": "RecordNotFound",
|
||||
"already_exist": "AlreadyExists",
|
||||
"not_deleted": "RecordNotDeleted",
|
||||
"not_updated": "RecordNotUpdated",
|
||||
"not_created": "RecordNotCreated",
|
||||
"not_listed": "RecordsNotListed",
|
||||
"not_confirm": "IsNotConfirmed",
|
||||
}
|
||||
class HTTPExceptionError:
|
||||
|
||||
def __init__(self, lang):
|
||||
self.lang = lang
|
||||
|
||||
60
api_objects/errors/errorHandlers.py
Normal file
60
api_objects/errors/errorHandlers.py
Normal file
@@ -0,0 +1,60 @@
|
||||
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 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"))
|
||||
|
||||
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
|
||||
|
||||
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(
|
||||
status_code=int(status_code),
|
||||
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},
|
||||
)
|
||||
17
api_objects/errors/errorMessages/__init__.py
Normal file
17
api_objects/errors/errorMessages/__init__.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from .baseErrorCluster import (
|
||||
BASE_ERRORS,
|
||||
BASE_ERROR_LANGUAGE,
|
||||
EXCEPTION_DICTS,
|
||||
)
|
||||
|
||||
ERRORS_LANG = {
|
||||
"tr": {
|
||||
**BASE_ERROR_LANGUAGE["tr"],
|
||||
},
|
||||
"en": {
|
||||
**BASE_ERROR_LANGUAGE["en"],
|
||||
}
|
||||
}
|
||||
ERRORS_DICT = {
|
||||
**BASE_ERRORS,
|
||||
}
|
||||
104
api_objects/errors/errorMessages/baseErrorCluster.py
Normal file
104
api_objects/errors/errorMessages/baseErrorCluster.py
Normal file
@@ -0,0 +1,104 @@
|
||||
|
||||
BASE_ERRORS = {
|
||||
"NOT_CREATED": 405,
|
||||
"NOT_DELETED": 405,
|
||||
"NOT_UPDATED": 405,
|
||||
"NOT_LISTED": 404,
|
||||
"NOT_FOUND": 404,
|
||||
"ALREADY_EXISTS": 400,
|
||||
"IS_NOT_CONFIRMED": 405,
|
||||
"NOT_AUTHORIZED": 401,
|
||||
"NOT_VALID": 406,
|
||||
"NOT_ACCEPTABLE": 406,
|
||||
"INVALID_DATA": 422,
|
||||
"UNKNOWN_ERROR": 502,
|
||||
}
|
||||
|
||||
BASE_ERROR_LANGUAGE = {
|
||||
"tr": {
|
||||
"NOT_CREATED": "Kayıt oluşturulamadı.",
|
||||
"NOT_DELETED": "Kayıt silinemedi.",
|
||||
"NOT_UPDATED": "Kayıt güncellenemedi.",
|
||||
"NOT_LISTED": "Kayıt listelenemedi.",
|
||||
"NOT_FOUND": "Kayıt bulunamadı.",
|
||||
"ALREADY_EXISTS": "Kayıt zaten mevcut.",
|
||||
"IS_NOT_CONFIRMED": "Kayıt onaylanmadı.",
|
||||
"NOT_AUTHORIZED": "Yetkisiz kullanıcı.",
|
||||
"NOT_VALID": "Gecersiz veri.",
|
||||
"NOT_ACCEPTABLE": "Gecersiz veri.",
|
||||
"INVALID_DATA": "Gecersiz veri.",
|
||||
"UNKNOWN_ERROR": "Bilinmeyen bir hata oluştu.",
|
||||
},
|
||||
"en": {
|
||||
"NOT_CREATED": "Not Created.",
|
||||
"NOT_DELETED": "Not Deleted.",
|
||||
"NOT_UPDATED": "Not Updated.",
|
||||
"NOT_LISTED": "Not Listed.",
|
||||
"NOT_FOUND": "Not Found.",
|
||||
"ALREADY_EXISTS": "Already Exists.",
|
||||
"IS_NOT_CONFIRMED": "Not Confirmed.",
|
||||
"NOT_AUTHORIZED": "Not Authorized.",
|
||||
"NOT_VALID": "Not Valid.",
|
||||
"NOT_ACCEPTABLE": "Not Acceptable.",
|
||||
"INVALID_DATA": "Invalid Data.",
|
||||
"UNKNOWN_ERROR": "Unknown Error occured.",
|
||||
},
|
||||
}
|
||||
|
||||
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",
|
||||
}
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
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
|
||||
|
||||
@classmethod
|
||||
def create(cls, requests, exceptions, response_model, status):
|
||||
return cls(requests, exceptions, response_model, 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__()},
|
||||
)
|
||||
Reference in New Issue
Block a user