Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend/postgres: Handle context error separately #400

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions backend/postgres/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package postgres

import (
"context"
"errors"
"io"
"net"
Expand All @@ -13,14 +14,20 @@ import (
var ErrNotUnique = errors.New("not unique")

func IsErrConnectionFailed(err error) bool {
// Context errors are checked separately otherwise they would be considered a network error.
if err == context.DeadlineExceeded || err == context.Canceled {
return false
}

// bun has this check internally for network errors
if errors.Is(err, io.EOF) {
return true
}

var netError net.Error

// bun has this check internally for network errors
_, ok := err.(net.Error)
if ok {
if ok := errors.As(err, &netError); ok {
return true
}

Expand Down
10 changes: 10 additions & 0 deletions backend/postgres/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ func TestIsErrConnectionFailed(t *testing.T) {
err := errors.New("any other error")
require.False(t, pbpostgres.IsErrConnectionFailed(err))
})

t.Run("context deadline exceeded error", func(t *testing.T) {
err := context.DeadlineExceeded
require.False(t, pbpostgres.IsErrConnectionFailed(err))
})

t.Run("context cancelled error", func(t *testing.T) {
err := context.Canceled
require.False(t, pbpostgres.IsErrConnectionFailed(err))
})
}