Skip to content

Commit ad78c30

Browse files
committed
exerrors: Add IsNetworkError helper
1 parent fc01e95 commit ad78c30

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

exerrors/network.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2025 Tulir Asokan
2+
//
3+
// This Source Code Form is subject to the terms of the Mozilla Public
4+
// License, v. 2.0. If a copy of the MPL was not distributed with this
5+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
7+
package exerrors
8+
9+
import (
10+
"errors"
11+
"net"
12+
"syscall"
13+
)
14+
15+
func IsNetworkError(err error) bool {
16+
for err != nil {
17+
if _, ok := err.(net.Error); ok {
18+
return true
19+
} else if _, ok := err.(*net.OpError); ok {
20+
return true
21+
} else if errno, ok := err.(syscall.Errno); ok {
22+
// common errnos for network related operations
23+
return errno == syscall.ENETDOWN ||
24+
errno == syscall.ENETUNREACH ||
25+
errno == syscall.ENETRESET ||
26+
errno == syscall.ECONNABORTED ||
27+
errno == syscall.ECONNRESET ||
28+
errno == syscall.ENOBUFS ||
29+
errno == syscall.ETIMEDOUT ||
30+
errno == syscall.ECONNREFUSED ||
31+
errno == syscall.EHOSTDOWN ||
32+
errno == syscall.EHOSTUNREACH
33+
}
34+
35+
err = errors.Unwrap(err)
36+
}
37+
38+
return false
39+
}

0 commit comments

Comments
 (0)