Skip to content

Catch the unknown paths beneath a catch-all's path, such as the API's - #201

Merged
djthorpe merged 1 commit into
mainfrom
djt/1112/catch-all-subtree
Nov 12, 2026
Merged

djthorpe merged 1 commit into
mainfrom
djt/1112/catch-all-subtree

Conversation

@djthorpe

@djthorpe djthorpe commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

RunServer registers RegisterCatchAll(prefix), which only matched /api exactly. An unknown path beneath it, such as /api/unknown, fell through to whatever handled /. When a server serves a UI at / with a file server, that's a plain-text 404 instead of the API's JSON error.

  • The fix: RegisterCatchAll(path) now also registers the subtree beneath the path (/api/), so every unknown path under the API prefix gets a JSON 404. / is still every path, as before.
  • Effect: routes registered beneath the prefix still match first, since they're more specific. Registering the catch-all twice is still fine.
  • Test: Test_RegisterCatchAll_Subtree has a router with a route under /api, a file server at /, and the catch-all. It checks the route, and JSON 404s for /api, /api/, /api/unknown and /api/unknown/too, while / and other paths stay the file server's. It fails without the fix.
  • cmd/server: /api/unknown now gets a JSON 404 rather than plain text.

RegisterCatchAll registered only its path, such as /api, so an unknown
path beneath it, such as /api/unknown, was a file server's plain text 404,
when one served /, rather than JSON. It registers the subtree beneath it,
/api/, too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 16:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the API-prefix catch-all change.

No open issues 🎉

What changed in this PR

Extends the router’s catch-all handling so unknown paths beneath an API prefix return structured JSON 404 responses instead of falling through to a root file server.

Changes:

  • Registers catch-all handlers for both the normalized prefix and its subtree, while preserving repeated registration.
  • Adds regression coverage for known routes, unknown API paths, and file-server responses outside the prefix.
File Description
pkg/​httprouter/​router.go Adds subtree catch-all registration and updates its documentation.
pkg/​httprouter/​router_test.go Tests JSON 404 handling, repeated registration, and root file-server behavior.

Review effort: Balanced

@djthorpe djthorpe self-assigned this Nov 12, 2026
@djthorpe
djthorpe merged commit 19f6878 into main Nov 12, 2026
3 checks passed
@djthorpe
djthorpe deleted the djt/1112/catch-all-subtree branch November 12, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants