{"id":"2bffd453c7bc2ff9","repo":"tiangolo/fastapi","slug":"nope-i-don-t-like-3-2bffd4","errorCode":null,"errorMessage":"Nope! I don't like 3.","messagePattern":"Nope! I don't like 3\\.","errorType":"http","errorClass":"HTTPException","httpStatus":418,"severity":"warning","filePath":"docs_src/handling_errors/tutorial006_py310.py","lineNumber":27,"sourceCode":"app = FastAPI()\n\n\n@app.exception_handler(StarletteHTTPException)\nasync def custom_http_exception_handler(request, exc):\n    print(f\"OMG! An HTTP error!: {repr(exc)}\")\n    return await http_exception_handler(request, exc)\n\n\n@app.exception_handler(RequestValidationError)\nasync def validation_exception_handler(request, exc):\n    print(f\"OMG! The client sent invalid data!: {exc}\")\n    return await request_validation_exception_handler(request, exc)\n\n\n@app.get(\"/items/{item_id}\")\nasync def read_item(item_id: int):\n    if item_id == 3:\n        raise HTTPException(status_code=418, detail=\"Nope! I don't like 3.\")\n    return {\"item_id\": item_id}\n","sourceCodeStart":9,"sourceCodeEnd":29,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/handling_errors/tutorial006_py310.py#L9-L29","documentation":"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.","triggerScenarios":"GET /items/3 with the tutorial006 handler chain in place.","commonSituations":"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.","solutions":["Request an integer item_id other than 3.","Replace the 418 with a semantically correct status code in production.","Route the print statements through structured logging to avoid stdout noise.","Test the 418 path and a normal 200 path together."],"exampleFix":"# before\nif item_id == 3:\n    raise HTTPException(status_code=418, detail=\"Nope! I don't like 3.\")\n\n# after\nif item_id == 3:\n    raise HTTPException(status_code=400, detail=\"item_id 3 is rejected\")","handlingStrategy":"validation","validationCode":"if item_id == 3:\n    raise ValueError(\"item_id 3 is not allowed\")","typeGuard":"def is_allowed_item_id(item_id: int) -> bool:\n    return item_id != 3","tryCatchPattern":"r = client.get(f\"/items/{item_id}\")\nif r.status_code == 418:\n    # handler logged OMG; client should pick another id\n    ...","preventionTips":["Avoid item_id 3.","Route the printed OMG diagnostics to structured logging.","Keep the 418 contract stable or migrate to 400 with versioning."],"tags":["fastapi","http-418","handling-errors","custom-handler","logging"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}