Skip to content

Commit 7c01a61

Browse files
author
Shabad Kirill
committed
codestyle + changelog
1 parent 30c81f0 commit 7c01a61

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v3.115.1
2+
* Fixed error catching in DoTxWithResult
3+
14
## v3.115.0
25
* Added public package `pkg/xtest` with test helpers
36

retry/sql.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,8 @@ func DoTxWithResult[T any](ctx context.Context, db *sql.DB,
222222
return zeroValue, xerrors.WithStackTrace(err)
223223
}
224224
if err = tx.Commit(); err != nil {
225-
// We create and use tx in this method, so if we catch this error
226-
// it means context cancellation
227-
if xerrors.Is(err, sql.ErrTxDone) {
228-
if ctxErr := ctx.Err(); ctxErr != nil {
229-
return zeroValue, xerrors.WithStackTrace(ctxErr)
230-
}
231-
}
232-
return zeroValue, xerrors.WithStackTrace(err)
225+
// We create and use tx in this method, so if we catch this error, it means context cancellation
226+
return zeroValue, xerrors.WithStackTrace(transformCommitError(ctx, err))
233227
}
234228

235229
return v, nil
@@ -243,6 +237,16 @@ func DoTxWithResult[T any](ctx context.Context, db *sql.DB,
243237
return v, nil
244238
}
245239

240+
func transformCommitError(ctx context.Context, err error) error {
241+
if xerrors.Is(err, sql.ErrTxDone) {
242+
if ctxErr := ctx.Err(); ctxErr != nil {
243+
return ctxErr
244+
}
245+
}
246+
247+
return err
248+
}
249+
246250
func mustDeleteConn[T interface {
247251
*sql.Conn
248252
}](err error, conn T) bool {

retry/sql_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ func TestTxDoneErrorReturnsContextError(t *testing.T) {
310310
attempts++
311311
cancel()
312312
time.Sleep(10 * time.Millisecond)
313+
313314
return 42, nil
314315
},
315316
)
@@ -334,6 +335,7 @@ func TestTxDoneErrorReturnsContextError(t *testing.T) {
334335
attempts++
335336
cancel()
336337
time.Sleep(10 * time.Millisecond)
338+
337339
return nil
338340
},
339341
)

0 commit comments

Comments
 (0)