{"id":"f399096faee9061c","repo":"tiangolo/fastapi","slug":"item-not-found-f39909","errorCode":null,"errorMessage":"Item not found","messagePattern":"Item not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"docs_src/bigger_applications/app_an_py310/routers/items.py","lineNumber":24,"sourceCode":"    prefix=\"/items\",\n    tags=[\"items\"],\n    dependencies=[Depends(get_token_header)],\n    responses={404: {\"description\": \"Not found\"}},\n)\n\n\nfake_items_db = {\"plumbus\": {\"name\": \"Plumbus\"}, \"gun\": {\"name\": \"Portal Gun\"}}\n\n\n@router.get(\"/\")\nasync def read_items():\n    return fake_items_db\n\n\n@router.get(\"/{item_id}\")\nasync def read_item(item_id: str):\n    if item_id not in fake_items_db:\n        raise HTTPException(status_code=404, detail=\"Item not found\")\n    return {\"name\": fake_items_db[item_id][\"name\"], \"item_id\": item_id}\n\n\n@router.put(\n    \"/{item_id}\",\n    tags=[\"custom\"],\n    responses={403: {\"description\": \"Operation forbidden\"}},\n)\nasync def update_item(item_id: str):\n    if item_id != \"plumbus\":\n        raise HTTPException(\n            status_code=403, detail=\"You can only update the item: plumbus\"\n        )\n    return {\"item_id\": item_id, \"name\": \"The great Plumbus\"}\n","sourceCodeStart":6,"sourceCodeEnd":39,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/bigger_applications/app_an_py310/routers/items.py#L6-L39","documentation":"Raised by GET /items/{item_id} in the bigger-applications items router when item_id is not a key in fake_items_db (only \"plumbus\" and \"gun\" exist), returning HTTP 404. The router also declares responses={404} so the 404 is documented in the OpenAPI schema.","triggerScenarios":"GET /items/{item_id} with an id other than \"plumbus\" or \"gun\" (e.g. /items/xyz), after the X-Token check passes.","commonSituations":"Requesting an item id that was never seeded; typo; using an id from another dataset.","solutions":["Use a seeded id (\"plumbus\" or \"gun\").","Handle 404 in the client with a fallback.","List items via GET /items/ first to discover valid ids."],"exampleFix":"# before\nclient.get(\"/items/xyz\", headers=auth)\n# after\nclient.get(\"/items/plumbus\", headers=auth)","handlingStrategy":"validation","validationCode":"valid_ids = {\"plumbus\", \"gun\"}\nif item_id in valid_ids:\n    client.get(f\"/items/{item_id}\", headers=auth)","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}\", headers=auth)\nif resp.status_code == 404:\n    ...","preventionTips":["List /items/ first to discover valid ids.","Handle 404 as expected control flow.","Guard against id typos."],"tags":["fastapi","not-found","httpexception","rest"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}