-
Notifications
You must be signed in to change notification settings - Fork 919
GODRIVER-3690 Add ErrorCodesFrom to the mongo package #2241
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
Changes from all commits
58198cf
03fda7a
f498a53
4696628
0b9a8fd
7ef025a
c71aa78
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 |
|---|---|---|
|
|
@@ -334,6 +334,12 @@ type LabeledError interface { | |
| HasErrorLabel(string) bool | ||
| } | ||
|
|
||
| type errorCoder interface { | ||
| ErrorCodes() []int | ||
| } | ||
|
|
||
| var _ errorCoder = ServerError(nil) | ||
|
|
||
| // 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 { | ||
|
|
@@ -364,10 +370,12 @@ func hasErrorCode(srvErr ServerError, code int) bool { | |
| return false | ||
| } | ||
|
|
||
| var _ ServerError = CommandError{} | ||
| var _ ServerError = WriteError{} | ||
| var _ ServerError = WriteException{} | ||
| var _ ServerError = BulkWriteException{} | ||
| var ( | ||
| _ ServerError = CommandError{} | ||
| _ ServerError = WriteError{} | ||
| _ ServerError = WriteException{} | ||
| _ ServerError = BulkWriteException{} | ||
| ) | ||
|
|
||
| var _ error = ClientBulkWriteException{} | ||
|
|
||
|
|
@@ -901,3 +909,23 @@ func joinBatchErrors(errs []error) string { | |
|
|
||
| return buf.String() | ||
| } | ||
|
|
||
| // ErrorCodes returns the list of server error codes contained in err. | ||
| func ErrorCodes(err error) []int { | ||
| if err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| var ec errorCoder | ||
| // First check if the error is already wrapped (common case) | ||
| if errors.As(err, &ec) { | ||
| return ec.ErrorCodes() | ||
| } | ||
|
|
||
| // Only wrap if necessary (for internal errors) | ||
| if errors.As(wrapErrors(err), &ec) { | ||
|
Collaborator
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. Do you know if
Member
Author
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.
|
||
| return ec.ErrorCodes() | ||
| } | ||
|
|
||
| return []int{} | ||
| } | ||
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.
Do we need to define
interface{ ErrorCodes() []int }here and add additional checks that these structs also satisfies the interface? It may preventErrorCodes()inServerErrorfrom being modified.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.
Nice, will update