Initial commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
from .model import *
|
||||
@@ -0,0 +1,81 @@
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class BaseError(BaseModel):
|
||||
message: str = Field(..., description="Error message or description")
|
||||
|
||||
|
||||
class BaseIdentifiedError(BaseError):
|
||||
identifier: str = Field(..., description="Unique identifier which this error references to")
|
||||
|
||||
|
||||
class NotFoundError(BaseIdentifiedError):
|
||||
"""The entity does not exist"""
|
||||
pass
|
||||
|
||||
|
||||
class AlreadyExistsError(BaseIdentifiedError):
|
||||
"""An entity being created already exists"""
|
||||
pass
|
||||
|
||||
|
||||
class ModifiedCountException(Exception):
|
||||
message: str = Field(..., description="Error message or description")
|
||||
|
||||
def __init__(self, identifier: any, message: str = 'Data not update', **data: Any):
|
||||
super().__init__()
|
||||
if identifier:
|
||||
self.message = f'data with id {identifier} not update'
|
||||
else:
|
||||
self.message = message
|
||||
|
||||
|
||||
class ValidationException(Exception):
|
||||
message: str = Field(..., description="Error message or description")
|
||||
|
||||
def __init__(self, message: str = 'validation exception', **data: Any):
|
||||
super().__init__()
|
||||
self.message = f'{message} with {data}' if data else message
|
||||
|
||||
|
||||
class RecordNotFoundException(Exception):
|
||||
message: str = Field(..., description="Error message or description")
|
||||
|
||||
def __init__(self, identifier: any = None, message: str = 'record not found', **data: Any):
|
||||
super().__init__()
|
||||
if identifier:
|
||||
self.message = f'data with id {identifier} not found'
|
||||
else:
|
||||
self.message = message
|
||||
|
||||
|
||||
class SoftDeletedException(Exception):
|
||||
message: str = Field(..., description="Error message or description")
|
||||
|
||||
def __init__(self, identifier: any, message: str = 'Data not soft deleted', **data: Any):
|
||||
super().__init__()
|
||||
if identifier:
|
||||
self.message = f'data with id {identifier} not soft deleted'
|
||||
else:
|
||||
self.message = message
|
||||
|
||||
|
||||
class NotFoundException(Exception):
|
||||
message: str = Field(..., description="Error message or description")
|
||||
|
||||
def __init__(self, identifier: any, message: str = 'Data not found', **data: Any):
|
||||
super().__init__()
|
||||
if identifier:
|
||||
self.message = f'data with id {identifier} not found'
|
||||
else:
|
||||
self.message = message
|
||||
|
||||
|
||||
class BadRequestException(Exception):
|
||||
message: str = Field(..., description="Error message or description")
|
||||
|
||||
def __init__(self, message: str = 'Data bad data', **data: Any):
|
||||
super().__init__()
|
||||
self.message = message
|
||||
@@ -0,0 +1,80 @@
|
||||
from typing import Type
|
||||
|
||||
from fastapi import status as statuscode
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
# # Package # #
|
||||
from .errors import *
|
||||
|
||||
|
||||
class BaseAPIException(Exception):
|
||||
"""Base error for custom API exceptions"""
|
||||
message = "Generic error"
|
||||
code = statuscode.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
model = BaseError
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
kwargs.setdefault("message", self.message)
|
||||
self.message = kwargs["message"]
|
||||
self.data = self.model(**kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return self.message
|
||||
|
||||
def response(self):
|
||||
return JSONResponse(
|
||||
content=self.data.dict(),
|
||||
status_code=self.code
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def response_model(cls):
|
||||
return {cls.code: {"model": cls.model}}
|
||||
|
||||
|
||||
class BaseIdentifiedException(BaseAPIException):
|
||||
"""Base error for exceptions related with entities, uniquely identified"""
|
||||
message = "Entity error"
|
||||
code = statuscode.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
model = BaseIdentifiedError
|
||||
|
||||
def __init__(self, identifier, **kwargs):
|
||||
super().__init__(identifier=identifier, **kwargs)
|
||||
|
||||
|
||||
class NotFoundException(BaseIdentifiedException):
|
||||
"""Base error for exceptions raised because an entity does not exist"""
|
||||
message = "The entity does not exist"
|
||||
code = statuscode.HTTP_404_NOT_FOUND
|
||||
model = NotFoundError
|
||||
|
||||
|
||||
class AlreadyExistsException(BaseIdentifiedException):
|
||||
"""Base error for exceptions raised because an entity already exists"""
|
||||
message = "The entity already exists"
|
||||
code = statuscode.HTTP_409_CONFLICT
|
||||
model = AlreadyExistsError
|
||||
|
||||
|
||||
class ColorConstNotFoundException(NotFoundException):
|
||||
"""Error raised when a person does not exist"""
|
||||
message = "The const does not exist"
|
||||
|
||||
|
||||
class ConstNotFoundException(NotFoundException):
|
||||
"""Error raised when a person does not exist"""
|
||||
message = "The const does not exist"
|
||||
|
||||
|
||||
class PersonAlreadyExistsException(AlreadyExistsException):
|
||||
"""Error raised when a person already exists"""
|
||||
message = "The const already exists"
|
||||
|
||||
|
||||
def get_exception_responses(*args: Type[BaseAPIException]) -> dict:
|
||||
"""Given BaseAPIException classes, return a dict of responses used on FastAPI endpoint definition, with the format:
|
||||
{statuscode: schema, statuscode: schema, ...}"""
|
||||
responses = dict()
|
||||
for cls in args:
|
||||
responses.update(cls.response_model())
|
||||
return responses
|
||||
Reference in New Issue
Block a user