Skip to content

Commit 747f590

Browse files
authored
exhttp: add IsNetworkError helper (#29)
1 parent 05fea40 commit 747f590

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

exhttp/networkerror.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package exhttp
2+
3+
import (
4+
"errors"
5+
"net"
6+
"syscall"
7+
8+
"golang.org/x/net/http2"
9+
)
10+
11+
func IsNetworkError(err error) bool {
12+
if errno := syscall.Errno(0); errors.As(err, &errno) {
13+
// common errnos for network related operations
14+
return errno == syscall.ENETDOWN ||
15+
errno == syscall.ENETUNREACH ||
16+
errno == syscall.ENETRESET ||
17+
errno == syscall.ECONNABORTED ||
18+
errno == syscall.ECONNRESET ||
19+
errno == syscall.ENOBUFS ||
20+
errno == syscall.ETIMEDOUT ||
21+
errno == syscall.ECONNREFUSED ||
22+
errno == syscall.EHOSTDOWN ||
23+
errno == syscall.EHOSTUNREACH
24+
} else if netError := net.Error(nil); errors.As(err, &netError) {
25+
return true
26+
} else if errors.As(err, &http2.StreamError{}) {
27+
return true
28+
}
29+
30+
return false
31+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/rs/zerolog v1.34.0
1212
github.com/stretchr/testify v1.10.0
1313
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6
14+
golang.org/x/net v0.40.0
1415
golang.org/x/sys v0.33.0
1516
golang.org/x/text v0.25.0
1617
google.golang.org/protobuf v1.36.6

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
2727
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2828
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 h1:y5zboxd6LQAqYIhHnB48p0ByQ/GnQx2BE33L8BOHQkI=
2929
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6/go.mod h1:U6Lno4MTRCDY+Ba7aCcauB9T60gsv5s4ralQzP72ZoQ=
30+
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
31+
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
3032
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3133
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3234
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)