簡體   English   中英

如何在Go中檢查錯誤是否為TLS握手超時

[英]How to check if error is tls handshake timeout in Go

我有以下代碼向URL請求並檢查錯誤。

import "net/http"

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

如何檢查錯誤是否是由於tls握手超時引起的? 我嘗試了以下方法:

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

但是我無法訪問http.tlsHandshakeTimeoutError類型,因為它未導出。 我還能如何檢查go中的錯誤類型?

tlsHandshakeTimeoutError不導出,並且檢查此錯誤的唯一可能性是:

import "net/url"

// ....

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

這是關於它的討論的公開票:

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

順便說一下, http錯誤(還有tlsHandshakeTimeoutError還提供:

type WithTimeout interface {
   Timeout() bool
}

您可以使用它來檢查是否不喜歡字符串比較。 是來自http2包的isTemporary實現的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM