From b8831e566c009173a1bac1955f423c11b5eb9e7e Mon Sep 17 00:00:00 2001 From: tigerwill90 Date: Tue, 26 Nov 2024 18:56:04 +0100 Subject: [PATCH] feat: improve parseRoute error message --- fox.go | 12 +++++++----- fox_test.go | 4 +--- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/fox.go b/fox.go index 306eed0..510e99f 100644 --- a/fox.go +++ b/fox.go @@ -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] != '/' { @@ -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] != '/' { @@ -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++ diff --git a/fox_test.go b/fox_test.go index d9a830c..dc6b72d 100644 --- a/fox_test.go +++ b/fox_test.go @@ -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) @@ -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) }) } diff --git a/go.mod b/go.mod index dccd0d3..7487943 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 30e3023..e740272 100644 --- a/go.sum +++ b/go.sum @@ -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=