0
0
FastAPIframework~20 mins

Validation error responses in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the default HTTP status code for validation errors in FastAPI?
When FastAPI detects a validation error in the request data, what HTTP status code does it return by default?
A422 Unprocessable Entity
B200 OK
C400 Bad Request
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Think about the status code that means the server understands the request but the data is invalid.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly customizes the validation error response in FastAPI?
You want to customize the response when validation fails in FastAPI. Which code snippet correctly overrides the default validation error handler?
FastAPI
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError

app = FastAPI()

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    return JSONResponse(
        status_code=400,
        content={"detail": exc.errors(), "body": exc.body},
    )
AOverride FastAPI's default exception handler by subclassing FastAPI and redefining validation_error method
BUse @app.middleware('http') to catch validation errors and return JSONResponse
CUse @app.get('/error') endpoint to manually check validation and return custom response
DUse @app.exception_handler(RequestValidationError) with async function returning JSONResponse with status_code 400
Attempts:
2 left
💡 Hint
Look for the decorator that handles exceptions of type RequestValidationError.
🔧 Debug
advanced
2:30remaining
Why does this FastAPI validation error handler not work as expected?
Consider this code snippet that tries to customize validation error responses. Why does it fail to catch validation errors?
FastAPI
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

app = FastAPI()

@app.exception_handler(Exception)
async def generic_exception_handler(request, exc):
    if isinstance(exc, RequestValidationError):
        return JSONResponse(status_code=400, content={"error": "Validation failed"})
    return JSONResponse(status_code=500, content={"error": "Server error"})
ABecause @app.exception_handler(Exception) does not catch RequestValidationError exceptions
BBecause FastAPI processes RequestValidationError before generic Exception handlers, so this handler is never called for validation errors
CBecause the handler function is missing the Request type annotation for the request parameter
DBecause JSONResponse cannot be returned from exception handlers
Attempts:
2 left
💡 Hint
Think about the order FastAPI uses to handle exceptions and specific vs generic handlers.
state_output
advanced
1:30remaining
What is the content of the validation error response body?
When a validation error occurs in FastAPI, what keys are included in the JSON response body by default?
A{"message": "Invalid input", "status": 400}
B{"error": "Validation failed", "code": 422}
C{"detail": [{"loc": [...], "msg": "...", "type": "..."}]}
D{"errors": ["field required", "value is not valid"]}
Attempts:
2 left
💡 Hint
Look for the key that contains a list of error details with location and message.
🧠 Conceptual
expert
3:00remaining
How can you globally change the validation error status code in FastAPI without overriding the handler?
You want all validation errors in your FastAPI app to return status code 400 instead of the default 422, but you do not want to write a custom exception handler. Which approach achieves this?
AUse a middleware to intercept responses and change status code 422 to 400
BUse the openapi_schema parameter to modify the validation error status code globally
COverride the default RequestValidationError handler by subclassing FastAPI and setting a custom validation error status code property
DSet the status_code attribute on the Pydantic model to 400
Attempts:
2 left
💡 Hint
Think about intercepting responses after they are generated without changing exception handlers.