feat: add SetNotFoundHandler and SetMethodNotAllowedHandler for custom 404 and 405 responses - #56
Open
qm012 wants to merge 4 commits into
Open
feat: add SetNotFoundHandler and SetMethodNotAllowedHandler for custom 404 and 405 responses#56qm012 wants to merge 4 commits into
qm012 wants to merge 4 commits into
Conversation
qm012
commented
Aug 27, 2026
| // does not populate r.pat/r.matches, so r.Pattern and | ||
| // r.PathValue would be broken for wildcard patterns. | ||
| a.mux.ServeHTTP(w, r) | ||
| return |
Owner
Author
There was a problem hiding this comment.
With custom error handlers set, matched requests now match twice (mux.Handler + mux.ServeHTTP), since Handler doesn't populate r.Pattern/PathValue. I assume that's acceptable as it's slow-path only — the alternative (a WriteHeader-intercepting wrapper) looks like more complexity than it saves. Worth confirming.
Merging this branch will increase overall coverage
Coverage by fileChanged files (no unit tests)
Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code. Changed unit test files
|
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.
related: #38
Adds two setters to
Appfor customizing error responses:SetNotFoundHandler— served when a request matches no registered pattern (404)SetMethodNotAllowedHandler— served when a path matches a pattern but its method does not (405)Both are optional: leaving them unset keeps the mux's default responses, and
ServeHTTPtakes a fast path straight to the mux in that case.Why
http.ServeMuxhas no public hook for its 404/405 handlers (seegolang/go#65648), so apps could not customize
error pages or JSON error bodies without bypassing the mux entirely.
Usage
JSON API errors — return a structured body instead of the mux's plain text:
A single handler can cover both cases if only the status differs:
Notes: