Challenge - 5 Problems
FastAPI Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a custom exception is raised in FastAPI?
Consider this FastAPI app with a custom exception and handler. What will the client receive when the endpoint is called?
FastAPI
from fastapi import FastAPI, Request from fastapi.responses import JSONResponse app = FastAPI() class MyCustomError(Exception): def __init__(self, name: str): self.name = name @app.exception_handler(MyCustomError) async def custom_exception_handler(request: Request, exc: MyCustomError): return JSONResponse( status_code=418, content={"message": f"Oops! {exc.name} caused an error."} ) @app.get("/error") async def error_endpoint(): raise MyCustomError("TestError")
Attempts:
2 left
💡 Hint
Look at the exception handler's return value and status code.
✗ Incorrect
The custom exception handler catches MyCustomError and returns a JSON response with status 418 and a message including the error name.
📝 Syntax
intermediate2:00remaining
Which option correctly registers a custom exception handler in FastAPI?
You want to handle a custom exception MyError globally in FastAPI. Which code snippet correctly registers the handler?
FastAPI
from fastapi import FastAPI, Request from fastapi.responses import JSONResponse app = FastAPI() class MyError(Exception): pass async def my_error_handler(request: Request, exc: MyError): return JSONResponse({"error": "MyError occurred"}, status_code=400) # Which line correctly registers the handler?
Attempts:
2 left
💡 Hint
Check FastAPI's method to add exception handlers programmatically.
✗ Incorrect
The correct method to register an exception handler is add_exception_handler(exception_class, handler_function).
🔧 Debug
advanced2:00remaining
Why does this custom exception handler not work as expected?
This FastAPI app defines a custom exception and handler but the handler is never called. Why?
FastAPI
from fastapi import FastAPI, Request from fastapi.responses import JSONResponse app = FastAPI() class CustomError(Exception): pass @app.exception_handler(Exception) async def generic_handler(request: Request, exc: Exception): return JSONResponse({"error": "Generic error"}, status_code=500) @app.get("/fail") async def fail(): raise CustomError()
Attempts:
2 left
💡 Hint
Check which exception the handler is registered for.
✗ Incorrect
Only a handler registered for CustomError will catch it. The generic Exception handler does not replace specific handlers but here no CustomError handler is defined.
❓ state_output
advanced2:00remaining
What is the HTTP status code returned when a custom handler returns a JSONResponse with status 404?
Given this handler, what status code does the client receive when the exception is raised?
FastAPI
from fastapi import FastAPI, Request from fastapi.responses import JSONResponse app = FastAPI() class NotFoundError(Exception): pass @app.exception_handler(NotFoundError) async def not_found_handler(request: Request, exc: NotFoundError): return JSONResponse({"detail": "Item not found"}, status_code=404) @app.get("/item") async def get_item(): raise NotFoundError()
Attempts:
2 left
💡 Hint
Look at the status_code argument in JSONResponse.
✗ Incorrect
The handler returns a JSONResponse with status_code=404, so the client receives HTTP 404.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI custom exception handlers is true?
Select the correct statement about how FastAPI handles custom exceptions and their handlers.
Attempts:
2 left
💡 Hint
Think about inheritance and handler specificity.
✗ Incorrect
FastAPI chooses the handler registered for the most specific exception class matching the raised exception.