| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 | from typing import List, Unionfrom fastapi.encoders import jsonable_encoderfrom fastapi.requests import Requestfrom fastapi.responses import JSONResponsefrom starlette import statusfrom .exceptions import (    BadRequest,    Conflict,    DoesNotExist,    PermissionDenied,    Unauthorized,)from .value_object import ValueObject__all__ = [    "ValidationErrorResponse",    "DefaultErrorResponse",    "not_found_handler",    "conflict_handler",    "validation_error_handler",    "not_implemented_handler",    "permission_denied_handler",    "unauthorized_handler",]class ValidationErrorEntry(ValueObject):    loc: List[Union[str, int]]    msg: str    type: strclass ValidationErrorResponse(ValueObject):    detail: List[ValidationErrorEntry]class DefaultErrorResponse(ValueObject):    message: strasync def not_found_handler(request: Request, exc: DoesNotExist):    return JSONResponse(        status_code=status.HTTP_404_NOT_FOUND,        content={"message": f"Could not find {exc.name} with id={exc.id}"},    )async def conflict_handler(request: Request, exc: Conflict):    return JSONResponse(        status_code=status.HTTP_409_CONFLICT,        content={"message": str(exc)},    )async def validation_error_handler(request: Request, exc: BadRequest):    return JSONResponse(        status_code=status.HTTP_400_BAD_REQUEST,        content=jsonable_encoder({"detail": exc.errors()}),    )async def not_implemented_handler(request: Request, exc: NotImplementedError):    return JSONResponse(        status_code=status.HTTP_501_NOT_IMPLEMENTED,        content={"message": str(exc)},    )async def unauthorized_handler(request: Request, exc: Unauthorized):    return JSONResponse(        status_code=status.HTTP_401_UNAUTHORIZED,        content={"message": "Unauthorized"},    )async def permission_denied_handler(request: Request, exc: PermissionDenied):    return JSONResponse(        status_code=status.HTTP_403_FORBIDDEN,        content={"message": "Permission denied"},    )
 |