{"id":"b354f78c3ca897d9","repo":"tiangolo/fastapi","slug":"item-not-found-b354f7","errorCode":null,"errorMessage":"Item not found","messagePattern":"Item not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"docs_src/dependencies/tutorial008b_an_py310.py","lineNumber":28,"sourceCode":"    \"portal-gun\": {\"description\": \"Gun to create portals\", \"owner\": \"Rick\"},\n}\n\n\nclass OwnerError(Exception):\n    pass\n\n\ndef get_username():\n    try:\n        yield \"Rick\"\n    except OwnerError as e:\n        raise HTTPException(status_code=400, detail=f\"Owner error: {e}\")\n\n\n@app.get(\"/items/{item_id}\")\ndef get_item(item_id: str, username: Annotated[str, Depends(get_username)]):\n    if item_id not in data:\n        raise HTTPException(status_code=404, detail=\"Item not found\")\n    item = data[item_id]\n    if item[\"owner\"] != username:\n        raise OwnerError(username)\n    return item\n","sourceCodeStart":10,"sourceCodeEnd":33,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/dependencies/tutorial008b_an_py310.py#L10-L33","documentation":"Raised by GET /items/{item_id} in the tutorial008b example when item_id is not a key in the data dict (only \"plumbus\" and \"portal-gun\" exist), returning HTTP 404. This check precedes the owner check, so unknown ids never reach the OwnerError path.","triggerScenarios":"GET /items/{item_id} with an id not in {\"plumbus\",\"portal-gun\"}, e.g. /items/unknown.","commonSituations":"Requesting an unseeded/deleted item; id typo; wrong environment data.","solutions":["Use a known id (\"plumbus\" or \"portal-gun\").","Handle the 404 in the client.","Confirm the id against the data source."],"exampleFix":"# before\nclient.get(\"/items/unknown\")\n# after\nclient.get(\"/items/portal-gun\")","handlingStrategy":"validation","validationCode":"valid_ids = {\"plumbus\", \"portal-gun\"}\nif item_id in valid_ids:\n    client.get(f\"/items/{item_id}\")","typeGuard":"def item_exists(item_id: str, known: set[str]) -> bool:\n    return item_id in known","tryCatchPattern":"resp = client.get(f\"/items/{item_id}\")\nif resp.status_code == 404:\n    ...","preventionTips":["Verify the id is seeded before requesting.","Handle 404 gracefully.","Normalize the id string."],"tags":["fastapi","not-found","httpexception","rest"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}