FastAPI - Error Handling
Given this FastAPI code snippet, what will be the response when a
ValueError is raised?
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel
app = FastAPI()
class ErrorResponse(BaseModel):
detail: str
@app.exception_handler(ValueError)
async def value_error_handler(request: Request, exc: ValueError):
return JSONResponse(
status_code=400,
content=ErrorResponse(detail=str(exc)).dict()
)
@app.get("/test")
async def test():
raise ValueError("Invalid input")