Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Is comparison on MySQLError #1210

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -23,6 +23,7 @@ Asta Xie <xiemengjun at gmail.com>
Bulat Gaifullin <gaifullinbf at gmail.com>
Caine Jette <jette at alum.mit.edu>
Carlos Nieto <jose.carlos at menteslibres.net>
Chris Kirkland <chriskirkland at github.com>
Chris Moos <chris at tech9computers.com>
Craig Wilson <craiggwilson at gmail.com>
Daniel Montoya <dsmontoyam at gmail.com>
Expand Down
7 changes: 7 additions & 0 deletions errors.go
Expand Up @@ -63,3 +63,10 @@ type MySQLError struct {
func (me *MySQLError) Error() string {
return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
}

func (me *MySQLError) Is(err error) bool {
if merr, ok := err.(*MySQLError); ok {
return merr.Number == me.Number
}
return false
}
21 changes: 21 additions & 0 deletions errors_test.go
Expand Up @@ -5,11 +5,14 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// +build go1.13
chriskirkland marked this conversation as resolved.
Show resolved Hide resolved

package mysql

import (
"bytes"
"errors"
"log"
"testing"
)
Expand Down Expand Up @@ -40,3 +43,21 @@ func TestErrorsStrictIgnoreNotes(t *testing.T) {
dbt.mustExec("DROP TABLE IF EXISTS does_not_exist")
})
}

func TestMySQLErrIs(t *testing.T) {
infraErr := &MySQLError{1234, "the server is on fire"}
otherInfraErr := &MySQLError{1234, "the datacenter is flooded"}
if !errors.Is(infraErr, otherInfraErr) {
t.Errorf("expected errors to be the same: %+v %+v", infraErr, otherInfraErr)
}

differentCodeErr := &MySQLError{5678, "the server is on fire"}
if errors.Is(infraErr, differentCodeErr) {
t.Fatalf("expected errors to be different: %+v %+v", infraErr, differentCodeErr)
}

nonMysqlErr := errors.New("not a mysql error")
if errors.Is(infraErr, nonMysqlErr) {
t.Fatalf("expected errors to be different: %+v %+v", infraErr, nonMysqlErr)
}
}