Skip to content

Commit

Permalink
feat: improve parseRoute error message
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Nov 26, 2024
1 parent ae414e6 commit b8831e5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
12 changes: 7 additions & 5 deletions fox.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,9 @@ func (fox *Router) parseRoute(url string) (uint32, int, error) {
}
inParam = false

if len(url[startParam:i]) > int(fox.maxParamKeyBytes) {
return 0, -1, fmt.Errorf("%w: parameter key too large", ErrInvalidRoute)
paramLen := len(url[startParam:i])
if paramLen > int(fox.maxParamKeyBytes) {
return 0, -1, fmt.Errorf("%w: parameter key too large: max=%d got=%d", ErrInvalidRoute, fox.maxParamKeyBytes, paramLen)
}

if i+1 < len(url) && url[i+1] != delim && url[i+1] != '/' {
Expand Down Expand Up @@ -671,8 +672,9 @@ func (fox *Router) parseRoute(url string) (uint32, int, error) {
}
inParam = false

if len(url[startParam:i]) > int(fox.maxParamKeyBytes) {
return 0, -1, fmt.Errorf("%w: parameter key too large", ErrInvalidRoute)
paramLen := len(url[startParam:i])
if paramLen > int(fox.maxParamKeyBytes) {
return 0, -1, fmt.Errorf("%w: parameter key too large: max=%d got=%d", ErrInvalidRoute, fox.maxParamKeyBytes, paramLen)
}

if i+1 < len(url) && url[i+1] != '/' {
Expand Down Expand Up @@ -753,7 +755,7 @@ func (fox *Router) parseRoute(url string) (uint32, int, error) {
}

if paramCnt > uint32(fox.maxParams) {
return 0, -1, fmt.Errorf("%w: too many params (%d)", ErrInvalidRoute, paramCnt)
return 0, -1, fmt.Errorf("%w: too many params: max=%d got=%d", ErrInvalidRoute, fox.maxParams, paramCnt)
}

i++
Expand Down
4 changes: 1 addition & 3 deletions fox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2891,8 +2891,6 @@ func TestInfixWildcardTsr(t *testing.T) {

tree := f.getRoot()

fmt.Println(tree.root[0])

c := newTestContext(f)
n, tsr := lookupByPath(tree, tree.root[0].children[0], tc.path, c, false)
require.NotNil(t, n)
Expand Down Expand Up @@ -3993,7 +3991,7 @@ func TestParseRouteParamsConstraint(t *testing.T) {
assert.Error(t, err)
_, _, err = f.parseRoute("/{abc}/*{abcd}/{abc}")
assert.Error(t, err)
_, _, err = f.parseRoute("/{abc}/{abc}/*{abcd}")
_, _, err = f.parseRoute("/{abc}/{abc}/*{abcdef}")
assert.Error(t, err)
})
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.0

require (
github.com/google/gofuzz v1.2.0
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
golang.org/x/sys v0.27.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit b8831e5

Please sign in to comment.