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: Export request errors #18596

Closed
stevenosborne-wf opened this issue Jan 10, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@stevenosborne-wf
Copy link

commented Jan 10, 2017

tl;dr

From the net/http package, export errTimeout, errRequestCanceled, and errRequestCanceledConn.

Request timeout

When making an http request with a timeout, the http client.Do throws an unexported errTimeout. This makes parsing the error problematic. For example:

package main

import (
        "context"
        "fmt"
        "net/http"
        "time"
)

func main() {
        client := http.DefaultClient
        request, _ := http.NewRequest("GET", "https://google.com", nil)
        ctx, _ := context.WithTimeout(context.Background(), 1*time.Millisecond)
        request = request.WithContext(ctx)
        if response, err := client.Do(request); err != nil {
                // Untyped error. Have to scrape error message to find out if it's a
                // timeout.
                fmt.Println(err)
        } else {
                fmt.Println(response.StatusCode)
        }
}

It would be safer to export this error. For example:

package main

import (
        "context"
        "fmt"
        "net/http"
        "time"
)

func main() {
        client := http.DefaultClient
        request, _ := http.NewRequest("GET", "https://google.com", nil)
        ctx, _ := context.WithTimeout(context.Background(), 1*time.Millisecond)
        request = request.WithContext(ctx)
        if response, err := client.Do(request); err != nil {
                // Untyped error. Have to scrape error message to find out if it's a
                // timeout.
                if err == http.ErrTimeout {
                        // Retry request with a back off.
                }
        } else {
                fmt.Println(response.StatusCode)
        }
}
@davecheney

This comment has been minimized.

Copy link
Contributor

commented Jan 10, 2017

Assert if the error implements Timeout() and use that.

if err, ok := err.(net.Error); ok {
      if err.Timeout() { 
            // whatever
      }
}
@bradfitz

This comment has been minimized.

Copy link
Member

commented Jan 10, 2017

Dup of #15935, #14203, #13667

@bradfitz bradfitz closed this Jan 10, 2017

@stevenosborne-wf

This comment has been minimized.

Copy link
Author

commented Jan 10, 2017

@bradfitz I did a quick search and couldn't find a similar issue. Thanks for pointing those out.

@stevenosborne-wf

This comment has been minimized.

Copy link
Author

commented Jan 10, 2017

@davecheney Thanks

@golang golang locked and limited conversation to collaborators Jan 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.