Add WithNotFoundHandler option for custom 404 responses on Router
Allows configuring a custom handler for unmatched routes, enabling consistent JSON error responses instead of ServeMux's default plain text. NewRouter now accepts RouterOption functional options. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -12,13 +12,28 @@ type Router struct {
|
|||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
prefix string
|
prefix string
|
||||||
middlewares []Middleware
|
middlewares []Middleware
|
||||||
|
notFoundHandler http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// RouterOption configures a Router.
|
||||||
|
type RouterOption func(*Router)
|
||||||
|
|
||||||
|
// WithNotFoundHandler sets a custom handler for requests that don't match
|
||||||
|
// any registered pattern. This is useful for returning JSON 404/405 responses
|
||||||
|
// instead of the default plain text.
|
||||||
|
func WithNotFoundHandler(h http.Handler) RouterOption {
|
||||||
|
return func(r *Router) { r.notFoundHandler = h }
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRouter creates a new Router backed by a fresh http.ServeMux.
|
// NewRouter creates a new Router backed by a fresh http.ServeMux.
|
||||||
func NewRouter() *Router {
|
func NewRouter(opts ...RouterOption) *Router {
|
||||||
return &Router{
|
r := &Router{
|
||||||
mux: http.NewServeMux(),
|
mux: http.NewServeMux(),
|
||||||
}
|
}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(r)
|
||||||
|
}
|
||||||
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle registers a handler for the given pattern. The pattern follows
|
// Handle registers a handler for the given pattern. The pattern follows
|
||||||
@@ -63,6 +78,14 @@ func (r *Router) Mount(prefix string, handler http.Handler) {
|
|||||||
|
|
||||||
// ServeHTTP implements http.Handler, making Router usable as a handler.
|
// ServeHTTP implements http.Handler, making Router usable as a handler.
|
||||||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if r.notFoundHandler != nil {
|
||||||
|
// Use the mux to check for a match. If none, use the custom handler.
|
||||||
|
_, pattern := r.mux.Handler(req)
|
||||||
|
if pattern == "" {
|
||||||
|
r.notFoundHandler.ServeHTTP(w, req)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
r.mux.ServeHTTP(w, req)
|
r.mux.ServeHTTP(w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
70
server/route_notfound_test.go
Normal file
70
server/route_notfound_test.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package server_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.codelab.vc/pkg/httpx/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRouter_NotFoundHandler(t *testing.T) {
|
||||||
|
t.Run("custom 404 handler", func(t *testing.T) {
|
||||||
|
notFound := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"error": "not found"})
|
||||||
|
})
|
||||||
|
|
||||||
|
r := server.NewRouter(server.WithNotFoundHandler(notFound))
|
||||||
|
r.HandleFunc("GET /exists", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Matched route works normally.
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/exists", nil)
|
||||||
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Code != http.StatusOK {
|
||||||
|
t.Fatalf("matched route: got status %d, want %d", w.Code, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmatched route uses custom handler.
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
req = httptest.NewRequest(http.MethodGet, "/nope", nil)
|
||||||
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Code != http.StatusNotFound {
|
||||||
|
t.Fatalf("not found: got status %d, want %d", w.Code, http.StatusNotFound)
|
||||||
|
}
|
||||||
|
if ct := w.Header().Get("Content-Type"); ct != "application/json" {
|
||||||
|
t.Fatalf("Content-Type = %q, want %q", ct, "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
var body map[string]string
|
||||||
|
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||||
|
t.Fatalf("unmarshal: %v", err)
|
||||||
|
}
|
||||||
|
if body["error"] != "not found" {
|
||||||
|
t.Fatalf("error = %q, want %q", body["error"], "not found")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("default behavior without custom handler", func(t *testing.T) {
|
||||||
|
r := server.NewRouter()
|
||||||
|
r.HandleFunc("GET /exists", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/nope", nil)
|
||||||
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
// Default ServeMux returns 404.
|
||||||
|
if w.Code != http.StatusNotFound {
|
||||||
|
t.Fatalf("got status %d, want %d", w.Code, http.StatusNotFound)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user