You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does this issue reproduce with the latest release?
Yes
What did you do?
The issue arrises when trying to compare errors that have not be cached as part of the library.
iferrros.New("foo") !=errors.New("foo") {
// Always executes branch as per the contract of errors.New
}
// Using go 1.13+ error handlingiferrors.Is(errors.New("foo"), errors.New("foo")) {
// Never executed even though they would appear to be the same error
}
What did you expect to see?
In order to keep compatibility with the existing contract provided by the errors package, I would expect the following:
iferrors.New("foo") ==errors.New("foo") {
t.Errorf("Different allocation must not be equal")
}
if!errors.Is(errors.New("foo"), errors.New("foo") {
t.Errorf("Errors made of the same message must be considered the same error")
}
What did you see instead?
Currently errors.New does not implement a means to compare the internally stored error message and requires libraries to define Errs as global variables that can be reassigned at runtime if not careful.
The text was updated successfully, but these errors were encountered:
errors.New intentionally returns unique errors. My “foo” and your “foo” might have different meanings, and so the errors package would be wrong to declare them as equal.
As you noted, the idiomatic way to compare errors is to use a package-level variable or a custom error type.
ianlancetaylor
changed the title
proposal: errors.New returned type to implement errors.Is
proposal: errors: change New to return a type that implements Is
Sep 27, 2021
With a heavy sigh I agree with you @ericlagergren, my frustration when those errors that would be important to capture and match aren't exposed in a means that is easy to do so (beyond string matching which is a different kettle of fish).
I really should argue for better practices in this libraries than trying to hack the src so to speak.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
The issue arrises when trying to compare errors that have not be cached as part of the library.
What did you expect to see?
In order to keep compatibility with the existing contract provided by the errors package, I would expect the following:
What did you see instead?
Currently errors.New does not implement a means to compare the internally stored error message and requires libraries to define Errs as global variables that can be reassigned at runtime if not careful.
The text was updated successfully, but these errors were encountered: