Skip to content

Commit

Permalink
Improve err extraction logic
Browse files Browse the repository at this point in the history
The previous regexp had superfluous wildcard matching at the beginning
and end. This results in eagerly matching the whole string and
subsequently matches the last occurance of the match.

Instead we don't want to use the wildcard so the default left to right
matching is used and we get the first occurence.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
  • Loading branch information
dbussink committed Jan 4, 2024
1 parent 7e1afc3 commit 8d8d1d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go/mysql/sqlerror/sql_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (se *SQLError) SQLState() string {
return se.State
}

var errExtract = regexp.MustCompile(`.*\(errno ([0-9]*)\) \(sqlstate ([0-9a-zA-Z]{5})\).*`)
var errExtract = regexp.MustCompile(`\(errno ([0-9]*)\) \(sqlstate ([0-9a-zA-Z]{5})\)`)

// NewSQLErrorFromError returns a *SQLError from the provided error.
// If it's not the right type, it still tries to get it from a regexp.
Expand Down
13 changes: 10 additions & 3 deletions go/mysql/sqlerror/sql_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"

"github.com/stretchr/testify/assert"
)

func TestDemuxResourceExhaustedErrors(t *testing.T) {
Expand Down Expand Up @@ -173,11 +174,17 @@ func TestNewSQLErrorFromError(t *testing.T) {
num: EROutOfResources,
ss: SSUnknownSQLState,
},
{
err: vterrors.Errorf(vtrpc.Code_RESOURCE_EXHAUSTED, "vttablet: rpc error: code = AlreadyExists desc = Duplicate entry '1' for key 'PRIMARY' (errno 1062) (sqlstate 23000) (CallerID: userData1): Sql: \"insert into test(id, `name`) values (:vtg1 /* INT64 */, :vtg2 /* VARCHAR */)\", BindVars: {vtg1: \"type:INT64 value:\\\"1\\\"\"vtg2: \"type:VARCHAR value:\\\"(errno 1366) (sqlstate 10000)\\\"\"}"),
num: ERDupEntry,
ss: SSConstraintViolation,
},
}

for _, tc := range tCases {
t.Run(tc.err.Error(), func(t *testing.T) {
err := NewSQLErrorFromError(tc.err).(*SQLError)
var err *SQLError
require.ErrorAs(t, NewSQLErrorFromError(tc.err), &err)
assert.Equal(t, tc.num, err.Number())
assert.Equal(t, tc.ss, err.SQLState())
})
Expand Down

0 comments on commit 8d8d1d3

Please sign in to comment.