go如何准确识别各种网络错误
目录
go 如何准确识别各种网络错误
常见错误
- 无法解析host ip
- TCP无法建立连接
- 读写超时
- ……
返回结果字符串判断
errString := err.Error()
fmt.Println(errString)
switch {
case strings.Contains(errString, "timeout"):
fmt.Println("Timeout")
case strings.Contains(errString, "no such host"):
fmt.Println("Unknown host")
case strings.Contains(errString, "connection refused"):
fmt.Println("Connection refused")
default:
fmt.Printf("Unknown error:%s", errString)
}
这种根据错误信息进行字符串匹配进行判断的方法有非常明显的局限性:该错误信息依赖于操作系统,不同的操作系统对于同一错误返回的字符串信息可能是不同的。因此,这种判断网络错误类型的方法是不可靠的
接口类型断言
go error的接口类型
type error interface {
Error() string
}
go的网络标准库返回的net.Error的接口类型
type Error interface {
error
Timeout() bool // Is the error a timeout?
Temporary() bool // Is the error temporary?
}
net.Error接口类型的具体实现net.OpError
type OpError struct {
// Op is the operation which caused the error, such as
// "dial", "read" or "write".
Op string
// Net is the network type on which this error occurred,
// such as "tcp" or "udp6".
Net string
// For operations involving a remote network connection, like
// Dial, Read, or Write, Source is the corresponding local
// network address.
Source Addr
// Addr is the network address for which this error occurred.
// For local operations, like Listen or SetDeadline, Addr is
// the address of the local endpoint being manipulated.
// For operations involving a remote network connection, like
// Dial, Read, or Write, Addr is the remote address of that
// connection.
Addr Addr
// Err is the error that occurred during the operation.
Err error
}
其中,net.OpError.Err 可能是以下几种类型:
- net.DNSError
- net.InvalidAddrError
- net.UnknownNetworkError
- net.AddrError
- net.DNSConfigError
- *os.SyscallError
因此可用断言式进行判断
func isCaredNetError(err error) bool {
netErr, ok := err.(net.Error)
if !ok {
return false
}
if netErr.Timeout() {
log.Println("timeout")
return true
}
opErr, ok := netErr.(*net.OpError)
if !ok {
return false
}
switch t := opErr.Err.(type) {
case *net.DNSError:
log.Printf("net.DNSError:%+v", t)
return true
case *os.SyscallError:
log.Printf("os.SyscallError:%+v", t)
if errno, ok := t.Err.(syscall.Errno); ok {
switch errno {
case syscall.ECONNREFUSED:
log.Println("connect refused")
return true
case syscall.ETIMEDOUT:
log.Println("timeout")
return true
}
}
}
return false
}
这种错误判定方式除了能解决最开始提到的可靠性和准确性问题,也具有良好的普适性。即基于 net 的其他标准库,如 net/http 也支持这种错误判断方式。