Skip to content

Commit 417641a

Browse files
support Is comparison on MySQLError (go-sql-driver#1210)
* support Is comparison on MySQLError * add myself to authors * skip error tests for go 1.12 * remove test build tag
1 parent e74ba5c commit 417641a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Asta Xie <xiemengjun at gmail.com>
2323
Bulat Gaifullin <gaifullinbf at gmail.com>
2424
Caine Jette <jette at alum.mit.edu>
2525
Carlos Nieto <jose.carlos at menteslibres.net>
26+
Chris Kirkland <chriskirkland at github.com>
2627
Chris Moos <chris at tech9computers.com>
2728
Craig Wilson <craiggwilson at gmail.com>
2829
Daniel Montoya <dsmontoyam at gmail.com>

errors.go

+7
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ type MySQLError struct {
6363
func (me *MySQLError) Error() string {
6464
return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
6565
}
66+
67+
func (me *MySQLError) Is(err error) bool {
68+
if merr, ok := err.(*MySQLError); ok {
69+
return merr.Number == me.Number
70+
}
71+
return false
72+
}

errors_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package mysql
1010

1111
import (
1212
"bytes"
13+
"errors"
1314
"log"
1415
"testing"
1516
)
@@ -40,3 +41,21 @@ func TestErrorsStrictIgnoreNotes(t *testing.T) {
4041
dbt.mustExec("DROP TABLE IF EXISTS does_not_exist")
4142
})
4243
}
44+
45+
func TestMySQLErrIs(t *testing.T) {
46+
infraErr := &MySQLError{1234, "the server is on fire"}
47+
otherInfraErr := &MySQLError{1234, "the datacenter is flooded"}
48+
if !errors.Is(infraErr, otherInfraErr) {
49+
t.Errorf("expected errors to be the same: %+v %+v", infraErr, otherInfraErr)
50+
}
51+
52+
differentCodeErr := &MySQLError{5678, "the server is on fire"}
53+
if errors.Is(infraErr, differentCodeErr) {
54+
t.Fatalf("expected errors to be different: %+v %+v", infraErr, differentCodeErr)
55+
}
56+
57+
nonMysqlErr := errors.New("not a mysql error")
58+
if errors.Is(infraErr, nonMysqlErr) {
59+
t.Fatalf("expected errors to be different: %+v %+v", infraErr, nonMysqlErr)
60+
}
61+
}

0 commit comments

Comments
 (0)