-
-
Notifications
You must be signed in to change notification settings - Fork 654
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
Recommend errors.Is
instead of ==
for testing sentinel errors?
#2013
Comments
The difference in meaning is really subtle. We do not really go into error wrapping in the current exercise so I am not convinced we should change it without also introducing the concept of error wrapping. @junedev? |
There were some discussions around this an the consens was that it is good to also know how to not unwrap so we are teaching this here. Also people need to learn about == since it is still used in a lot of code bases. Since also covering error wrapping properly in this exercise would have been too much content, there will be a separate exercise for this, see issue #1997. @telemachus It would be great if you can leave the comment in that issue that we should explain that if there is no specific not to unwrap, errors.Is is preferred. Just to make sure we don't forget about it then. |
@jmrunkle Maybe I'm confused, but I'm not sure why this is necessary. Even if an error is not wrapped, doesn't To respond to @junedev's point ("people need to learn about == since it is still used in a lot of code bases"), I agree that
This lesson doesn't have to say anything about wrapping, and unless I'm confused all the examples should still work. (If not, I'd love to know since that means I'm wrong about |
@telemachus At work, we have several use cases were we specifically DON'T want to unwrap as it would lead to different behavior. E.g. we have an http error type that, if if would be returned up the chain would lead the server to respond with the enclosed status code. However, in some cases we don't want to forward that code so we wrap the error and then don't unwrap when checking it later, which in our case makes the server return 500 instead of whatever was in the original error. Similar for sentinels, e.g. I would be happy to refer to the error wrapping concept right next to explaining |
@junedev Thanks for the detailed explanation. I hadn't thought of cases where people would not want the unwrapping behavior, so that helps. I'll leave a note on #1997 to edit this exercise after that one is written. |
The current introduction to Errors in Go recommends using
==
to test for sentinel errors, but I think that the community recommends usingerrors.Is
instead.For example, from the docs for
errors
:Would you be interested in a pull request that swapped
errors.Is
for==
? (Or perhaps one that mentions both—since people will see both in code—but recommendserrors.Is
?)The text was updated successfully, but these errors were encountered: