{"id":"d485ee48f976273b","repo":"tiangolo/fastapi","slug":"username-d485ee","errorCode":null,"errorMessage":"{username}","messagePattern":"\\{username\\}","errorType":"exception","errorClass":"OwnerError","httpStatus":400,"severity":"error","filePath":"docs_src/dependencies/tutorial008b_py310.py","lineNumber":29,"sourceCode":"\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":11,"sourceCodeEnd":31,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/dependencies/tutorial008b_py310.py#L11-L31","documentation":"Identical to error 20 but in the non-`Annotated` parameter style (`username: str = Depends(get_username)`). An OwnerError carrying the username is raised when the resolved item's owner differs from the dependency-yielded identity, then translated to HTTP 400 by the yield dependency's except block.","triggerScenarios":"GET /items/plumbus (owner=\"Morty\") with username dependency yielding \"Rick\", tripping `if item[\"owner\"] != username: raise OwnerError(username)`.","commonSituations":"Same as error 20; specifically seen when migrating tutorials from default-value Depends to Annotated Depends and the older form is still in use.","solutions":["Request an item owned by the yielded user (\"portal-gun\").","Return HTTP 403 with a clear detail from the handler instead of the opaque OwnerError.","Replace the hardcoded `yield \"Rick\"` with a real auth dependency.","Cover both owned and unowned item ids in tests."],"exampleFix":"# before\nif item[\"owner\"] != username:\n    raise OwnerError(username)\n\n# after\nif item[\"owner\"] != username:\n    raise HTTPException(status_code=403, detail=\"Forbidden: not the owner\")","handlingStrategy":"validation","validationCode":"if KNOWN_OWNERS[item_id] != \"Rick\":\n    raise PermissionError(\"owner mismatch\")","typeGuard":"def owned_by(item: dict, who: str) -> bool:\n    return item.get(\"owner\") == who","tryCatchPattern":"r = client.get(f\"/items/{item_id}\")\nif r.status_code == 400:\n    detail = r.json().get(\"detail\", \"\")\n    if detail.startswith(\"Owner error:\"):\n        # pick another item\n        ...","preventionTips":["Migrate from default-value Depends to Annotated for clearer signatures.","Keep ownership data consistent with the auth principal.","Treat 'Owner error' as authorization denial."],"tags":["fastapi","dependencies","authorization","yield-dependency","http-400"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}