python runtime_error ai_generated true

fastapi.exceptions.HTTPException: 404 Not Found

ID: python/fastapi-http-exception-handler

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-07-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 90% fail

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