Get https://www.maps.google.com: dial tcp: lookup www.maps.google.com: no such host
I struggle with adjusting my code to inspect for a ‘no such host’ error. How can I do that?
I learned from the client.Do() doc that returned errors are of type *url.Error. I find that error definition here, and there I also see that the Error() function returns a string. Perhaps I can inspect that one to see for a ‘no such host’ error?
Things I’ve tried include:
if string(err) == url.InvalidHostError() {
log.Println("Host not found.")
}
if err(*url.Error) == url.InvalidHostError() {
log.Println("Host not found.")
}
if strings.Contains(err.(*Error), "no such host") {
log.Println("Host not found.")
}
I’ve Googled, read about error handling, explored the code of url.go, and inspected my copy of The Go programming language. But I don’t feel I understand the topic and get stuck.
But perhaps they want to inspect the error message to provide a more user friendly one?
Many–if not all–clients I have to deal with, will start call their internal IT on an error like that, because it says tcp. Thats network stuff… They won’t even read up until the interesting part.
What I do in those scenarions is to use err.Error() to get the string representation and just let some regular expressions over the stringified error, praying that the structure of the error message will never change.
Thanks everyone for pitching in, much appreciated.
This gets me the error message:
err.Err undefined (type error has no field or method Err)
Which confuses me, because I can see what you mean.
Thanks for idea, that’s a good approach. I’ve adjusted my code and now I do:
request, err := client.Do(req)
if err != nil {
if strings.Contains(err.Error(), "no such host") {
log.Printf("Host not found: %s", path)
} else {
log.Println("Failed to make request.", err)
}
return ""
}
defer request.Body.Close()
This works fine! (Although a bit more precise and going back for a second try, but that’s for later.)
That’s true, good point. But my app hopes to check 1 million domain, and don’t want to check each domain to see what their website address is.