Bird
0
0

You want to create a FastAPI endpoint that returns a custom error response model for both 400 and 404 errors. Which of the following is the correct way to define the responses parameter?

hard🚀 Application Q8 of 15
FastAPI - Error Handling
You want to create a FastAPI endpoint that returns a custom error response model for both 400 and 404 errors. Which of the following is the correct way to define the responses parameter?
Aresponses={"400": ErrorModel, "404": ErrorModel}
Bresponses={400: ErrorModel, 404: ErrorModel}
Cresponses={400: {"model": ErrorModel}, 404: {"model": ErrorModel}}
Dresponses=[400, 404], model=ErrorModel
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct 'responses' dictionary format

    The keys must be integers for status codes, and values must be dictionaries with a 'model' key.
  2. Step 2: Check each option

    responses={400: {"model": ErrorModel}, 404: {"model": ErrorModel}} correctly maps 400 and 404 to dicts with 'model': ErrorModel. Options A and B assign model directly, which is incorrect. responses=[400, 404], model=ErrorModel is invalid syntax.
  3. Final Answer:

    responses={400: {"model": ErrorModel}, 404: {"model": ErrorModel}} -> Option C
  4. Quick Check:

    Multiple error models use dict with 'model' key per status code [OK]
Quick Trick: Map each status code to dict with 'model' key in 'responses' [OK]
Common Mistakes:
MISTAKES
  • Assigning model class directly without dict
  • Using list instead of dict for responses
  • Assigning model directly with string keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes