Skip to content

Commit

Permalink
fix(db): endless loop in UnwrapUntilPostgresError (#2010)
Browse files Browse the repository at this point in the history
Ref: SRX-YNEAHU
  • Loading branch information
sven-urbanski-freiheit-com authored Oct 4, 2024
1 parent 0dd8bbd commit c1d056b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
14 changes: 4 additions & 10 deletions pkg/db/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,9 @@ func IsRetryablePostgresError(err error) bool {
}

func UnwrapUntilPostgresError(err error) *pq.Error {
for {
var pqErr *pq.Error
if errors.As(err, &pqErr) {
return pqErr
}
err2 := errors.Unwrap(err)
if err2 == nil {
// cannot unwrap any further
return nil
}
var pqErr *pq.Error
if errors.As(err, &pqErr) {
return pqErr
}
return nil
}
40 changes: 40 additions & 0 deletions pkg/db/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,43 @@ func TestIsNotRetryableError(t *testing.T) {
})
}
}

func TestIsNotRetryableErrorEndlessLoop(t *testing.T) {
tcs := []struct {
InputCode error
ExpectedRetryable bool
}{
{
InputCode: fmt.Errorf("foobar"),
ExpectedRetryable: false,
},
{
InputCode: fmt.Errorf("foobar1: %w", &pq.Error{Code: pq.ErrorCode("54000")}),
ExpectedRetryable: false,
},
{
InputCode: fmt.Errorf("other1: %w", fmt.Errorf("foobar: %w", &pq.Error{Code: pq.ErrorCode("54000")})),
ExpectedRetryable: false,
},
{
InputCode: fmt.Errorf("foobar2: %w", &pq.Error{Code: pq.ErrorCode("40000")}),
ExpectedRetryable: true,
},
{
InputCode: fmt.Errorf("other2: %w", fmt.Errorf("foobar: %w", &pq.Error{Code: pq.ErrorCode("40000")})),
ExpectedRetryable: true,
},
}
for _, tc := range tcs {
tc := tc
t.Run(fmt.Sprintf("endless_loop_check_%s", tc.InputCode.Error()), func(t *testing.T) {
t.Parallel()

actualRetryable := IsRetryablePostgresError(tc.InputCode)

if diff := cmp.Diff(tc.ExpectedRetryable, actualRetryable); diff != "" {
t.Fatalf("error mismatch (-want, +got):\n%s", diff)
}
})
}
}

0 comments on commit c1d056b

Please sign in to comment.