From 06b48e68e333f65921505a8272a643daaf5bbd9d Mon Sep 17 00:00:00 2001 From: iwysiu Date: Mon, 26 Apr 2021 17:10:33 -0400 Subject: [PATCH 1/3] GODRIVER-1955 create labeledError interface --- mongo/errors.go | 16 ++++++++++------ mongo/integration/sdam_error_handling_test.go | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mongo/errors.go b/mongo/errors.go index 5267621274..1f4d974384 100644 --- a/mongo/errors.go +++ b/mongo/errors.go @@ -106,7 +106,7 @@ func IsTimeout(err error) bool { return ne.Timeout() } //timeout error labels - if se, ok := err.(ServerError); ok { + if se, ok := err.(labeledError); ok { if se.HasErrorLabel("NetworkTimeoutError") || se.HasErrorLabel("ExceededTimeLimitError") { return true } @@ -130,8 +130,8 @@ func unwrap(err error) error { // errorHasLabel returns true if err contains the specified label func errorHasLabel(err error, label string) bool { for ; err != nil; err = unwrap(err) { - if e, ok := err.(ServerError); ok { - return e.HasErrorLabel(label) + if e, ok := err.(labeledError); ok && e.HasErrorLabel(label) { + return true } } return false @@ -184,14 +184,18 @@ func (e MongocryptdError) Unwrap() error { return e.Wrapped } +type labeledError interface { + error + // HasErrorLabel returns true if the error contains the specified label. + HasErrorLabel(string) bool +} + // ServerError is the interface implemented by errors returned from the server. Custom implementations of this // interface should not be used in production. type ServerError interface { - error + labeledError // HasErrorCode returns true if the error has the specified code. HasErrorCode(int) bool - // HasErrorLabel returns true if the error contains the specified label. - HasErrorLabel(string) bool // HasErrorMessage returns true if the error contains the specified message. HasErrorMessage(string) bool // HasErrorCodeWithMessage returns true if any of the contained errors have the specified code and message. diff --git a/mongo/integration/sdam_error_handling_test.go b/mongo/integration/sdam_error_handling_test.go index d282be6baf..3ae4ebf6f2 100644 --- a/mongo/integration/sdam_error_handling_test.go +++ b/mongo/integration/sdam_error_handling_test.go @@ -85,6 +85,7 @@ func TestSDAMErrorHandling(t *testing.T) { _, err := mt.Coll.InsertOne(timeoutCtx, bson.D{{"test", 1}}) assert.NotNil(mt, err, "expected InsertOne error, got nil") assert.True(mt, mongo.IsTimeout(err), "expected timeout error, got %v", err) + assert.True(mt, mongo.IsNetworkError(err), "expected network error, got %v", err) assert.True(mt, isPoolCleared(), "expected pool to be cleared but was not") }) mt.RunOpts("pool cleared on non-timeout network error", noClientOpts, func(mt *mtest.T) { From 4d6443e2606c86ce48ff3d546f208cdf00c46681 Mon Sep 17 00:00:00 2001 From: iwysiu Date: Tue, 27 Apr 2021 17:42:48 -0400 Subject: [PATCH 2/3] remove labeledError from public interface --- mongo/errors.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mongo/errors.go b/mongo/errors.go index 1f4d974384..a31c16715c 100644 --- a/mongo/errors.go +++ b/mongo/errors.go @@ -106,8 +106,8 @@ func IsTimeout(err error) bool { return ne.Timeout() } //timeout error labels - if se, ok := err.(labeledError); ok { - if se.HasErrorLabel("NetworkTimeoutError") || se.HasErrorLabel("ExceededTimeLimitError") { + if le, ok := err.(labeledError); ok { + if le.HasErrorLabel("NetworkTimeoutError") || le.HasErrorLabel("ExceededTimeLimitError") { return true } } @@ -130,7 +130,7 @@ func unwrap(err error) error { // errorHasLabel returns true if err contains the specified label func errorHasLabel(err error, label string) bool { for ; err != nil; err = unwrap(err) { - if e, ok := err.(labeledError); ok && e.HasErrorLabel(label) { + if le, ok := err.(labeledError); ok && le.HasErrorLabel(label) { return true } } @@ -193,7 +193,9 @@ type labeledError interface { // ServerError is the interface implemented by errors returned from the server. Custom implementations of this // interface should not be used in production. type ServerError interface { - labeledError + error + // HasErrorLabel returns true if the error contains the specified label. + HasErrorLabel(string) bool // HasErrorCode returns true if the error has the specified code. HasErrorCode(int) bool // HasErrorMessage returns true if the error contains the specified message. From 0c34089c91dd484169527fa98ecf666aa9d78a43 Mon Sep 17 00:00:00 2001 From: iwysiu Date: Wed, 28 Apr 2021 13:20:10 -0400 Subject: [PATCH 3/3] put HasErrorCode back --- mongo/errors.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mongo/errors.go b/mongo/errors.go index a31c16715c..6123635f67 100644 --- a/mongo/errors.go +++ b/mongo/errors.go @@ -194,10 +194,10 @@ type labeledError interface { // interface should not be used in production. type ServerError interface { error - // HasErrorLabel returns true if the error contains the specified label. - HasErrorLabel(string) bool // HasErrorCode returns true if the error has the specified code. HasErrorCode(int) bool + // HasErrorLabel returns true if the error contains the specified label. + HasErrorLabel(string) bool // HasErrorMessage returns true if the error contains the specified message. HasErrorMessage(string) bool // HasErrorCodeWithMessage returns true if any of the contained errors have the specified code and message.