1 回答
TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個贊
您可以檢查錯誤是否與一些已知的錯誤類型兼容。我是這樣做的:
func classifyNetworkError(err error) string {
cause := err
for {
// Unwrap was added in Go 1.13.
// See https://github.com/golang/go/issues/36781
if unwrap, ok := cause.(interface{ Unwrap() error }); ok {
cause = unwrap.Unwrap()
continue
}
break
}
// DNSError.IsNotFound was added in Go 1.13.
// See https://github.com/golang/go/issues/28635
if cause, ok := cause.(*net.DNSError); ok && cause.Err == "no such host" {
return "name not found"
}
if cause, ok := cause.(syscall.Errno); ok {
if cause == 10061 || cause == syscall.ECONNREFUSED {
return "connection refused"
}
}
if cause, ok := cause.(net.Error); ok && cause.Timeout() {
return "timeout"
}
return sprintf("unknown network error: %s", err)
}
- 1 回答
- 0 關(guān)注
- 104 瀏覽
添加回答
舉報
