| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 | # (c) Nelen & Schuurmansfrom typing import Listfrom typing import Unionfrom fastapi.encoders import jsonable_encoderfrom fastapi.requests import Requestfrom fastapi.responses import JSONResponsefrom starlette import statusfrom clean_python import BadRequestfrom clean_python import Conflictfrom clean_python import DoesNotExistfrom clean_python import PermissionDeniedfrom clean_python import Unauthorizedfrom clean_python 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"},        headers={"WWW-Authenticate": "Bearer"},    )async def permission_denied_handler(request: Request, exc: PermissionDenied):    return JSONResponse(        status_code=status.HTTP_403_FORBIDDEN,        content={"message": "Permission denied", "detail": str(exc)},    )
 |