{"id":"0e901746b13c342a","repo":"tiangolo/fastapi","slug":"owner-error-e","errorCode":null,"errorMessage":"Owner error: {e}","messagePattern":"Owner error: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"docs_src/dependencies/tutorial008b_an_py310.py","lineNumber":22,"sourceCode":"\napp = FastAPI()\n\n\ndata = {\n    \"plumbus\": {\"description\": \"Freshly pickled plumbus\", \"owner\": \"Morty\"},\n    \"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":4,"sourceCodeEnd":33,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/dependencies/tutorial008b_an_py310.py#L4-L33","documentation":"Raised inside the get_username generator dependency when it catches an OwnerError thrown by the route after the dependency yielded. FastAPI re-enters the generator to deliver the exception; the dependency translates it into HTTP 400 with detail f\"Owner error: {e}\". This demonstrates using a yield-dependency both to provide a value and to centralize error handling for a domain-specific exception.","triggerScenarios":"GET /items/{item_id} where the item exists and is valid (so no 404) but item[\"owner\"] != \"Rick\" (the yielded username). For example, GET /items/plumbus whose owner is \"Morty\".","commonSituations":"Accessing a resource owned by another user; assuming the current user matches the owner; logic errors where the yielded username is wrong.","solutions":["Request an item owned by the yielded user (\"Rick\"), e.g. /items/portal-gun.","If ownership is legitimate, change the yielded username or the data's owner to align.","Handle the 400 response by informing the user they lack ownership."],"exampleFix":"# before\nclient.get(\"/items/plumbus\")  # owner is Morty\n# after\nclient.get(\"/items/portal-gun\")  # owner is Rick","handlingStrategy":"try-catch","validationCode":"# Only request items owned by the current user (\"Rick\")\nowned_by_rick = {\"portal-gun\"}\nif item_id in owned_by_rick:\n    client.get(f\"/items/{item_id}\")","typeGuard":"def owned_by_user(item: dict, user: str) -> bool:\n    return item.get(\"owner\") == user","tryCatchPattern":"resp = client.get(f\"/items/{item_id}\")\nif resp.status_code == 400 and \"Owner error\" in resp.json().get(\"detail\", \"\"):\n    # current user does not own this item\n    ...","preventionTips":["Request only resources owned by the yielded user.","Handle the 400 Owner error as an authorization outcome.","Keep the dependency's yielded value and data owners aligned."],"tags":["fastapi","dependencies","authorization","exception-handling"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}