Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a custom exception handler in FastAPI?
A custom exception handler in FastAPI is a function you write to catch specific errors and return a friendly response instead of the default error message.
Click to reveal answer
beginner
How do you register a custom exception handler in FastAPI?
You use the @app.exception_handler decorator with the exception class you want to handle, then define a function that takes the request and exception as parameters and returns a response.
Click to reveal answer
intermediate
Why use custom exception handlers instead of default error responses?
Custom handlers let you control the message, status code, and format sent to users, making errors clearer and improving user experience.
Click to reveal answer
beginner
What parameters does a FastAPI exception handler function receive?
It receives two parameters: the request object and the exception instance that was raised.
Click to reveal answer
beginner
Show a simple example of a custom exception handler for a ValueError in FastAPI.
The handler returns a JSONResponse with status_code=418.
Step 2: Understand what happens when exception is raised
Raising MyException triggers the handler, which sends the 418 status.
Final Answer:
418 -> Option B
Quick Check:
Handler sets status 418 = A [OK]
Hint: Check the status_code in JSONResponse inside handler [OK]
Common Mistakes:
Assuming default 500 error code
Confusing 404 with missing route
Ignoring custom status_code in handler
4. What is wrong with this FastAPI custom exception handler code?
from fastapi import FastAPI
app = FastAPI()
class CustomError(Exception):
pass
@app.exception_handler(CustomError)
def handler(exc: CustomError):
return {"error": "Something went wrong"}
medium
A. Return value must be a string, not a dict
B. Exception class must inherit from HTTPException
C. Handler function must be async and accept Request parameter
D. Decorator should be @app.add_exception_handler, not @app.exception_handler
Solution
Step 1: Check handler function signature
FastAPI expects async handler with parameters (Request, Exception).
Step 2: Identify missing Request and async
The handler lacks the Request parameter and is not async.
Final Answer:
Handler function must be async and accept Request parameter -> Option C
Quick Check:
Handler signature requires async and Request = C [OK]
Hint: Handler must be async and take Request as first argument [OK]
Common Mistakes:
Making handler synchronous
Omitting Request parameter
Thinking exception must inherit HTTPException
5. You want to create a custom exception handler in FastAPI that returns a JSON response with a dynamic message and a 400 status code whenever ValueError is raised. Which code snippet correctly implements this?
hard
A. from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
app = FastAPI()
@app.exception_handler(ValueError)
async def value_error_handler(request: Request, exc: ValueError):
return JSONResponse(content={"error": str(exc)}, status=400)
B. from fastapi import FastAPI
app = FastAPI()
@app.exception_handler(ValueError)
def value_error_handler(exc: ValueError):
return {"error": str(exc), "status": 400}