{"record":{"id":"9dfeb69b8466c4c6","repo":"tiangolo/fastapi","slug":"item-not-found-there-s-only-a-plumbus-here-9dfeb6","errorCode":null,"errorMessage":"Item not found, there's only a plumbus here","messagePattern":"Item not found, there's only a plumbus here","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"docs_src/dependencies/tutorial008d_an_py310.py","lineNumber":27,"sourceCode":"    pass\n\n\ndef get_username():\n    try:\n        yield \"Rick\"\n    except InternalError:\n        print(\"We don't swallow the internal error here, we raise again 😎\")\n        raise\n\n\n@app.get(\"/items/{item_id}\")\ndef get_item(item_id: str, username: Annotated[str, Depends(get_username)]):\n    if item_id == \"portal-gun\":\n        raise InternalError(\n            f\"The portal gun is too dangerous to be owned by {username}\"\n        )\n    if item_id != \"plumbus\":\n        raise HTTPException(\n            status_code=404, detail=\"Item not found, there's only a plumbus here\"\n        )\n    return item_id\n","sourceCodeStart":9,"sourceCodeEnd":31,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/dependencies/tutorial008d_an_py310.py#L9-L31","documentation":"HTTPException (404) in tutorial008d_an_py310 — the Annotated variant where the yield-dependency DOES re-raise InternalError. For any item_id other than 'plumbus' (and other than 'portal-gun' which raises InternalError that now propagates as a 500), the endpoint returns this 404. Demonstrates the contrast with the swallowing variant.","triggerScenarios":"GET /items/{item_id} where item_id != 'plumbus' and != 'portal-gun'. GET /items/portal-gun now surfaces as InternalError (500) instead of being swallowed.","commonSituations":"Endpoints that both return clean 404s for unknown ids and propagate internal errors. Developers hit the 404 when testing ordinary unknown ids and a 500 when testing 'portal-gun'.","solutions":["Request GET /items/plumbus for a success.","Distinguish the two failure paths: unknown id -> 404, 'portal-gun' -> 500 InternalError (re-raised by the dependency).","If you need 'portal-gun' to return a real HTTP status, convert InternalError to an HTTPException in the dependency's except block."],"exampleFix":"// before\ndef get_username():\n    try:\n        yield \"Rick\"\n    except InternalError:\n        print(\"We don't swallow the internal error here, we raise again\")\n        raise\n// after (translate to HTTP)\nexcept InternalError as e:\n    raise HTTPException(status_code=403, detail=str(e))","handlingStrategy":"try-catch","validationCode":"ALLOWED = {'plumbus'}\nif item_id not in ALLOWED:\n    print('404 expected; portal-gun yields 500')","typeGuard":"def is_known_item(item_id: str) -> bool:\n    return item_id == 'plumbus'","tryCatchPattern":"resp = requests.get(f'http://localhost:8000/items/{item_id}')\nif resp.status_code == 404:\n    print('unknown id')\nelif resp.status_code == 500:\n    print('InternalError re-raised by dependency')","preventionTips":["Distinguish 404 (unknown id) from 500 (InternalError re-raised).","Map InternalError to an HTTP status in the dependency for cleaner responses.","Test 'portal-gun' separately from generic unknown ids."],"tags":["fastapi","http-404","annotated","dependencies","re-raise"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}