Hot to inspect error of `client.Do()`?

I make requests in my Go program as follows:

// Prepare request
req, err := http.NewRequest("GET", path, nil)
if err != nil {
    log.Fatalln("Error preparing request.", err)
client := &http.Client{Timeout: time.Second * 12}

// Execute request
request, err := client.Do(req)
if err != nil {
    log.Fatalln("Failed to make request.", err)
defer request.Body.Close()

One of the errors I get is:

 Get dial tcp: lookup 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.

Any suggestion is welcome! :slight_smile:

This type

type Error struct {
        Op  string
        URL string
        Err error

has three members Op, URL and Err you could access:


You don’t need to much error inspection. Checked in browser the claimed address does not exists.

**** ’s server IP address could not be found.

Probably you want to say

1 Like

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. :slight_smile:

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! :slight_smile: (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.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.