-
Notifications
You must be signed in to change notification settings - Fork 915
GODRIVER-2775 Refactor replaceErrors to always wrap errors instead of replacing them. #2113
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-2775 Refactor replaceErrors to always wrap errors instead of replacing them. #2113
Conversation
API Change Report./v2/mongocompatible changesMarshalError.Unwrap: added |
bf769c0
to
374edb5
Compare
374edb5
to
f3e687c
Compare
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! just a note that the PR title is GODRIVER-2774, I believe it should be GODRIVER-2775
question to make sure I understood properly, replaceErrors
is to convert errors defined in x package to the same errors defined in mongo package? also general curiosity why were these errors duplicated/defined in both packages initially?
} | ||
|
||
func (me MarshalError) Unwrap() error { return me.Err } | ||
|
||
// MongocryptError represents an libmongocrypt error during in-use encryption. | ||
type MongocryptError struct { | ||
Code int32 | ||
Message string |
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.
The "Message" field doesn't seem to be used anymore.
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 point, it's not used anymore. Message
and Code
are exported and in a stable package, so we can't remove them. I'll see if I can modify the Error()
logic in a way that makes sense for both if MongocryptError
is used as a wrapper and if it's used as a standalone error in some use case in the future.
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.
Updated to work as both a standalone error and as a wrapper.
@zhouselena The duplication is caused by needing to return errors with additional information (e.g. error code, server message, error labels, etc) but where the error originates from an For example, we want this: package main
import (
"fmt"
"go.mongodb.org/mongo-driver/v2/mongo"
)
func main() {
// ...
_, err := coll.InsertOne(...)
var cerr mongo.CommandError
if errors.As(err, &cerr) {
fmt.Println(cerr.Labels)
}
// ... instead of this: package main
import (
"fmt"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
)
func main() {
// ...
_, err := coll.InsertOne(...)
var derr driver.Error
if errors.As(err, &derr) {
fmt.Println(derr.Labels)
}
// ... However, referencing the We might be able to move those error value definitions to an internal package that both |
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.
ty for the explanation!! lgtm!
GODRIVER-2775
Summary
replaceErrors
towrapErrors
.wrapErrors
to wrap specific error values instead of replacing the top-level error.errors.Is
/errors.As
instead of type assertions, it may match more errors now. The possible effect is that the error may be wrapped in an additional top-level error. Error values are no longer discarded, so there should be no risk that a user'serrors.Is
/errors.As
assertions will starting to fail.Unwrap
methods tomongo.MarshalError
andmongo.MongocryptError
so that the wrapped errors can be inspected usingerrors.Is
/errors.As
.mongo.MarshalError
andmongo.MongocryptError
and always returne the error string from the wrapped error.Wrapped
error to the error string returned bymongo.CommandError
.Background & Motivation
The approach in this PR reduces the likelihood of causing bugs by wrapping error values, which is one of the main goals. However, it doesn't try to answer some of the questions in GODRIVER-2775 or try to establish any new usability patterns for handling errors returned by the Go Driver APIs. We should defer those questions to GODRIVER-3602.