-
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
base: master
Are you sure you want to change the base?
GODRIVER-3690 Add ErrorCodesFrom to the mongo package #2241
Conversation
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.
Pull Request Overview
This PR extends the CommandFailedEvent structure to include error codes, addressing GODRIVER-3690. The change enables users to access error codes directly from command failure events without parsing the error themselves.
Key changes:
- Added a
Codesfield toCommandFailedEventto expose error codes - Implemented
errorCodes()helper function to extract codes from driver errors - Added test coverage for the new field
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| event/monitoring.go | Added Codes field to CommandFailedEvent structure |
| x/mongo/driver/operation.go | Populates the new Codes field when publishing failed events; removed blank line |
| x/mongo/driver/errors.go | Implemented errorCodes() function to extract error codes from driver errors |
| mongo/errors.go | Refactored variable declarations to use var() block (style change) |
| internal/integration/clam_prose_test.go | Added test case for CommandFailedEvent.Codes; removed blank line |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
🧪 Performance ResultsCommit SHA: 03fda7aThe following benchmark tests for version 69335dbe9bbda10007c74eb0 had statistically significant changes (i.e., |z-score| > 1.96):
For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch. |
API Change Report./v2/mongocompatible changesErrorCodes: added |
3b7f589 to
63d4781
Compare
63d4781 to
58198cf
Compare
RafaelCenzano
left a comment
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.
LGTM
| var ec interface{ ErrorCodes() []int } | ||
| if errors.As(wrapErrors(err), &ec) { | ||
| return ec.ErrorCodes() |
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.
Can users use errors.As to convert the error to a mongo.ServerError and get the same information?
E.g.
var se mongo.ServerError
if errors.As(err, &se) {
se.ErrorCodes()
}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.
This would only work if mongo.ServerError is in the error tree for the arbitrary error which isn't the case for errors propagated by event monitoring. Here's an example: https://github.com/prestonvasquez/go-playground/blob/01e0e77437bdd9ba0a23d912121e152880dc19e9/mgd_errors_test.go#L17
mongo/errors.go
Outdated
| } | ||
|
|
||
| // ErrorCodesFrom returns the list of server error codes contained in err. | ||
| func ErrorCodesFrom(err error) []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.
Optional: Consider naming this ErrorCodes, which matches the method name on the error types.
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.
Good idea 👍
GODRIVER-3690
Summary
Add an
ErrorCodesfunction that can be used to parse server error codes from an arbitrary Go error.Usage:
Background
Would be convenient to get server status codes without caller taking a dependency on the experimental API. For example, OpenTelemetry needs to include
db.response.status.codefor otelmongo: open-telemetry/opentelemetry-go-contrib#7983 (comment)An alternative to this design would be to put
Codesonevent.CommandFailedEvent. Suggest we forgo this solution for two reasons:event.CommandFailedEvent