-
Notifications
You must be signed in to change notification settings - Fork 18k
errors: Is(nil) behaves unexpectedly #40442
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
Comments
Perhaps the documentation around the semantics of |
I guess I am wanting a single, simple way to do error handling. I get that people love the Plus, we can finally solve the nil error problem if we push people to use var err error = (*myError)(nil)
switch {
case err == nil:
// false
case errors.Is(err, nil):
// could be true
case errors.Is(err, (*otherError)(nil):
// not totally sure about this one
} |
Just for clarity, can I get a decree on if |
Today Personally I think that changing that would just introduce more complexity and confusion to a somewhat area that is already complex and confusing. So I don't think we should change it. Which is pretty much what @neild said above. So I don't think there is anything to do here. |
Since this is a bug, and we have identified that everything is working as expected. I think we can close this out. Personally, I will transition to exclusively using That being said, I am interested in proposing the two discussed changes: see #40673, #40674, #40675. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
While it is suggested that one use
errors.Is(err, ErrSentinel)
overerr == ErrSentinel
, I am unclear if this translates toerrors.Is(err, nil)
. Assuming it does, and becauseerrors.Is(err, ErrSentinel) == (err == ErrSentinel)
, I would expect that a customIs
method, but notUnwrap
method sincenil
is the signal for this is the base error, could signal that a non-nilerror
interface is not an error: (https://play.golang.org/p/BsSRGGzpxZL)While this example is intentionally contrived for simplicity, I have a use case where I would like to provide consistent debugging information from my custom error, but also allow categories to evaluate to nil.
What did you expect to see?
What did you see instead?
What about Unwrap?
Clearly,
func (err) Unwrap() error { return nil }
cannot implyerrors.Is(err{}, nil) == true
, however I think it is reasonable forfmt.Errorf("doing a thing: %w", nil)
to. We could also exposeerrors.Nil
as:The text was updated successfully, but these errors were encountered: