-
Notifications
You must be signed in to change notification settings - Fork 891
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
GODRIVER-3086 Add ErrorCodes to ServerError API #1894
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,9 +252,23 @@ type ServerError interface { | |
// HasErrorCodeWithMessage returns true if any of the contained errors have the specified code and message. | ||
HasErrorCodeWithMessage(int, string) bool | ||
|
||
// ErrorCodes returns a deduplicated list of error codes returned by the | ||
// server. | ||
ErrorCodes() []int | ||
|
||
serverError() | ||
} | ||
|
||
func hasErrorCode(srvErr ServerError, code int) bool { | ||
for _, srvErrCode := range srvErr.ErrorCodes() { | ||
if code == srvErrCode { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
var _ ServerError = CommandError{} | ||
var _ ServerError = WriteError{} | ||
var _ ServerError = WriteException{} | ||
|
@@ -285,7 +299,12 @@ func (e CommandError) Unwrap() error { | |
|
||
// HasErrorCode returns true if the error has the specified code. | ||
func (e CommandError) HasErrorCode(code int) bool { | ||
return int(e.Code) == code | ||
return hasErrorCode(e, code) | ||
} | ||
|
||
// ErrorCodes returns a list of error codes returned by the server. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’d be good to mention if there’s a defined sort order. |
||
func (e CommandError) ErrorCodes() []int { | ||
return []int{int(e.Code)} | ||
} | ||
|
||
// HasErrorLabel returns true if the error contains the specified label. | ||
|
@@ -340,7 +359,12 @@ func (we WriteError) Error() string { | |
|
||
// HasErrorCode returns true if the error has the specified code. | ||
func (we WriteError) HasErrorCode(code int) bool { | ||
return we.Code == code | ||
return hasErrorCode(we, code) | ||
} | ||
|
||
// ErrorCodes returns a list of error codes returned by the server. | ||
func (we WriteError) ErrorCodes() []int { | ||
return []int{we.Code} | ||
} | ||
|
||
// HasErrorLabel returns true if the error contains the specified label. WriteErrors do not contain labels, | ||
|
@@ -449,15 +473,27 @@ func (mwe WriteException) Error() string { | |
|
||
// HasErrorCode returns true if the error has the specified code. | ||
func (mwe WriteException) HasErrorCode(code int) bool { | ||
if mwe.WriteConcernError != nil && mwe.WriteConcernError.Code == code { | ||
return true | ||
return hasErrorCode(mwe, code) | ||
} | ||
|
||
// ErrorCodes returns a list of error codes returned by the server. | ||
func (mwe WriteException) ErrorCodes() []int { | ||
errorCodeSet := make(map[int]struct{}) | ||
for _, writeError := range mwe.WriteErrors { | ||
errorCodeSet[writeError.Code] = struct{}{} | ||
} | ||
for _, we := range mwe.WriteErrors { | ||
if we.Code == code { | ||
return true | ||
} | ||
|
||
if mwe.WriteConcernError != nil { | ||
errorCodeSet[mwe.WriteConcernError.Code] = struct{}{} | ||
} | ||
return false | ||
|
||
// Deduplicate error codes. | ||
errorCodes := make([]int, 0, len(errorCodeSet)) | ||
for code := range errorCodeSet { | ||
errorCodes = append(errorCodes, code) | ||
} | ||
|
||
return errorCodes | ||
} | ||
|
||
// HasErrorLabel returns true if the error contains the specified label. | ||
|
@@ -561,15 +597,27 @@ func (bwe BulkWriteException) Error() string { | |
|
||
// HasErrorCode returns true if any of the errors have the specified code. | ||
func (bwe BulkWriteException) HasErrorCode(code int) bool { | ||
if bwe.WriteConcernError != nil && bwe.WriteConcernError.Code == code { | ||
return true | ||
return hasErrorCode(bwe, code) | ||
} | ||
|
||
// ErrorCodes returns a list of error codes returned by the server. | ||
func (bwe BulkWriteException) ErrorCodes() []int { | ||
errorCodeSet := make(map[int]struct{}) | ||
for _, writeError := range bwe.WriteErrors { | ||
errorCodeSet[writeError.Code] = struct{}{} | ||
} | ||
for _, we := range bwe.WriteErrors { | ||
if we.Code == code { | ||
return true | ||
} | ||
|
||
if bwe.WriteConcernError != nil { | ||
errorCodeSet[bwe.WriteConcernError.Code] = struct{}{} | ||
} | ||
return false | ||
|
||
// Deduplicate error codes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that it affects us :), but: I see the same logic here and in WriteException’s method. This seems like the sort of thing you’d deduplicate. |
||
errorCodes := make([]int, 0, len(errorCodeSet)) | ||
for code := range errorCodeSet { | ||
errorCodes = append(errorCodes, code) | ||
} | ||
|
||
return errorCodes | ||
} | ||
|
||
// HasErrorLabel returns true if the error contains the specified label. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’d be good to mention if there’s a defined sort order.