{"id":"2615281d899bd889","repo":"tiangolo/fastapi","slug":"item-not-found-261528","errorCode":null,"errorMessage":"Item not found","messagePattern":"Item not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"docs_src/app_testing/app_b_py310/main.py","lineNumber":25,"sourceCode":"    \"foo\": {\"id\": \"foo\", \"title\": \"Foo\", \"description\": \"There goes my hero\"},\n    \"bar\": {\"id\": \"bar\", \"title\": \"Bar\", \"description\": \"The bartenders\"},\n}\n\napp = FastAPI()\n\n\nclass Item(BaseModel):\n    id: str\n    title: str\n    description: str | None = None\n\n\n@app.get(\"/items/{item_id}\", response_model=Item)\nasync def read_main(item_id: str, x_token: str = Header()):\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item_id not in fake_db:\n        raise HTTPException(status_code=404, detail=\"Item not found\")\n    return fake_db[item_id]\n\n\n@app.post(\"/items/\")\nasync def create_item(item: Item, x_token: str = Header()) -> Item:\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item.id in fake_db:\n        raise HTTPException(status_code=409, detail=\"Item already exists\")\n    fake_db[item.id] = item.model_dump()\n    return item\n","sourceCodeStart":7,"sourceCodeEnd":37,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/app_testing/app_b_py310/main.py#L7-L37","documentation":"Non-Annotated variant of error 2: GET /items/{item_id} raises HTTP 404 \"Item not found\" when item_id is absent from fake_db. Identical runtime behavior to error 2.","triggerScenarios":"GET /items/{item_id} with valid token but an id not in {\"foo\",\"bar\"}.","commonSituations":"Looking up a non-existent or deleted item; id typo.","solutions":["Request an existing id or create it first.","Handle the 404 gracefully in the client.","Verify id format and casing."],"exampleFix":"# before\nclient.get(\"/items/ghost\", headers=auth)\n# after\nclient.get(\"/items/foo\", headers=auth)","handlingStrategy":"validation","validationCode":"valid_ids = {\"foo\", \"bar\"}\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":["Verify the id is known before requesting.","Strip/normalize the id string.","Handle 404 without retrying blindly."],"tags":["fastapi","not-found","httpexception","rest"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}