Complete the code to import the class used for validation errors in FastAPI.
from fastapi.exceptions import [1]
The RequestValidationError class is used by FastAPI to represent validation errors in requests.
Complete the code to create a FastAPI exception handler for validation errors.
@app.exception_handler([1]) async def validation_exception_handler(request, exc): return JSONResponse(status_code=422, content={"detail": exc.errors()})
The @app.exception_handler decorator needs the RequestValidationError to catch validation errors.
Fix the error in the handler to return the validation errors correctly as JSON.
from fastapi.responses import JSONResponse @app.exception_handler(RequestValidationError) async def validation_exception_handler(request, exc): return JSONResponse(status_code=422, content=[1])
The response content must be a dictionary with a detail key holding the list of errors.
Fill both blanks to import and use the correct JSON response class and error class for validation errors.
from fastapi.responses import [1] from fastapi.exceptions import [2] @app.exception_handler(RequestValidationError) async def validation_exception_handler(request, exc): return JSONResponse(status_code=422, content={"detail": exc.errors()})
Use JSONResponse to send JSON data and RequestValidationError to catch validation errors.
Fill all three blanks to define a custom validation error handler that logs the error, returns JSON with status 422, and uses the correct imports.
from fastapi.exceptions import [1] from fastapi.responses import [2] import logging logger = logging.getLogger(__name__) @app.exception_handler([3]) async def validation_exception_handler(request, exc): logger.error(f"Validation error: {exc}") return JSONResponse(status_code=422, content={"detail": exc.errors()})
Import RequestValidationError from fastapi.exceptions, JSONResponse from fastapi.responses, and use RequestValidationError in the handler decorator.