diff --git a/pkg/regexpool/regexpool.go b/pkg/regexpool/regexpool.go index 6042e3a932..eaf80d7e92 100644 --- a/pkg/regexpool/regexpool.go +++ b/pkg/regexpool/regexpool.go @@ -31,7 +31,7 @@ var ( // Pool is the representation of a pool of regular expression. type Pool struct { cache *memorycache.LRUCache - fails map[string]struct{} + fails map[string]error mu sync.RWMutex } @@ -54,7 +54,7 @@ func NewPool(size int) (*Pool, error) { return nil, err } return &Pool{ - fails: make(map[string]struct{}), + fails: make(map[string]error), cache: cache, }, nil } @@ -69,10 +69,10 @@ func (p *Pool) Get(expr string) (*regexp.Regexp, error) { // Check if the given expression was unable to compile before // then return the error fast. p.mu.RLock() - _, ok := p.fails[expr] + failErr, ok := p.fails[expr] p.mu.RUnlock() if ok { - return nil, fmt.Errorf("unable to compile: %s", expr) + return nil, fmt.Errorf("unable to compile %q: %w", expr, failErr) } // Compile the expression string and cache its result. reg, err := regexp.Compile(expr) @@ -81,7 +81,7 @@ func (p *Pool) Get(expr string) (*regexp.Regexp, error) { return reg, nil } p.mu.Lock() - p.fails[expr] = struct{}{} + p.fails[expr] = err p.mu.Unlock() - return nil, fmt.Errorf("unable to compile: %s", expr) + return nil, fmt.Errorf("unable to compile %q: %w", expr, err) } diff --git a/pkg/regexpool/regexpool_test.go b/pkg/regexpool/regexpool_test.go index dfa6385986..04a621bc1c 100644 --- a/pkg/regexpool/regexpool_test.go +++ b/pkg/regexpool/regexpool_test.go @@ -15,7 +15,6 @@ package regexpool import ( - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -36,6 +35,14 @@ func TestPool(t *testing.T) { assert.NotNil(t, regex) regex, err = pool.Get("(abc") - assert.Equal(t, fmt.Errorf("unable to compile: (abc"), err) + assert.Error(t, err) + assert.Contains(t, err.Error(), `unable to compile "(abc":`) + assert.Contains(t, err.Error(), "error parsing regexp") + assert.Nil(t, regex) + + regex, err = pool.Get("(abc") + assert.Error(t, err) + assert.Contains(t, err.Error(), `unable to compile "(abc":`) + assert.Contains(t, err.Error(), "error parsing regexp") assert.Nil(t, regex) }