Skip to content
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

net/http: use errors.As when checking for net.Error in Serve #49177

Open
janisz opened this issue Oct 27, 2021 · 4 comments
Open

net/http: use errors.As when checking for net.Error in Serve #49177

janisz opened this issue Oct 27, 2021 · 4 comments

Comments

@janisz
Copy link

@janisz janisz commented Oct 27, 2021

What did you do?

if ne, ok := err.(net.Error); ok && ne.Temporary() {

What did you expect to see?

var ne net.Error
if errors.As(err, &ne) && ne.Temporary() {

What did you see instead?

if ne, ok := err.(net.Error); ok && ne.Temporary() {
@seankhliao seankhliao changed the title Use errors.As instead of casting in http.go net/http: use errors.As when checking for net.Error in Serve Oct 27, 2021
@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Oct 27, 2021

Is there any particular reason this should be checked with errors.As rather than a type assertion?

Loading

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Oct 27, 2021

Unless this solves a bug, let's leave the code alone to avoid churn. Thanks.

Loading

@janisz
Copy link
Author

@janisz janisz commented Oct 27, 2021

It's not a bug but it's surprisingly popular to copy and you can find this line in many repositories
https://sourcegraph.com/search?q=context:global+if+ne%2C+ok+:%3D+err.%28net.Error%29%3B+ok+%26%26+ne.Temporary%28%29+%7B+lang:go&patternType=literal

Loading

@davecheney
Copy link
Contributor

@davecheney davecheney commented Oct 27, 2021

That code isn’t incorrect; it’s asserting the error matches the net.Error interface. A more correct use of errors.As is to assert the error is a specific concrete type, which is not the case here.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants