Specifically check for timeout error

前端 未结 3 1810
盖世英雄少女心
盖世英雄少女心 2020-12-08 07:10

I use the following to check for timeouts when calling a webservice, but I would like to check specifically if there is a timeout error returned. How do I do that :S

相关标签:
3条回答
  • 2020-12-08 07:33

    As of go1.6, all errors from timeouts should conform to net.Error with Timeout() set properly. All you need to check for is:

    if err, ok := err.(net.Error); ok && err.Timeout() {
    

    In older versions, checking for timeout through the http package was more difficult.

    • You can get a *net.OpError with Timeout() if you hit a Deadline set on the underlying connection.
    • You can get a tlsHandshakeTimeoutError (which is obviously not exported) that implements the net.Error interface.
    • You can get a url.Error, if there was a problem within the url package (timeout during initial connection)
    • You can get an error with "use of closed network connection" if you hit a timeout set with http.Client.Timeout[go1.3+] (which calls Transport.CancelRequest). As of go1.5, this will have the Timeout property set correctly.

    You could check for a net.Error with a type switch:

    switch err := err.(type) {
    case net.Error:
        if err.Timeout() {
            fmt.Println("This was a net.Error with a Timeout")
        }
    case *url.Error:
        fmt.Println("This is a *url.Error")
        if err, ok := err.Err.(net.Error); ok && err.Timeout() {
            fmt.Println("and it was because of a timeout")
        }
    }
    

    With go < 1.5 you will need to check the error string for an http.Client timeout:

    if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
        fmt.Println("Could be from a Transport.CancelRequest")
    }
    
    0 讨论(0)
  • 2020-12-08 07:34

    You want the net.Error interface. http://golang.org/pkg/net/#Error

    if e,ok := err.(net.Error); ok && e.Timeout() {
        // This was a timeout
    } else if err != nil {
        // This was an error, but not a timeout
    }
    

    Note that the type assertion err.(net.Error) will correctly handle the nil case and return false for the ok value if nil is returned as the error, short-circuiting the Timeout check.

    0 讨论(0)
  • 2020-12-08 07:38

    You can simply pass an error to os.IsTimeout() and if it's a timeout returned by net/http then it will return true.

    func IsTimeout(err error) bool

    IsTimeout returns a boolean indicating whether the error is known to report that a timeout occurred.

    0 讨论(0)
提交回复
热议问题