Repository navigation
Expand file tree
/
Copy patherror_test.go
More file actions
442 lines (395 loc) · 15 KB
/
Copy patherror_test.go
File metadata and controls
442 lines (395 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package gush
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"
"go.temporal.io/sdk/temporal"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestUnwrapError(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
got := unwrapError(nil)
if got != nil {
t.Errorf("unwrapError(nil) = %v, want nil", got)
}
})
t.Run("plain error returns itself", func(t *testing.T) {
err := errors.New("simple error")
got := unwrapError(err)
if got.Error() != "simple error" {
t.Errorf("unwrapError = %q, want %q", got.Error(), "simple error")
}
})
t.Run("wrapped error returns root cause", func(t *testing.T) {
root := errors.New("root cause")
wrapped := fmt.Errorf("activity error (type: RunJob, scheduledEventID: 5): %w", root)
got := unwrapError(wrapped)
if got.Error() != "root cause" {
t.Errorf("unwrapError = %q, want %q", got.Error(), "root cause")
}
})
t.Run("deeply wrapped error returns root cause", func(t *testing.T) {
root := errors.New("actual program error")
mid := fmt.Errorf("exit status 1: %w", root)
outer := fmt.Errorf("activity error (type: RunJob, scheduledEventID: 5, startedEventID: 6): %w", mid)
got := unwrapError(outer)
if got.Error() != "actual program error" {
t.Errorf("unwrapError = %q, want %q", got.Error(), "actual program error")
}
})
t.Run("ApplicationError includes message and type", func(t *testing.T) {
appErr := NewApplicationError("db connection failed", "DBError")
got := unwrapError(appErr)
want := "[DBError] db connection failed"
if got.Error() != want {
t.Errorf("unwrapError = %q, want %q", got.Error(), want)
}
})
t.Run("wrapped ApplicationError includes message and type", func(t *testing.T) {
appErr := NewApplicationError("db connection failed", "DBError")
wrapped := fmt.Errorf("activity error: %w", appErr)
got := unwrapError(wrapped)
want := "[DBError] db connection failed"
if got.Error() != want {
t.Errorf("unwrapError = %q, want %q", got.Error(), want)
}
})
t.Run("NonRetryableApplicationError includes message and type", func(t *testing.T) {
cause := errors.New("connection refused")
appErr := NewPermanentError("db connection failed", "DBError", cause)
wrapped := fmt.Errorf("activity error (type: RunJob, scheduledEventID: 5): %w", appErr)
got := unwrapError(wrapped)
want := "[DBError] db connection failed"
if got.Error() != want {
t.Errorf("unwrapError = %q, want %q", got.Error(), want)
}
})
t.Run("ApplicationError with empty type omits type suffix", func(t *testing.T) {
appErr := NewApplicationError("something broke", "")
got := unwrapError(appErr)
if got.Error() != "something broke" {
t.Errorf("unwrapError = %q, want %q", got.Error(), "something broke")
}
})
}
func TestUnwrapErrorKeepsApplicationError(t *testing.T) {
t.Run("non-retryable error stays detectable", func(t *testing.T) {
cause := errors.New("boom")
appErr := NewPermanentError("boom", "StepAborted", cause)
wrapped := fmt.Errorf("activity error (type: RunJob, scheduledEventID: 5): %w", appErr)
got := unwrapError(wrapped)
if got.Error() != "[StepAborted] boom" {
t.Errorf("unwrapError = %q, want %q", got.Error(), "[StepAborted] boom")
}
if !IsApplicationError(got) {
t.Error("IsApplicationError = false, want true")
}
if !IsPermanentError(got) {
t.Error("IsPermanentError = false, want true")
}
var target *ApplicationError
if !errors.As(got, &target) || target.Type() != "StepAborted" {
t.Errorf("errors.As found %v, want type StepAborted", target)
}
if !errors.Is(got, cause) {
t.Error("errors.Is(cause) = false, want true")
}
})
t.Run("retryable error stays retryable", func(t *testing.T) {
appErr := NewApplicationError("db connection failed", "DBError")
got := unwrapError(appErr)
if !IsApplicationError(got) {
t.Error("IsApplicationError = false, want true")
}
if IsPermanentError(got) {
t.Error("IsPermanentError = true, want false")
}
})
t.Run("unwrapping twice keeps the same message", func(t *testing.T) {
appErr := NewApplicationError("db connection failed", "DBError")
got := unwrapError(unwrapError(appErr))
if got.Error() != "[DBError] db connection failed" {
t.Errorf("unwrapError = %q, want %q", got.Error(), "[DBError] db connection failed")
}
})
}
func TestTemporalError(t *testing.T) {
t.Run("nil stays nil", func(t *testing.T) {
if got := temporalError(nil); got != nil {
t.Errorf("temporalError(nil) = %v, want nil", got)
}
})
t.Run("unwrapped error returns its ApplicationError", func(t *testing.T) {
appErr := NewPermanentError("boom", "StepAborted", nil)
got := temporalError(unwrapError(appErr))
if got != error(appErr) {
t.Errorf("temporalError = %#v, want the original ApplicationError", got)
}
})
t.Run("other errors are unchanged", func(t *testing.T) {
plain := errors.New("plain")
if got := temporalError(plain); got != plain {
t.Errorf("temporalError = %v, want %v", got, plain)
}
})
}
func TestTemporalErrorKeepsAWrappedErrorsTypeAndRetryability(t *testing.T) {
permanent := NewPermanentError("card declined", "Declined", nil)
retryable := NewApplicationError("try later", "Busy")
tests := []struct {
name string
err error
message string
errType string
nonRetryable bool
}{
{
name: "wrapped permanent error",
err: fmt.Errorf("charge order: %w", permanent),
message: "charge order: card declined",
errType: "Declined",
nonRetryable: true,
},
{
name: "wrapped error from another step",
err: fmt.Errorf("fulfill: %w", unwrapError(permanent)),
message: "fulfill: card declined",
errType: "Declined",
nonRetryable: true,
},
{
name: "wrapped retryable error",
err: fmt.Errorf("charge order: %w", retryable),
message: "charge order: try later",
errType: "Busy",
},
{
name: "wrapped twice",
err: fmt.Errorf("outer: %w", fmt.Errorf("inner: %w", permanent)),
message: "outer: inner: card declined",
errType: "Declined",
nonRetryable: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := temporalError(tt.err)
applicationError, ok := result.(*ApplicationError)
if !ok {
t.Fatalf("temporalError() = %T, want *ApplicationError", result)
}
if applicationError.Message() != tt.message {
t.Errorf("message = %q, want %q", applicationError.Message(), tt.message)
}
if applicationError.Type() != tt.errType {
t.Errorf("type = %q, want %q", applicationError.Type(), tt.errType)
}
if applicationError.NonRetryable() != tt.nonRetryable {
t.Errorf("non-retryable = %v, want %v", applicationError.NonRetryable(), tt.nonRetryable)
}
})
}
}
func TestTemporalErrorLeavesOtherErrorsAlone(t *testing.T) {
plain := errors.New("plain")
if temporalError(plain) != plain {
t.Error("a plain error changed")
}
wrappedPlain := fmt.Errorf("outer: %w", plain)
if temporalError(wrappedPlain) != wrappedPlain {
t.Error("a wrapped plain error changed")
}
var application error = NewApplicationError("boom", "Declined")
if temporalError(application) != application {
t.Error("an ApplicationError changed")
}
}
func TestWrappedErrorKeepsItsDetailsOnTheCause(t *testing.T) {
permanent := NewPermanentError("m", "T", nil, "the-detail")
result := temporalError(fmt.Errorf("wrap: %w", permanent))
cause, ok := errors.Unwrap(result).(*ApplicationError)
if !ok {
t.Fatalf("cause = %T", errors.Unwrap(result))
}
var detail string
if err := cause.Details(&detail); err != nil || detail != "the-detail" {
t.Errorf("detail = %q, error = %v", detail, err)
}
}
func TestAbortKeepsTheMessageReadable(t *testing.T) {
declined := NewApplicationError("declined", "Declined")
rejected := NewApplicationError("rejected", "Rejected")
tests := map[string]struct {
cause error
expected string
}{
"plain error": {cause: errors.New("bad input"), expected: "bad input"},
"application error": {cause: declined, expected: "declined"},
"wrapped application error": {cause: fmt.Errorf("charge: %w", declined), expected: "charge: declined"},
"error returned from RunStep": {cause: unwrapError(declined), expected: "[Declined] declined"},
"application error with a cause": {
cause: NewPermanentError("gateway rejected", "Declined", errors.New("card expired")),
expected: "gateway rejected: card expired",
},
"two wrapped errors": {cause: fmt.Errorf("%w and %w", declined, rejected), expected: "declined and rejected"},
"joined errors": {cause: errors.Join(declined, rejected), expected: "declined\nrejected"},
"timeout": {cause: temporal.NewTimeoutError(enums.TIMEOUT_TYPE_START_TO_CLOSE, nil), expected: "Test timeout"},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
causes := []error{test.cause}
aborted := abortError("StepAborted", causes)
if aborted.Message() != test.expected {
t.Errorf("message = %q, want %q", aborted.Message(), test.expected)
}
if !aborted.NonRetryable() {
t.Errorf("an abort must not be retried")
}
if !errors.Is(aborted, test.cause) {
t.Errorf("the cause is not reachable from %v", aborted)
}
})
}
}
func TestIsFlowRunningErrorIgnoresOtherErrors(t *testing.T) {
for _, err := range []error{nil, errors.New("other"), NewApplicationError("other", "Other")} {
if IsFlowRunningError(err) {
t.Errorf("IsFlowRunningError(%v) = true", err)
}
}
}
func TestResumeError(t *testing.T) {
t.Run("no error stays nil", func(t *testing.T) {
if err := resumeError(nil); err != nil {
t.Errorf("resumeError(nil) = %v", err)
}
})
t.Run("a plain error ends the step", func(t *testing.T) {
got := resumeError(errors.New("rejected"))
if !IsPermanentError(got) {
t.Errorf("resumeError() = %v, want a permanent error", got)
}
if got.Error() == "" || !strings.Contains(got.Error(), "rejected") {
t.Errorf("resumeError() lost the message: %v", got)
}
})
t.Run("an application error keeps its retryability", func(t *testing.T) {
retry := NewApplicationError("ask again", "AskAgain")
if got := resumeError(retry); !IsApplicationError(got) || IsPermanentError(got) {
t.Errorf("resumeError(retryable) = %v", got)
}
wrapped := fmt.Errorf("reviewer: %w", retry)
if got := resumeError(wrapped); !IsApplicationError(got) || IsPermanentError(got) {
t.Errorf("resumeError(wrapped retryable) = %v", got)
}
permanent := NewPermanentError("no", "No", nil)
if got := resumeError(permanent); !IsPermanentError(got) {
t.Errorf("resumeError(permanent) = %v", got)
}
})
t.Run("a canceled error asks to cancel the step", func(t *testing.T) {
canceled := []error{temporal.NewCanceledError(), context.Canceled, fmt.Errorf("review: %w", context.Canceled)}
for _, err := range canceled {
if !isCancellation(err) {
t.Errorf("isCancellation(%v) = false", err)
}
}
other := []error{nil, errors.New("rejected"), NewApplicationError("retry", "Retry")}
for _, err := range other {
if isCancellation(err) {
t.Errorf("isCancellation(%v) = true", err)
}
}
})
}
func TestResumeErrorDropsACanceledCause(t *testing.T) {
permanent := NewPermanentError("rejected", "Rejected", context.Canceled)
wrapped := fmt.Errorf("review: %w", permanent)
options := temporal.ApplicationErrorOptions{Cause: context.Canceled}
retryable := temporal.NewApplicationErrorWithOptions("retry", "Retry", options)
tests := map[string]struct {
err error
message string
permanent bool
}{
"permanent": {permanent, "rejected", true},
"wrapped": {wrapped, "review: rejected", true},
"retryable": {retryable, "retry", false},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := resumeError(test.err)
if errors.Is(got, context.Canceled) || temporal.IsCanceledError(got) {
t.Errorf("resumeError() = %v still reads as canceled", got)
}
var appErr *ApplicationError
if !errors.As(got, &appErr) {
t.Fatalf("resumeError() = %v", got)
}
if appErr.Message() != test.message || appErr.NonRetryable() != test.permanent {
t.Errorf("message = %q, permanent = %v", appErr.Message(), appErr.NonRetryable())
}
})
}
}
func TestResumeErrorKeepsAnotherCause(t *testing.T) {
cause := errors.New("reviewer unavailable")
permanent := NewPermanentError("rejected", "Rejected", cause)
if got := resumeError(permanent); !errors.Is(got, cause) {
t.Errorf("resumeError() = %v lost its cause", got)
}
}
func TestIsFlowRunningError(t *testing.T) {
started := serviceerror.NewWorkflowExecutionAlreadyStarted("running", "", "")
if !IsFlowRunningError(started) {
t.Errorf("IsFlowRunningError(%v) = false", started)
}
wrapped := fmt.Errorf("start: %w", started)
if !IsFlowRunningError(wrapped) {
t.Errorf("IsFlowRunningError(%v) = false", wrapped)
}
// What a flow sees after the error crosses a step, or when it comes from a flow that ran.
acrossAStep := NewApplicationError("running", "WorkflowExecutionAlreadyStarted")
if IsFlowRunningError(acrossAStep) {
t.Errorf("IsFlowRunningError(%v) = true", acrossAStep)
}
if IsFlowRunningError(errors.New("plain")) {
t.Error("IsFlowRunningError(plain) = true")
}
}
func TestIsInvalidConfigError(t *testing.T) {
config := DefaultFlowConfig()
config.Timeout = -1
client := &Client{}
request := FlowRequest{Flow: "AnyFlow"}
_, invalid := client.StartFlow(context.Background(), request, WithConfig(config))
rejected := serviceerror.NewInvalidArgument("the server said no")
// What a flow that failed on its settings looks like once it leaves the flow.
failedFlow := NewPermanentError("invalid config", invalidConfigErrorType, nil)
// A flow that wrapped that failure in an error of its own before returning it.
wrappedByFlow := NewPermanentError("order failed", "OrderFailed", failedFlow)
// An application's own errors, one of them retryable and sharing the reserved type.
sameType := NewApplicationError("settings not loaded yet", invalidConfigErrorType)
applicationType := NewPermanentError("settings not loaded yet", "InvalidConfig", nil)
// What the SDK returns when gRPC refuses a request over its message size limit.
tooLarge := status.Error(codes.ResourceExhausted, "grpc: received message larger than max (5242977 vs. 4194304)")
// A rate limit shares the code and can succeed later.
rateLimited := status.Error(codes.ResourceExhausted, "namespace rate limit exceeded")
recognized := []error{failedFlow, wrappedByFlow, fmt.Errorf("run: %w", failedFlow), invalid, fmt.Errorf("start: %w", invalid), rejected, fmt.Errorf("start: %w", rejected), tooLarge, fmt.Errorf("start: %w", tooLarge)}
for _, err := range recognized {
if !IsInvalidConfigError(err) {
t.Errorf("IsInvalidConfigError(%v) = false", err)
}
}
others := []error{sameType, applicationType, nil, errors.New("some other failure"), serviceerror.NewUnavailable("try later"), rateLimited, serviceerror.NewResourceExhausted(0, "busy")}
for _, err := range others {
if IsInvalidConfigError(err) {
t.Errorf("IsInvalidConfigError(%v) = true", err)
}
}
}