Skip to content

Commit 8ffb7d7

Browse files
committed
wrapping internal error in retry operations
1 parent 793b547 commit 8ffb7d7

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.11.2
2+
* Wrapped internal errors in retry operations
3+
14
## 3.11.1
25
* Excluded error wrapping from retry operations
36

internal/table/retry.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,36 @@ func doTx(ctx context.Context, c SessionProvider, op table.TxOperation, opts ...
9090
options.options.Idempotent,
9191
func(ctx context.Context, s table.Session) (err error) {
9292
tx, err := s.BeginTransaction(ctx, options.options.TxSettings)
93+
if err != nil {
94+
err = errors.Errorf(0, "begin transaction failed: %w", err)
95+
}
96+
9397
defer func() {
9498
if err != nil {
9599
_ = tx.Rollback(ctx)
96100
}
97101
}()
102+
98103
err = op(ctx, tx)
104+
if err != nil {
105+
err = errors.Errorf(0, "retry operation failed: %w", err)
106+
}
107+
99108
if attempts > 0 {
100109
onIntermediate(err)
101110
}
111+
102112
attempts++
113+
103114
if err != nil {
104115
return err
105116
}
117+
106118
_, err = tx.CommitTx(ctx, options.options.TxCommitOptions...)
107119
if err != nil {
108120
err = errors.Errorf(0, "commit failed: %w", err)
109121
}
122+
110123
return err
111124
},
112125
options.trace,
@@ -131,10 +144,16 @@ func do(ctx context.Context, c SessionProvider, op table.Operation, opts ...retr
131144
options.options.Idempotent,
132145
func(ctx context.Context, s table.Session) error {
133146
err = op(ctx, s)
147+
if err != nil {
148+
err = errors.Errorf(0, "retry operation failed: %w", err)
149+
}
150+
134151
if attempts > 0 {
135152
onIntermediate(err)
136153
}
154+
137155
attempts++
156+
138157
return err
139158
},
140159
options.trace,
@@ -250,8 +269,7 @@ func retryBackoff(
250269
}
251270
select {
252271
case <-ctx.Done():
253-
err = ctx.Err()
254-
return
272+
return errors.Errorf(0, "context done: %w", ctx.Err())
255273

256274
default:
257275
if s == nil {
@@ -260,11 +278,14 @@ func retryBackoff(
260278
panic("both of session and error are nil")
261279
}
262280
if err != nil {
263-
return
281+
return errors.Errorf(0, "get session from pool failed: %w", ctx.Err())
264282
}
265283
}
266284

267285
err = op(ctx, s)
286+
if err != nil {
287+
err = errors.Errorf(0, "retry operation failed: %w", ctx.Err())
288+
}
268289

269290
if s.isClosing() {
270291
_ = p.CloseSession(ctx, s)

retry/retry.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,30 @@ func Retry(ctx context.Context, op retryOperation, opts ...retryOption) (err err
126126

127127
default:
128128
err = op(ctx)
129+
if err != nil {
130+
err = errors.Errorf(0, "retry operation failed: %w", err)
131+
}
132+
129133
onDone = onIntermediate(err)
134+
130135
if err == nil {
131136
return
132137
}
138+
133139
m := Check(err)
140+
134141
if m.StatusCode() != code {
135142
i = 0
136143
}
144+
137145
if !m.MustRetry(h.idempotent) {
138146
return
139147
}
148+
140149
if e := Wait(ctx, FastBackoff, SlowBackoff, m, i); e != nil {
141-
return
150+
return errors.Errorf(0, "wait failed, last operation error: %w", err)
142151
}
152+
143153
code = m.StatusCode()
144154
}
145155
}

0 commit comments

Comments
 (0)