From 602fb961b9b2d71fd97f6b54066b96466f23ca7f Mon Sep 17 00:00:00 2001 From: Chris Kirkland Date: Fri, 14 May 2021 16:08:44 -0500 Subject: [PATCH 1/4] support Is comparison on MySQLError --- errors.go | 7 +++++++ errors_test.go | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/errors.go b/errors.go index 760782ff2..92cc9a361 100644 --- a/errors.go +++ b/errors.go @@ -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 +} diff --git a/errors_test.go b/errors_test.go index 96f9126d6..3a1aef74d 100644 --- a/errors_test.go +++ b/errors_test.go @@ -10,6 +10,7 @@ package mysql import ( "bytes" + "errors" "log" "testing" ) @@ -40,3 +41,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) + } +} From b2ff99261cdf8c9d8e119501641c0cd01b311d6b Mon Sep 17 00:00:00 2001 From: Chris Kirkland Date: Fri, 14 May 2021 16:13:53 -0500 Subject: [PATCH 2/4] add myself to authors --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 50afa2c85..3def18bb1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -23,6 +23,7 @@ Asta Xie Bulat Gaifullin Caine Jette Carlos Nieto +Chris Kirkland Chris Moos Craig Wilson Daniel Montoya From 2f2d00f628f55733ade8b2929d42adbbccd9b8e1 Mon Sep 17 00:00:00 2001 From: Chris Kirkland Date: Mon, 17 May 2021 08:45:18 -0500 Subject: [PATCH 3/4] skip error tests for go 1.12 --- errors_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/errors_test.go b/errors_test.go index 3a1aef74d..c423841f6 100644 --- a/errors_test.go +++ b/errors_test.go @@ -5,6 +5,8 @@ // 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 package mysql From 8253d90758e3247844a40d11a52fde863788843c Mon Sep 17 00:00:00 2001 From: Chris Kirkland Date: Thu, 3 Jun 2021 08:22:11 -0500 Subject: [PATCH 4/4] remove test build tag --- errors_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/errors_test.go b/errors_test.go index c423841f6..3a1aef74d 100644 --- a/errors_test.go +++ b/errors_test.go @@ -5,8 +5,6 @@ // 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 package mysql