mirror of https://github.com/gin-gonic/gin.git
fix(tree): Keep panic infos consistent when wildcard type build faild (#4077)
This commit is contained in:
parent
9d11234efe
commit
ea53388e6e
|
@ -26,7 +26,7 @@ jobs:
|
||||||
- name: Setup golangci-lint
|
- name: Setup golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v6
|
uses: golangci/golangci-lint-action@v6
|
||||||
with:
|
with:
|
||||||
version: v1.58.1
|
version: v1.61.0
|
||||||
args: --verbose
|
args: --verbose
|
||||||
test:
|
test:
|
||||||
needs: lint
|
needs: lint
|
||||||
|
|
2
tree.go
2
tree.go
|
@ -369,7 +369,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
|
||||||
|
|
||||||
// currently fixed width 1 for '/'
|
// currently fixed width 1 for '/'
|
||||||
i--
|
i--
|
||||||
if path[i] != '/' {
|
if i < 0 || path[i] != '/' {
|
||||||
panic("no / before catch-all in path '" + fullPath + "'")
|
panic("no / before catch-all in path '" + fullPath + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
25
tree_test.go
25
tree_test.go
|
@ -993,3 +993,28 @@ func TestTreeInvalidEscape(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWildcardInvalidSlash(t *testing.T) {
|
||||||
|
const panicMsgPrefix = "no / before catch-all in path"
|
||||||
|
|
||||||
|
routes := map[string]bool{
|
||||||
|
"/foo/bar": true,
|
||||||
|
"/foo/x*zy": false,
|
||||||
|
"/foo/b*r": false,
|
||||||
|
}
|
||||||
|
|
||||||
|
for route, valid := range routes {
|
||||||
|
tree := &node{}
|
||||||
|
recv := catchPanic(func() {
|
||||||
|
tree.addRoute(route, nil)
|
||||||
|
})
|
||||||
|
|
||||||
|
if recv == nil != valid {
|
||||||
|
t.Fatalf("%s should be %t but got %v", route, valid, recv)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rs, ok := recv.(string); recv != nil && (!ok || !strings.HasPrefix(rs, panicMsgPrefix)) {
|
||||||
|
t.Fatalf(`"Expected panic "%s" for route '%s', got "%v"`, panicMsgPrefix, route, recv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue