[英]How to check if error is tls handshake timeout in Go
I have the following code making a request to URL and checking for errors. 我有以下代码向URL请求并检查错误。
import "net/http"
response, err := http.Head("url")
How do check if the error is due to tls handshake timeout? 如何检查错误是否是由于tls握手超时引起的? 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. 但是我无法访问
http.tlsHandshakeTimeoutError
类型,因为它未导出。 How else can I check for the error type in go? 我还能如何检查go中的错误类型?
Yes tlsHandshakeTimeoutError
- is not exported and the only one possibility to check on this error is: 是
tlsHandshakeTimeoutError
不导出,并且检查此错误的唯一可能性是:
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 https://github.com/golang/go/issues/15935
By the way http
errors (and tlsHandshakeTimeoutError
also) provide also: 顺便说一下,
http
错误(还有tlsHandshakeTimeoutError
还提供:
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.
这是来自http2包的isTemporary实现的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.