Bird
Raised Fist0
FastAPIframework~20 mins

Global exception middleware in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Global Exception Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a ValueError is raised inside a FastAPI route with global exception middleware?

Consider a FastAPI app with a global exception middleware that catches ValueError and returns a JSON response with status 400 and a message. What will the client receive if a route raises ValueError('Invalid input')?

FastAPI
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse

app = FastAPI()

@app.middleware("http")
async def catch_value_error(request: Request, call_next):
    try:
        response = await call_next(request)
        return response
    except ValueError as e:
        return JSONResponse(status_code=400, content={"error": str(e)})

@app.get("/test")
async def test_route():
    raise ValueError("Invalid input")
A{"error": "Invalid input"} with status code 400
B500 Internal Server Error with default HTML response
C{"detail": "Invalid input"} with status code 422
DEmpty response with status code 204
Attempts:
2 left
💡 Hint

Think about what the middleware does when it catches a ValueError.

📝 Syntax
intermediate
2:00remaining
Which middleware code snippet correctly catches all exceptions and returns a JSON error response?

Choose the correct FastAPI middleware code that catches any exception during request processing and returns a JSON response with status 500 and a generic error message.

A
async def middleware(request: Request, call_next):
    try:
        return await call_next(request)
    except Exception as e:
        return JSONResponse(status_code=500, content={"error": "Server error"})
B
async def middleware(request: Request, call_next):
    try:
        response = call_next(request)
        return response
    except Exception:
        return JSONResponse(status_code=500, content={"error": "Server error"})
C
async def middleware(request: Request, call_next):
    try:
        response = await call_next(request)
    except Exception as e:
        return JSONResponse(status_code=500, content={"error": str(e)})
    return response
D
async def middleware(request: Request, call_next):
    response = await call_next(request)
    except Exception:
        return JSONResponse(status_code=500, content={"error": "Server error"})
    return response
Attempts:
2 left
💡 Hint

Remember to await the call_next and handle exceptions properly.

🔧 Debug
advanced
3:00remaining
Why does this global exception middleware not catch exceptions raised in background tasks?

Given this middleware that catches exceptions during request processing, why are exceptions raised inside background tasks not caught by it?

FastAPI
from fastapi import FastAPI, BackgroundTasks, Request
from fastapi.responses import JSONResponse

app = FastAPI()

@app.middleware("http")
async def catch_exceptions(request: Request, call_next):
    try:
        response = await call_next(request)
        return response
    except Exception as e:
        return JSONResponse(status_code=500, content={"error": str(e)})

@app.get("/bg")
async def bg_route(background_tasks: BackgroundTasks):
    def task():
        raise RuntimeError("Background error")
    background_tasks.add_task(task)
    return {"message": "Task started"}
AMiddleware must be registered with background task manager to catch those exceptions
BBackground tasks exceptions are caught but ignored silently by FastAPI
CBackground tasks run synchronously blocking middleware exception handling
DMiddleware only catches exceptions during request processing, background tasks run after response is sent
Attempts:
2 left
💡 Hint

Think about when background tasks run compared to the request lifecycle.

🧠 Conceptual
advanced
2:00remaining
What is the main benefit of using global exception middleware in FastAPI?

Why would a developer add a global exception middleware instead of handling exceptions inside each route?

ATo automatically retry failed requests without user code
BTo centralize error handling and provide consistent error responses across all routes
CTo log all requests regardless of errors
DTo improve performance by skipping exception handling in routes
Attempts:
2 left
💡 Hint

Think about code reuse and consistency in user experience.

state_output
expert
3:00remaining
What is the response status code and body when a KeyError is raised but middleware only catches ValueError?

Given a FastAPI app with middleware that catches only ValueError exceptions and returns status 400, what happens if a route raises KeyError('missing')?

FastAPI
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse

app = FastAPI()

@app.middleware("http")
async def catch_value_error(request: Request, call_next):
    try:
        response = await call_next(request)
        return response
    except ValueError as e:
        return JSONResponse(status_code=400, content={"error": str(e)})

@app.get("/keyerror")
async def key_error_route():
    raise KeyError("missing")
A{"error": "missing"} with status code 400
B422 Unprocessable Entity with JSON error detail
C500 Internal Server Error with default HTML error page
D200 OK with empty body
Attempts:
2 left
💡 Hint

Consider which exceptions the middleware catches and what happens to others.

Practice

(1/5)
1. What is the main purpose of global exception middleware in a FastAPI application?
easy
A. To automatically generate API documentation
B. To speed up the API response time by caching results
C. To manage database connections efficiently
D. To catch and handle errors for the entire application in one place

Solution

  1. Step 1: Understand middleware role

    Middleware runs for every request and can intercept errors globally.
  2. Step 2: Identify purpose of global exception middleware

    It catches errors from any part of the app and handles them centrally.
  3. Final Answer:

    To catch and handle errors for the entire application in one place -> Option D
  4. Quick Check:

    Global error handling = catch all errors [OK]
Hint: Global middleware handles all errors in one place [OK]
Common Mistakes:
  • Confusing middleware with caching or documentation
  • Thinking it manages database connections
  • Assuming it only handles specific routes
