Bird
0
0

What is the issue with this FastAPI custom exception handler registration?

medium📝 Debug Q6 of 15
FastAPI - Error Handling
What is the issue with this FastAPI custom exception handler registration?
from fastapi import FastAPI, Request

app = FastAPI()

class CustomException(Exception):
    pass

@app.exception_handler(CustomException)
async def custom_handler(request: Request):
    return {"message": "Error occurred"}
AThe handler function should not be async
BThe decorator is missing the exception class
CThe handler function is missing the exception parameter
DThe return type must be a string, not a dict
Step-by-Step Solution
Solution:
  1. Step 1: Check handler function signature

    FastAPI expects the handler to accept two parameters: request and the exception instance.
  2. Step 2: Identify missing parameter

    The function custom_handler only accepts request, missing the exception parameter.
  3. Final Answer:

    The handler function is missing the exception parameter -> Option C
  4. Quick Check:

    Handler must accept (request, exc) parameters [OK]
Quick Trick: Handler functions need both request and exception parameters [OK]
Common Mistakes:
MISTAKES
  • Omitting the exception parameter in handler function
  • Using synchronous functions incorrectly
  • Returning raw dict instead of Response object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes