Repository navigation
Catch the unknown paths beneath a catch-all's path, such as the API's - #201
Merged
Merged
Conversation
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>
There was a problem hiding this comment.
🟢 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RunServerregistersRegisterCatchAll(prefix), which only matched/apiexactly. 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.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.Test_RegisterCatchAll_Subtreehas 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/unknownand/api/unknown/too, while/and other paths stay the file server's. It fails without the fix.cmd/server:/api/unknownnow gets a JSON 404 rather than plain text.