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

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

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

janisz opened this issue Oct 27, 2021 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@janisz
Copy link

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
Member

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

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 27, 2021
@ianlancetaylor
Copy link
Contributor

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

@janisz
Copy link
Author

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

@davecheney
Copy link
Contributor

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.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 12, 2022
@golang golang locked and limited conversation to collaborators Jun 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants