Skip to content

Commit 8bcf34c

Browse files
Merge pull request #400 from pace/fix-connection-error-check
backend/postgres: Handle context error separately
2 parents fe9ad5a + 7bba2ce commit 8bcf34c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

backend/postgres/errors.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package postgres
44

55
import (
6+
"context"
67
"errors"
78
"io"
89
"net"
@@ -13,14 +14,20 @@ import (
1314
var ErrNotUnique = errors.New("not unique")
1415

1516
func IsErrConnectionFailed(err error) bool {
17+
// Context errors are checked separately otherwise they would be considered a network error.
18+
if err == context.DeadlineExceeded || err == context.Canceled {
19+
return false
20+
}
21+
1622
// bun has this check internally for network errors
1723
if errors.Is(err, io.EOF) {
1824
return true
1925
}
2026

27+
var netError net.Error
28+
2129
// bun has this check internally for network errors
22-
_, ok := err.(net.Error)
23-
if ok {
30+
if ok := errors.As(err, &netError); ok {
2431
return true
2532
}
2633

backend/postgres/errors_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ func TestIsErrConnectionFailed(t *testing.T) {
3030
err := errors.New("any other error")
3131
require.False(t, pbpostgres.IsErrConnectionFailed(err))
3232
})
33+
34+
t.Run("context deadline exceeded error", func(t *testing.T) {
35+
err := context.DeadlineExceeded
36+
require.False(t, pbpostgres.IsErrConnectionFailed(err))
37+
})
38+
39+
t.Run("context cancelled error", func(t *testing.T) {
40+
err := context.Canceled
41+
require.False(t, pbpostgres.IsErrConnectionFailed(err))
42+
})
3343
}

0 commit comments

Comments
 (0)