Skip to content

Commit 6f413bc

Browse files
committed
refactor: improve error messages
1 parent b6982df commit 6f413bc

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

pkg/teapot/dispatch.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,20 @@ func (r *Router) Dispatch(method, pattern string, fn func(d *DispatchBuilder, m
7373

7474
// Conflict checks — catch problems early rather than letting chi panic
7575
if _, exists := r.dispatchers[dispatcherKey]; exists {
76-
panic(fmt.Sprintf("teapot: Dispatch conflicts with existing dispatcher for %s %s", method, fullPattern))
76+
panic(fmt.Sprintf(
77+
"teapot: duplicate dispatch registration\n\n"+
78+
" route: %s %s\n"+
79+
" cause: a Dispatch() or query-multiplexed route already owns this method+pattern\n"+
80+
" help: consolidate all variants for this route into a single Dispatch() block",
81+
method, fullPattern))
7782
}
7883
if _, exists := r.directRoutes[dispatcherKey]; exists {
79-
panic(fmt.Sprintf("teapot: Dispatch conflicts with existing direct route for %s %s", method, fullPattern))
84+
panic(fmt.Sprintf(
85+
"teapot: duplicate route registration\n\n"+
86+
" route: %s %s\n"+
87+
" cause: a direct route (GET, POST, etc.) is already registered for this method+pattern\n"+
88+
" help: remove the direct route, or replace both with a single Dispatch() block",
89+
method, fullPattern))
8090
}
8191

8292
db := &DispatchBuilder{
@@ -91,7 +101,12 @@ func (r *Router) Dispatch(method, pattern string, fn func(d *DispatchBuilder, m
91101
// Validate that every route has a handler set
92102
for _, cfg := range db.routes {
93103
if cfg.handler == nil {
94-
panic("teapot: Dispatch route missing handler — call Do() or pass handler to Default()")
104+
panic(fmt.Sprintf(
105+
"teapot: invalid dispatch route %q (%s): no handler configured\n"+
106+
"hint: call Do(...) for this route or provide a handler via Default(...)",
107+
cfg.name,
108+
cfg.action,
109+
))
95110
}
96111
}
97112

pkg/teapot/router.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (rb *RouteBuilder) Name(name string) *RouteBuilder {
5757
if existingName == fullName {
5858
// Same name found - check if it's the same method
5959
if existingRoute.Method == rb.route.Method {
60-
panic(fmt.Sprintf("teapot: duplicate route %s:%s (existing: %s, new: %s)",
60+
panic(fmt.Sprintf("teapot: duplicate route %s:%s pattern(existing: %s, new: %s)",
6161
rb.route.Method, fullName, existingRoute.Pattern, rb.route.Pattern))
6262
}
6363

@@ -528,11 +528,11 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
528528
func (r *Router) URL(name string, params ...string) (string, error) {
529529
rt, exists := r.nameIndex[name]
530530
if !exists {
531-
return "", fmt.Errorf("route %q not found", name)
531+
return "", fmt.Errorf("route %q not found — check that .Name() was called during registration", name)
532532
}
533533

534534
if len(params)%2 != 0 {
535-
return "", fmt.Errorf("params must be key-value pairs")
535+
return "", fmt.Errorf("URL() for route %q: params must be key-value pairs, got %d arg(s)", name, len(params))
536536
}
537537

538538
// Build parameter map

pkg/teapot/validation_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func TestDuplicateRouteName(t *testing.T) {
2020
} else {
2121
msg := r.(string)
2222
asserts.Contains(msg, "duplicate route")
23-
asserts.Contains(msg, "GET:users")
23+
asserts.Contains(msg, "GET")
24+
asserts.Contains(msg, "users")
2425
}
2526
}()
2627

@@ -51,8 +52,9 @@ func TestSameNameDifferentMethodsDifferentPath(t *testing.T) {
5152
t.Error("expected panic for same name with different paths")
5253
} else {
5354
msg := r.(string)
54-
asserts.Contains(msg, "used with different paths")
55+
asserts.Contains(msg, "teapot: route name")
5556
asserts.Contains(msg, "users.show")
57+
asserts.Contains(msg, "used with different paths")
5658
}
5759
}()
5860

0 commit comments

Comments
 (0)