{"record":{"id":"b5545b55cd7cb826","repo":"tiangolo/fastapi","slug":"item-not-found-b5545b","errorCode":null,"errorMessage":"Item not found","messagePattern":"Item not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"docs_src/handling_errors/tutorial001_py310.py","lineNumber":11,"sourceCode":"from fastapi import FastAPI, HTTPException\n\napp = FastAPI()\n\nitems = {\"foo\": \"The Foo Wrestlers\"}\n\n\n@app.get(\"/items/{item_id}\")\nasync def read_item(item_id: str):\n    if item_id not in items:\n        raise HTTPException(status_code=404, detail=\"Item not found\")\n    return {\"item\": items[item_id]}\n","sourceCodeStart":1,"sourceCodeEnd":13,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/handling_errors/tutorial001_py310.py#L1-L13","documentation":"The canonical FastAPI HTTPException(404) example from the handling-errors tutorial. The endpoint GET /items/{item_id} raises 'Item not found' when item_id is not a key in the in-memory `items` dict (which only contains 'foo'). FastAPI serializes it as a JSON body {detail: 'Item not found'} with status 404.","triggerScenarios":"GET /items/{item_id} where item_id != 'foo'. Any other path segment returns 404.","commonSituations":"First-contact example for FastAPI error handling. Developers see this when testing unknown ids or after the in-memory store is reset.","solutions":["Request GET /items/foo to succeed.","Replace the in-memory dict with a real data source if ids should be dynamic.","Add a custom exception handler or response_model if you need a different error shape."],"exampleFix":"// before\nif item_id not in items:\n    raise HTTPException(status_code=404, detail=\"Item not found\")\n// after\nif item_id not in items:\n    raise HTTPException(status_code=404, detail=f\"Item '{item_id}' not found\")","handlingStrategy":"validation","validationCode":"items = {'foo': 'The Foo Wrestlers'}\nassert item_id in items, f'{item_id} not found'","typeGuard":"def item_exists(item_id: str, items: dict) -> bool:\n    return item_id in items","tryCatchPattern":"resp = requests.get(f'http://localhost:8000/items/{item_id}')\nif resp.status_code == 404:\n    print('Item not found')","preventionTips":["Keep the client's id set in sync with the server's store.","Use a typed catalog for tests.","Log 404s to detect stale references."],"tags":["fastapi","http-404","not-found","httpexception"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}