0
0
FastAPIframework~10 mins

Validation error responses in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the class used for validation errors in FastAPI.

FastAPI
from fastapi.exceptions import [1]
Drag options to blanks, or click blank then click option'
ARequestValidationError
BHTTPException
CValidationError
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTPException instead of RequestValidationError
Using ValidationError from Pydantic directly
2fill in blank
medium

Complete the code to create a FastAPI exception handler for validation errors.

FastAPI
@app.exception_handler([1])
async def validation_exception_handler(request, exc):
    return JSONResponse(status_code=422, content={"detail": exc.errors()})
Drag options to blanks, or click blank then click option'
AHTTPException
BRequestValidationError
CValidationError
DValueError
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTPException instead of RequestValidationError
Using ValidationError from Pydantic
3fill in blank
hard

Fix the error in the handler to return the validation errors correctly as JSON.

FastAPI
from fastapi.responses import JSONResponse

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    return JSONResponse(status_code=422, content=[1])
Drag options to blanks, or click blank then click option'
A{"errors": exc.errors()}
B{"detail": exc.json()}
C{"detail": exc.errors()}
Dexc.errors()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning exc.errors() directly without wrapping in a dict
Using exc.json() which returns a string, not a dict
4fill in blank
hard

Fill both blanks to import and use the correct JSON response class and error class for validation errors.

FastAPI
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()})
Drag options to blanks, or click blank then click option'
AJSONResponse
BRequestValidationError
CHTTPException
DPlainTextResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using PlainTextResponse instead of JSONResponse
Using HTTPException instead of RequestValidationError
5fill in blank
hard

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.

FastAPI
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()})
Drag options to blanks, or click blank then click option'
ARequestValidationError
BJSONResponse
DHTTPException
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing HTTPException with validation error handling
Forgetting to import JSONResponse