python runtime_error ai_generated true

FastAPI 异常:404 未找到

fastapi.exceptions.HTTPException: 404 Not Found

ID: python/fastapi-http-exception-handler

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2024-07-08首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

请求的 URL 路径不存在或未定义路由。

English

请求的 URL 路径不存在或未定义路由。

generic

解决方案

  1. 95% 成功率
    @app.exception_handler(404)
    async def not_found_handler(request, exc):
        return JSONResponse(status_code=404, content={'message': 'Not Found'})

无效尝试

常见但无效的做法:

  1. 70% 失败

    会隐藏真正的 404 错误,导致客户端困惑

  2. 90% 失败

    会拦截所有请求,导致其他路由失效