python runtime_error ai_generated true

starlette.exceptions.HTTPException: 404 Not Found

ID: python/starlette-http-exception-404

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2024-05-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

A route is not defined for the requested URL, or the URL is incorrect due to routing mismatches.

generic

中文

请求的URL没有定义路由,或由于路由不匹配导致URL错误。

Workarounds

  1. 90% success
    Check app.routes to see registered routes and ensure the path matches exactly, including trailing slashes.
  2. 80% success
    Add a catch-all route: @app.route('/{path:path}')\nasync def catch_all(request):\n    raise HTTPException(status_code=404)

Dead Ends

Common approaches that don't work:

  1. 50% fail

    Catching all HTTPExceptions and returning a custom page can mask other errors like 500s.

  2. 70% fail

    Using regex patterns incorrectly in route paths can cause unexpected 404s.