Challenge - 5 Problems
HTTPException Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when HTTPException is raised in FastAPI?
Consider this FastAPI endpoint code:
What will the client receive if they request
from fastapi import FastAPI, HTTPException
app = FastAPI()
@app.get('/items/{item_id}')
async def read_item(item_id: int):
if item_id < 1:
raise HTTPException(status_code=400, detail='Invalid item ID')
return {'item_id': item_id}What will the client receive if they request
/items/0?FastAPI
from fastapi import FastAPI, HTTPException app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: int): if item_id < 1: raise HTTPException(status_code=400, detail='Invalid item ID') return {'item_id': item_id}
Attempts:
2 left
💡 Hint
Think about what HTTPException does in FastAPI when raised inside a route.
✗ Incorrect
Raising HTTPException in FastAPI immediately returns an HTTP response with the given status code and detail as JSON. It does not crash the server or redirect.
📝 Syntax
intermediate2:00remaining
Identify the correct way to raise HTTPException with a custom header
Which of the following code snippets correctly raises an HTTPException with status 403 and a custom header
X-Error: Forbidden?Attempts:
2 left
💡 Hint
Check the exact parameter names in HTTPException constructor.
✗ Incorrect
The HTTPException constructor expects named parameters: status_code, detail, and headers. Option A uses correct parameter names and syntax.
🔧 Debug
advanced2:00remaining
Why does this HTTPException raise a TypeError?
Given this code snippet:
What error will occur and why?
from fastapi import HTTPException raise HTTPException(status_code=404, detail=123)
What error will occur and why?
FastAPI
from fastapi import HTTPException raise HTTPException(status_code=404, detail=123)
Attempts:
2 left
💡 Hint
Check the expected type for the detail parameter in HTTPException.
✗ Incorrect
The detail parameter should be a string, dict, or list. Passing an integer causes a TypeError when FastAPI tries to serialize the response.
❓ state_output
advanced2:00remaining
What is the response status code after catching HTTPException?
Consider this FastAPI code:
What will the HTTP status code of the response be when calling
from fastapi import FastAPI, HTTPException
app = FastAPI()
@app.get('/test')
async def test():
try:
raise HTTPException(status_code=401, detail='Unauthorized')
except HTTPException as e:
return {'caught': True, 'status': e.status_code}What will the HTTP status code of the response be when calling
/test?FastAPI
from fastapi import FastAPI, HTTPException app = FastAPI() @app.get('/test') async def test(): try: raise HTTPException(status_code=401, detail='Unauthorized') except HTTPException as e: return {'caught': True, 'status': e.status_code}
Attempts:
2 left
💡 Hint
Think about what happens when you catch the exception and return a normal dict.
✗ Incorrect
Catching HTTPException prevents it from propagating. Returning a dict sends a 200 OK response with that JSON body.
🧠 Conceptual
expert2:00remaining
Which statement about HTTPException in FastAPI is true?
Select the one correct statement about how HTTPException works in FastAPI.
Attempts:
2 left
💡 Hint
Think about the purpose of HTTPException in request handling.
✗ Incorrect
Raising HTTPException stops the current route execution and sends the specified HTTP error response immediately. It does not log automatically or affect global server state.