Fix error handling in Router.Walk
In old version error was not returned properly, In one walkFn call error was checked only for SkipRouter but not for nil.
This commit is contained in:
3
mux.go
3
mux.go
@@ -285,6 +285,9 @@ func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
|
||||
if err == SkipRouter {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, sr := range t.matchers {
|
||||
if h, ok := sr.(*Router); ok {
|
||||
err := h.walk(walkFn, ancestors)
|
||||
|
||||
37
mux_test.go
37
mux_test.go
@@ -5,6 +5,7 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -1195,6 +1196,42 @@ func TestWalkNested(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWalkErrorRoute(t *testing.T) {
|
||||
router := NewRouter()
|
||||
router.Path("/g")
|
||||
expectedError := errors.New("error")
|
||||
err := router.Walk(func(route *Route, router *Router, ancestors []*Route) error {
|
||||
return expectedError
|
||||
})
|
||||
if err != expectedError {
|
||||
t.Errorf("Expected %v routes, found %v", expectedError, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWalkErrorMatcher(t *testing.T) {
|
||||
router := NewRouter()
|
||||
expectedError := router.Path("/g").Subrouter().Path("").GetError()
|
||||
err := router.Walk(func(route *Route, router *Router, ancestors []*Route) error {
|
||||
return route.GetError()
|
||||
})
|
||||
if err != expectedError {
|
||||
t.Errorf("Expected %v routes, found %v", expectedError, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWalkErrorHandler(t *testing.T) {
|
||||
handler := NewRouter()
|
||||
expectedError := handler.Path("/path").Subrouter().Path("").GetError()
|
||||
router := NewRouter()
|
||||
router.Path("/g").Handler(handler)
|
||||
err := router.Walk(func(route *Route, router *Router, ancestors []*Route) error {
|
||||
return route.GetError()
|
||||
})
|
||||
if err != expectedError {
|
||||
t.Errorf("Expected %v routes, found %v", expectedError, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubrouterErrorHandling(t *testing.T) {
|
||||
superRouterCalled := false
|
||||
subRouterCalled := false
|
||||
|
||||
Reference in New Issue
Block a user