tiangolo/fastapi · warning · HTTPException

Nope! I don't like 3.

Error message

Nope! I don't like 3.

What it means

Same HTTP 418 "Nope! I don't like 3." as error 37, but tutorial006 wires custom handlers that delegate to FastAPI's built-in `http_exception_handler`/`request_validation_exception_handler` after logging, demonstrating how to extend (rather than replace) default behavior. The 418 still fires only for item_id==3.

Source

Thrown at docs_src/handling_errors/tutorial006_py310.py:27

app = FastAPI()


@app.exception_handler(StarletteHTTPException)
async def custom_http_exception_handler(request, exc):
    print(f"OMG! An HTTP error!: {repr(exc)}")
    return await http_exception_handler(request, exc)


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    print(f"OMG! The client sent invalid data!: {exc}")
    return await request_validation_exception_handler(request, exc)


@app.get("/items/{item_id}")
async def read_item(item_id: int):
    if item_id == 3:
        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
    return {"item_id": item_id}

View on GitHub (pinned to 42a41db11f)

Solutions

  1. Request an integer item_id other than 3.
  2. Replace the 418 with a semantically correct status code in production.
  3. Route the print statements through structured logging to avoid stdout noise.
  4. Test the 418 path and a normal 200 path together.

Example fix

# before
if item_id == 3:
    raise HTTPException(status_code=418, detail="Nope! I don't like 3.")

# after
if item_id == 3:
    raise HTTPException(status_code=400, detail="item_id 3 is rejected")
Defensive patterns

Strategy: validation

Validate before calling

if item_id == 3:
    raise ValueError("item_id 3 is not allowed")

Type guard

def is_allowed_item_id(item_id: int) -> bool:
    return item_id != 3

Try / catch

r = client.get(f"/items/{item_id}")
if r.status_code == 418:
    # handler logged OMG; client should pick another id
    ...

Prevention

When it happens

Trigger: GET /items/3 with the tutorial006 handler chain in place.

Common situations: Clients hitting the reserved value 3; log/metric noise from the printed OMG line; assuming the response is JSON when the handler chain may alter it.

Related errors


AI-assisted analysis of tiangolo/fastapi@42a41db11f (2026-08-04). Data as JSON: /data/errors/2bffd453c7bc2ff9.json. Report an issue: GitHub.