{"record":{"id":"cf00267b1766221c","repo":"tiangolo/fastapi","slug":"item-not-found-cf0026","errorCode":null,"errorMessage":"Item not found","messagePattern":"Item not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"docs_src/dependencies/tutorial008b_py310.py","lineNumber":26,"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: 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":8,"sourceCodeEnd":31,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/dependencies/tutorial008b_py310.py#L8-L31","documentation":"Standard HTTPException with status_code 404 raised in the path operation when item_id is not a key in the in-memory `data` dict. It is FastAPI's canonical way to signal a missing resource. The library converts it into a JSON response of shape {detail: 'Item not found'} with a 404 status.","triggerScenarios":"GET /items/{item_id} where item_id is neither 'plumbus' nor 'portal-gun' (the only keys in `data`). Any other path segment, e.g. GET /items/foo, triggers it.","commonSituations":"Lookup-by-ID endpoints backed by a dict/database where the client passes an unknown or deleted identifier. Common after data resets, with stale client-cached IDs, or when the URL casing/encoding does not match the stored key.","solutions":["Verify the item_id against the known keys ('plumbus', 'portal-gun') before requesting.","Check the data source the app reads from — if it was reset or seeded differently, missing keys now return 404.","Return a more descriptive detail or use a typed response model so clients can distinguish 'not found' from 'forbidden'."],"exampleFix":"// before\nif item_id not in data:\n    raise HTTPException(status_code=404, detail=\"Item not found\")\n// after (richer detail)\nif item_id not in data:\n    raise HTTPException(status_code=404, detail=f\"Item '{item_id}' not found; available: {list(data)}\")","handlingStrategy":"validation","validationCode":"known_ids = {'plumbus', 'portal-gun'}\nitem_id = 'foo'\nassert item_id in known_ids, f'{item_id} not in {known_ids}'","typeGuard":"def item_exists(item_id: str, data: dict) -> bool:\n    return item_id in data","tryCatchPattern":"import requests\nresp = requests.get(f'http://localhost:8000/items/{item_id}')\nif resp.status_code == 404:\n    print('not found, pick from', data.keys())\nelse:\n    resp.raise_for_status()","preventionTips":["Keep the set of valid ids in sync with the client.","Use typed enums or literal types for known ids in tests.","Log requested ids to spot stale references."],"tags":["fastapi","http-404","not-found","lookup"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}