-
Notifications
You must be signed in to change notification settings - Fork 40
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
make Error compatible with functions in standard package #35
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.
LGTM
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
Sorry, I found out that this PR may be somehow incomplete now. Please DNM until I get it clear. |
Signed-off-by: disksing <i@disksing.com>
Signed-off-by: disksing <i@disksing.com>
Signed-off-by: disksing <i@disksing.com>
Added https://github.com/pkg/errors/blob/5dd12d0cfe7f152f80558d591504ce685299311e/errors.go#L162-L163 https://github.com/pkg/errors/blob/5dd12d0cfe7f152f80558d591504ce685299311e/errors.go#L247-L248 |
LGTM |
Signed-off-by: disksing i@disksing.com
The current
Equals
is not friendly to multi-level wrap errors: it always unwraps the bottom-level error and then compares them.For example, if the bottom level returns
os.NotExists
, we cannot wrap it withkv.ErrNotExists.Wrap(err)
, because if we did that, we would not able to determine it later withkv.ErrNotExists.Equals(err)
(the reason isEquals
will recursively unwraps outos.NotExists
then compare)Now we deal this problem by throwing away the underlying error and just return
kv.ErrNotExists
, which is undoubtedly inappropriate because some information is lost and there is no way to tell if the error isos.ErrorNotExists
anymore.Go1.13 introduced the error wrapping mechanism in the standard package (https://golang.org/doc/go1.13#error_wrapping) and provides
errors.Is
/errors.As
functions for error determination.This PR adds two methods to make
Error
compatible with standard error wrapping.Unwrap()
returns the next level of the error chain without recursion, so that Is/As can be checked for each level.Is()
is used to ensure that all instances ofError
with the same ID are judged to be equal.There are 2 cases need to be noted:
Since the ID of both
e2
ande21
are "e2", soerrors.Is(e21, e2)
anderrors.Is(e2, e21)
are bothtrue
.In this case, although
e3x
ande21
have different IDs, the are of the same type (both are*Error
), soAs
returnstrue
ande3x.ID() == "e2"
afterward. Due to limitation oferrors.As
, this issue cannot be avoided by implementing anAs
function forError
.