Skip to content

Commit d3c924d

Browse files
committed
GODRIVER-1955 create labeledError interface (#651)
1 parent f28d69f commit d3c924d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

mongo/errors.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func IsTimeout(err error) bool {
106106
return ne.Timeout()
107107
}
108108
//timeout error labels
109-
if se, ok := err.(ServerError); ok {
110-
if se.HasErrorLabel("NetworkTimeoutError") || se.HasErrorLabel("ExceededTimeLimitError") {
109+
if le, ok := err.(labeledError); ok {
110+
if le.HasErrorLabel("NetworkTimeoutError") || le.HasErrorLabel("ExceededTimeLimitError") {
111111
return true
112112
}
113113
}
@@ -130,8 +130,8 @@ func unwrap(err error) error {
130130
// errorHasLabel returns true if err contains the specified label
131131
func errorHasLabel(err error, label string) bool {
132132
for ; err != nil; err = unwrap(err) {
133-
if e, ok := err.(ServerError); ok {
134-
return e.HasErrorLabel(label)
133+
if le, ok := err.(labeledError); ok && le.HasErrorLabel(label) {
134+
return true
135135
}
136136
}
137137
return false
@@ -184,6 +184,12 @@ func (e MongocryptdError) Unwrap() error {
184184
return e.Wrapped
185185
}
186186

187+
type labeledError interface {
188+
error
189+
// HasErrorLabel returns true if the error contains the specified label.
190+
HasErrorLabel(string) bool
191+
}
192+
187193
// ServerError is the interface implemented by errors returned from the server. Custom implementations of this
188194
// interface should not be used in production.
189195
type ServerError interface {

mongo/integration/sdam_error_handling_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestSDAMErrorHandling(t *testing.T) {
8585
_, err := mt.Coll.InsertOne(timeoutCtx, bson.D{{"test", 1}})
8686
assert.NotNil(mt, err, "expected InsertOne error, got nil")
8787
assert.True(mt, mongo.IsTimeout(err), "expected timeout error, got %v", err)
88+
assert.True(mt, mongo.IsNetworkError(err), "expected network error, got %v", err)
8889
assert.True(mt, isPoolCleared(), "expected pool to be cleared but was not")
8990
})
9091
mt.RunOpts("pool cleared on non-timeout network error", noClientOpts, func(mt *mtest.T) {

0 commit comments

Comments
 (0)