2. Which of the following is the correct way to add a global exception middleware in FastAPI?
easy
A. app.use_middleware(ExceptionMiddleware, handler=custom_handler)
B. app.add_middleware(ExceptionMiddleware, handler=custom_handler)
C. app.middleware(ExceptionMiddleware, handler=custom_handler)
D. app.register_middleware(ExceptionMiddleware, handler=custom_handler)

Solution

  1. Step 1: Recall FastAPI middleware syntax

    FastAPI uses add_middleware method to add middleware.
  2. Step 2: Match correct method name

    Only add_middleware is valid; others are incorrect method names.
  3. Final Answer:

    app.add_middleware(ExceptionMiddleware, handler=custom_handler) -> Option B
  4. Quick Check:

    Use add_middleware() to add middleware [OK]
Hint: Use add_middleware() method to add middleware [OK]
Common Mistakes:
  • Using incorrect method names like use_middleware or register_middleware
  • Confusing middleware with route decorators
  • Missing required parameters in add_middleware
3. Given this FastAPI middleware code snippet, what will be the response if a ValueError is raised inside a route?
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse

app = FastAPI()

@app.middleware("http")
async def catch_exceptions_middleware(request: Request, call_next):
    try:
        response = await call_next(request)
        return response
    except ValueError as e:
        return JSONResponse(status_code=400, content={"error": str(e)})

@app.get("/test")
async def test_route():
    raise ValueError("Invalid input")
medium
A. {"detail": "ValueError"} with status 422
B. 500 Internal Server Error with default HTML page
C. {"error": "Invalid input"} with status 400
D. Empty response with status 200

Solution

  1. Step 1: Analyze middleware error handling

    The middleware catches ValueError and returns JSONResponse with status 400 and error message.
  2. Step 2: Check route behavior

    The route raises ValueError("Invalid input"), triggering the middleware's except block.
  3. Final Answer:

    {"error": "Invalid input"} with status 400 -> Option C
  4. Quick Check:

    Middleware catches ValueError and returns JSON error [OK]
Hint: Middleware catches ValueError and returns JSON with status 400 [OK]
Common Mistakes:
  • Assuming default 500 error instead of custom JSON response
  • Confusing status codes 422 and 400
  • Ignoring middleware and expecting normal route error
4. Identify the error in this FastAPI global exception middleware code:
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse

app = FastAPI()

@app.middleware("http")
async def exception_middleware(request: Request, call_next):
    try:
        response = call_next(request)
        return response
    except Exception as e:
        return JSONResponse(status_code=500, content={"error": "Server error"})
medium
A. Missing await before call_next(request)
B. Incorrect exception type used
C. JSONResponse should not be returned in middleware
D. Middleware should not catch exceptions

Solution

  1. Step 1: Check async call to call_next

    call_next is an async function and must be awaited.
  2. Step 2: Identify missing await

    Code calls call_next(request) without await, causing a coroutine object to be returned instead of response.
  3. Final Answer:

    Missing await before call_next(request) -> Option A
  4. Quick Check:

    Always await async call_next() in middleware [OK]
Hint: Always await call_next(request) in async middleware [OK]
Common Mistakes:
  • Forgetting to await async call_next
  • Thinking JSONResponse can't be returned in middleware
  • Believing middleware shouldn't catch exceptions
5. You want to create a global exception middleware in FastAPI that logs all exceptions and returns a JSON error with status 500. Which code snippet correctly implements this behavior?
hard
A. @app.middleware('http') async def global_exception(request, call_next): try: return await call_next(request) except Exception as e: print(f"Error: {e}") return JSONResponse(status_code=500, content={"error": "Internal server error"})
B. app.add_middleware(ExceptionMiddleware, handler=lambda req, exc: JSONResponse({"error": str(exc)}, status_code=500))
C. @app.exception_handler(Exception) async def global_exception_handler(request, exc): return JSONResponse(status_code=500, content={"error": str(exc)})
D. app.add_exception_handler(Exception, lambda request, exc: JSONResponse({"error": "Error occurred"}, status_code=500))

Solution

  1. Step 1: Understand middleware vs exception handler

    Middleware wraps all requests and can catch exceptions globally; exception handlers are per-exception but not middleware.
  2. Step 2: Check for logging and JSON response in middleware

    @app.middleware('http') async def global_exception(request, call_next): try: return await call_next(request) except Exception as e: print(f"Error: {e}") return JSONResponse(status_code=500, content={"error": "Internal server error"}) uses @app.middleware('http') with try-except, logs error with print, and returns JSONResponse with status 500.
  3. Step 3: Verify other options

    app.add_middleware(ExceptionMiddleware, handler=lambda req, exc: JSONResponse({"error": str(exc)}, status_code=500)) uses add_middleware incorrectly; C and D are exception handlers, not middleware.
  4. Final Answer:

    @app.middleware('http') async def global_exception(request, call_next): try: return await call_next(request) except Exception as e: print(f"Error: {e}") return JSONResponse(status_code=500, content={"error": "Internal server error"}) -> Option A
  5. Quick Check:

    Middleware with try-except and logging = @app.middleware('http') async def global_exception(request, call_next): try: return await call_next(request) except Exception as e: print(f"Error: {e}") return JSONResponse(status_code=500, content={"error": "Internal server error"}) [OK]
Hint: Use @app.middleware('http') with try-except and logging [OK]
Common Mistakes:
  • Confusing middleware with exception handlers
  • Using add_middleware incorrectly for exceptions
  • Not logging exceptions inside middleware