{"record":{"id":"267725dbdeb66756","repo":"tiangolo/fastapi","slug":"nope-i-don-t-like-3","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/tutorial004_py310.py","lineNumber":25,"sourceCode":"\n\n@app.exception_handler(StarletteHTTPException)\nasync def http_exception_handler(request, exc):\n    return PlainTextResponse(str(exc.detail), status_code=exc.status_code)\n\n\n@app.exception_handler(RequestValidationError)\nasync def validation_exception_handler(request, exc: RequestValidationError):\n    message = \"Validation errors:\"\n    for error in exc.errors():\n        message += f\"\\nField: {error['loc']}, Error: {error['msg']}\"\n    return PlainTextResponse(message, status_code=400)\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":7,"sourceCodeEnd":27,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/handling_errors/tutorial004_py310.py#L7-L27","documentation":"HTTPException(418) raised in the path operation when item_id == 3. The tutorial overrides the default StarletteHTTPException and RequestValidationError handlers to return PlainTextResponse. This demonstrates a path-operation-driven 418 that flows through a custom http_exception_handler.","triggerScenarios":"GET /items/3. Any integer other than 3 returns 200; non-integer values return 400 (validation) via the custom validation handler.","commonSituations":"Path operations that reject specific inputs with non-standard status codes. Developers hit 418 when the path param equals the sentinel value 3, and may be surprised that the body is plain text (per the custom handler) rather than JSON.","solutions":["Request GET /items/{n} with n != 3 (e.g. /items/1) to succeed.","If 418 is meant as a real error, switch to a conventional status (400/409).","Confirm the response parser handles text/plain since the custom handler returns PlainTextResponse."],"exampleFix":"// before\nif item_id == 3:\n    raise HTTPException(status_code=418, detail=\"Nope! I don't like 3.\")\n// after\nif item_id == 3:\n    raise HTTPException(status_code=400, detail=\"Value 3 is not allowed\")","handlingStrategy":"validation","validationCode":"if item_id == 3:\n    print('will return 418')\n# ensure item_id is an int to avoid 422\nassert isinstance(item_id, int)","typeGuard":"def is_allowed_item(item_id: int) -> bool:\n    return item_id != 3","tryCatchPattern":"resp = requests.get(f'http://localhost:8000/items/{item_id}')\nif resp.status_code == 418:\n    print('rejected value:', resp.text)","preventionTips":["Avoid sending the sentinel value 3.","Send an int path param to avoid 422.","Expect text/plain bodies due to the custom handler."],"tags":["fastapi","http-418","custom-handler","httpexception"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}