File tree Expand file tree Collapse file tree 3 files changed +27
-0
lines changed Expand file tree Collapse file tree 3 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ Asta Xie <xiemengjun at gmail.com>
2323Bulat Gaifullin <gaifullinbf at gmail.com>
2424Caine Jette <jette at alum.mit.edu>
2525Carlos Nieto <jose.carlos at menteslibres.net>
26+ Chris Kirkland <chriskirkland at github.com>
2627Chris Moos <chris at tech9computers.com>
2728Craig Wilson <craiggwilson at gmail.com>
2829Daniel Montoya <dsmontoyam at gmail.com>
Original file line number Diff line number Diff line change @@ -63,3 +63,10 @@ type MySQLError struct {
6363func (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+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ package mysql
1010
1111import (
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+ }
You can’t perform that action at this time.
0 commit comments