Skip to content

Commit

Permalink
GODRIVER-1955 create labeledError interface (mongodb#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwysiu authored and tsedgwick committed Jun 1, 2021
1 parent 82dbf47 commit 28b8baf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mongo/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func IsTimeout(err error) bool {
return ne.Timeout()
}
//timeout error labels
if se, ok := err.(ServerError); ok {
if se.HasErrorLabel("NetworkTimeoutError") || se.HasErrorLabel("ExceededTimeLimitError") {
if le, ok := err.(labeledError); ok {
if le.HasErrorLabel("NetworkTimeoutError") || le.HasErrorLabel("ExceededTimeLimitError") {
return true
}
}
Expand All @@ -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 le, ok := err.(labeledError); ok && le.HasErrorLabel(label) {
return true
}
}
return false
Expand Down Expand Up @@ -184,6 +184,12 @@ 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 {
Expand Down
1 change: 1 addition & 0 deletions mongo/integration/sdam_error_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 28b8baf

Please sign in to comment.