duanben1324 2017-08-21 09:11
浏览 640
已采纳

如何在Go中检查错误是否为TLS握手超时

I have the following code making a request to URL and checking for errors.

import "net/http"

response, err := http.Head("url")

How do check if the error is due to tls handshake timeout? I tried the following:

if err != nil {
    tlsError, ok := err.(http.tlsHandshakeTimeoutError)
    if ok {
        // handle the error
    }
}

But I cannot access the http.tlsHandshakeTimeoutError type because it is unexported. How else can I check for the error type in go?

  • 写回答

1条回答 默认 最新

  • dtds8802 2017-08-21 09:46
    关注

    Yes tlsHandshakeTimeoutError - is not exported and the only one possibility to check on this error is:

    import "net/url"
    
    // ....
    
    if urlError,ok :=  err.(*url.Error)  ; ok {
        if urlError.Error() == "net/http: TLS handshake timeout" {
            // handle the error
        }
    }
    

    Here is open ticket with discussion about it:

    https://github.com/golang/go/issues/15935

    By the way http errors (and tlsHandshakeTimeoutError also) provide also:

    type WithTimeout interface {
       Timeout() bool
    }
    

    You can use it for you check if you don't like string comparsion. Here is example of isTemporary implementation from http2 package.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 R语言 拟时序分析降维图如何减少分支
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统