-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherrors.go
35 lines (30 loc) · 1.1 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package netutil
import "strings"
// network errors in golang are difficult to distinguish.
// src/syscall/zerrors_linux_amd64.go has these definitions:
// var errors = [...]string{
// ...
// 101: "network is unreachable",
// ...
// 111: "connection refused",
// 112: "host is down",
// 113: "no route to host",
// ...
// }
//
// The same strings appear in other operating systems / architectures.
//
// As these strings are used to stringify syscall.Errno, we can identify
// class of network errors by using err.Error() string.
// IsNetworkUnreachable returns true if err indicates ENETUNREACH errno.
func IsNetworkUnreachable(err error) bool {
return strings.Contains(err.Error(), "network is unreachable")
}
// IsConnectionRefused returns true if err indicates ECONNREFUSED errno.
func IsConnectionRefused(err error) bool {
return strings.Contains(err.Error(), "connection refused")
}
// IsNoRouteToHost returns true if err indicates EHOSTUNREACH errno.
func IsNoRouteToHost(err error) bool {
return strings.Contains(err.Error(), "no route to host")
}