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: Make it possible to determine if a lookup error is errNoSuchHost #28635

Open
aermakov-zalando opened this Issue Nov 7, 2018 · 10 comments

Comments

Projects
None yet
6 participants
@aermakov-zalando

aermakov-zalando commented Nov 7, 2018

What version of Go are you using (go version)?

$ go version
go version go1.11.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/aermakov/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/aermakov/work/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/f1/4tjxswld76n6zmb02rmn18bnr4l_7q/T/go-build082609694=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I'm trying to determine whether a net.LookupHost fails because the DNS record doesn't exist, or because of other errors. Unfortunately, errNoSuchHost isn't exposed from net and there are no helper functions or methods to determine the type of net.DNSError. This leaves me with no choice other than string parsing, which is extremely brittle.

What did you expect to see?

I would expect to be able to tell noSuchHost errors from other errors, either by comparing with an exported error or by using a method on net.DNSError.

What did you see instead?

It's impossible to determine whether net.LookupHost returned an error the DNS record doesn't exist or any other reasons without resorting to string parsing.

@aermakov-zalando aermakov-zalando changed the title from net: Make it possible to determine if a lookup error is `errNoSuchHost` to net: Make it possible to determine if a lookup error is errNoSuchHost Nov 7, 2018

@agnivade

This comment has been minimized.

Member

agnivade commented Nov 7, 2018

/cc @mikioh

@maksadbek

This comment has been minimized.

maksadbek commented Nov 7, 2018

I see there are 6 uses of unexposed errNoSuchHost in the Go codebase.
See: https://github.com/golang/go/search?q=errNoSuchHost&unscoped_q=errNoSuchHost.

Including the test file where errNoSuchHost is checked with string comparison. See:

if !strings.HasSuffix(err.Error(), errNoSuchHost.Error()) {

IMHO exposing this error variable and refactoring the codebase would be enough.

@affanv14

This comment has been minimized.

affanv14 commented Nov 17, 2018

Hi, I'd like to take this up if it is free

@maksadbek

This comment has been minimized.

maksadbek commented Nov 19, 2018

I've missed that errNoSuchHost is always wrapped into DNSError before it is returned.
Could we add a variable IsNoSuchHost to DNSError struct. Then we could assert returned error to DNSError and check if IsNoSuchHost==true. See https://play.golang.org/p/WyhepLJg2WI

@affanv14

This comment has been minimized.

affanv14 commented Nov 21, 2018

@maksadbek I'm assuming you are taking this up?

@maksadbek

This comment has been minimized.

maksadbek commented Nov 22, 2018

Hi @affanv14
Yes, I'm ready to send a pr if it's ok to use the option I described above.

@affanv14

This comment has been minimized.

affanv14 commented Nov 23, 2018

I'm a new contributor myself, I mistook you for a member, I think you may have done the same! Anyway, I'll leave this up to you. Good luck on the PR!

@szuecs

This comment has been minimized.

szuecs commented Nov 29, 2018

You could also implement „methods“ on top of DNSError, then you don’t need an extra variable and expose the data required.

@maksadbek

This comment has been minimized.

maksadbek commented Nov 30, 2018

@szuecs Do you mean a method that checks prefix of the err text ?

@szuecs

This comment has been minimized.

szuecs commented Nov 30, 2018

Instead of storing it in a variable, you could do this yes.
It was just an idea, no need to do this, but maybe it's not so bad. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment