python
runtime_error
ai_generated
true
starlette.exceptions.HTTPException: 404:未找到。
starlette.exceptions.HTTPException: 404: Not Found
ID: python/starlette-http-exception-status-code
80%修复率
87%置信度
0证据数
2024-07-21首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.37.x | active | — | — | — |
| 0.40.x | active | — | — | — |
根因分析
某个路由或依赖显式抛出了 HTTPException(404),或路由器未匹配到任何路径,且异常未被自定义处理器捕获。
English
A route or dependency explicitly raised HTTPException(404) or a router matched no path, and the exception was not handled by a custom handler.
解决方案
-
95% 成功率
@app.exception_handler(HTTPException) async def handler(request, exc): return JSONResponse({'detail': exc.detail}, status_code=exc.status_code) -
90% 成功率
Register the handler on the specific router or app instance used in tests, not on a different app object.
无效尝试
常见但无效的做法:
-
80% 失败
HTTPException is handled by Starlette's HTTPException handler before generic Exception; order matters and generic handler may not fire.
-
70% 失败
Duplicates logic across every route and bypasses OpenAPI error docs.