-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy patherror.go
More file actions
1104 lines (953 loc) · 36.4 KB
/
Copy patherror.go
File metadata and controls
1104 lines (953 loc) · 36.4 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package internal
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
failurepb "go.temporal.io/api/failure/v1"
"go.temporal.io/sdk/converter"
)
/*
If activity fails then *ActivityError is returned to the workflow code. The error has important information about activity
and actual error which caused activity failure. This internal error can be unwrapped using errors.Unwrap() or checked using errors.As().
Below are the possible types of internal error:
1) *ApplicationError: (this should be the most common one)
*ApplicationError can be returned in two cases:
- If activity implementation returns *ApplicationError by using NewApplicationError()/NewNonRetryableApplicationError() API.
The error would contain a message and optional details. Workflow code could extract details to string typed variable, determine
what kind of error it was, and take actions based on it. The details are encoded payload therefore, workflow code needs to know what
the types of the encoded details are before extracting them.
- If activity implementation returns errors other than from NewApplicationError() API. In this case GetOriginalType()
will return original type of error represented as string. Workflow code could check this type to determine what kind of error it was
and take actions based on the type. These errors are retryable by default, unless error type is specified in retry policy.
2) *CanceledError:
If activity was canceled, internal error will be an instance of *CanceledError. When activity cancels itself by
returning NewCancelError() it would supply optional details which could be extracted by workflow code.
3) *TimeoutError:
If activity was timed out (several timeout types), internal error will be an instance of *TimeoutError. The err contains
details about what type of timeout it was.
4) *PanicError:
If activity code panic while executing, temporal activity worker will report it as activity failure to temporal server.
The SDK will present that failure as *PanicError. The error contains a string representation of the panic message and
the call stack when panic was happen.
Workflow code could handle errors based on different types of error. Below is sample code of how error handling looks like.
err := workflow.ExecuteActivity(ctx, MyActivity, ...).Get(ctx, nil)
if err != nil {
var applicationErr *ApplicationError
if errors.As(err, &applicationError) {
// retrieve error message
fmt.Println(applicationError.Error())
// handle activity errors (created via NewApplicationError() API)
var detailMsg string // assuming activity return error by NewApplicationError("message", true, "string details")
applicationErr.Details(&detailMsg) // extract strong typed details
// handle activity errors (errors created other than using NewApplicationError() API)
switch err.Type() {
case "CustomErrTypeA":
// handle CustomErrTypeA
case CustomErrTypeB:
// handle CustomErrTypeB
default:
// newer version of activity could return new errors that workflow was not aware of.
}
}
var canceledErr *CanceledError
if errors.As(err, &canceledErr) {
// handle cancellation
}
var timeoutErr *TimeoutError
if errors.As(err, &timeoutErr) {
// handle timeout, could check timeout type by timeoutErr.TimeoutType()
switch err.TimeoutType() {
case enumspb.TIMEOUT_TYPE_SCHEDULE_TO_START:
// Handle ScheduleToStart timeout.
case enumspb.TIMEOUT_TYPE_START_TO_CLOSE:
// Handle StartToClose timeout.
case enumspb.TIMEOUT_TYPE_HEARTBEAT:
// Handle heartbeat timeout.
default:
}
}
var panicErr *PanicError
if errors.As(err, &panicErr) {
// handle panic, message and stack trace are available by panicErr.Error() and panicErr.StackTrace()
}
}
Errors from child workflow should be handled in a similar way, except that instance of *ChildWorkflowExecutionError is returned to
workflow code. It might contain *ActivityError in case if error comes from activity (which in turn will contain on of the errors above),
or *ApplicationError in case if error comes from child workflow itself.
When panic happen in workflow implementation code, SDK catches that panic and causing the workflow task timeout.
That workflow task will be retried at a later time (with exponential backoff retry intervals).
Workflow consumers will get an instance of *WorkflowExecutionError. This error will contain one of errors above.
*/
type (
// ApplicationErrorOptions represents a combination of error attributes and additional requests.
// All fields are optional, providing flexibility in error customization.
//
// Exposed as: [go.temporal.io/sdk/temporal.ApplicationErrorOptions]
ApplicationErrorOptions struct {
// NonRetryable indicates if the error should not be retried regardless of the retry policy.
NonRetryable bool
// Cause is the original error that caused this error.
Cause error
// Details is a list of arbitrary values that can be used to provide additional context to the error.
Details []interface{}
// NextRetryInterval is a request from server to override retry interval calculated by the
// server according to the RetryPolicy set by the Workflow.
// It is impossible to specify immediate retry as it is indistinguishable from the default value. As a
// workaround you could set NextRetryDelay to some small value.
//
// NOTE: This option is supported by Temporal Server >= v1.24.2 older version will ignore this value.
NextRetryDelay time.Duration
// Category of the error. Maps to logging/metrics behaviors.
Category ApplicationErrorCategory
}
// ApplicationError returned from activity implementations with message and optional details.
//
// Exposed as: [go.temporal.io/sdk/temporal.ApplicationError]
ApplicationError struct {
temporalError
msg string
errType string
nonRetryable bool
cause error
details converter.EncodedValues
nextRetryDelay time.Duration
category ApplicationErrorCategory
}
// TimeoutError returned when activity or child workflow timed out.
//
// Exposed as: [go.temporal.io/sdk/temporal.TimeoutError]
TimeoutError struct {
temporalError
msg string
timeoutType enumspb.TimeoutType
lastHeartbeatDetails converter.EncodedValues
cause error
}
// CanceledErrorOptions should be used to set all the desired attributes of a new CanceledError
//
// Exposed as: [go.temporal.io/sdk/temporal.CanceledErrorOptions]
CanceledErrorOptions struct {
// Message is the error message.
// Defaults to "canceled" if not set.
Message string
// Details is a list of arbitrary values that can be used to provide additional context to the error.
Details []any
// Cause is the original error that caused this error.
Cause error
}
// CanceledError returned when operation was canceled.
//
// Exposed as: [go.temporal.io/sdk/temporal.CanceledError]
CanceledError struct {
temporalError
msg string
cause error
details converter.EncodedValues
}
// TerminatedError returned when workflow was terminated.
//
// Exposed as: [go.temporal.io/sdk/temporal.TerminatedError]
TerminatedError struct {
temporalError
}
// PanicError contains information about panicked workflow/activity.
//
// Exposed as: [go.temporal.io/sdk/temporal.PanicError]
PanicError struct {
temporalError
value interface{}
stackTrace string
}
// workflowPanicError contains information about panicked workflow.
// Used to distinguish go panic in the workflow code from a PanicError returned from a workflow function.
workflowPanicError struct {
value interface{}
stackTrace string
}
// ContinueAsNewError contains information about how to continue the workflow as new.
//
// Exposed as: [go.temporal.io/sdk/workflow.ContinueAsNewError]
ContinueAsNewError struct {
// params *ExecuteWorkflowParams
// WorkflowType is the type of the workflow.
WorkflowType *WorkflowType
// Input is the arguments for the continued workflow execution.
Input *commonpb.Payloads
// Header is the header of the workflow.
Header *commonpb.Header
// TaskQueueName is the task queue that the workflow is running on.
TaskQueueName string
// WorkflowRunTimeout is the timeout for a single run of the workflow execution.
WorkflowRunTimeout time.Duration
// WorkflowTaskTimeout is the maximum execution time of a single Workflow Task.
WorkflowTaskTimeout time.Duration
// Deprecated: WorkflowExecutionTimeout is deprecated and is never set or
// used internally.
WorkflowExecutionTimeout time.Duration
// VersioningIntent specifies whether the continued workflow should run on a worker with a
// compatible build ID or not. See VersioningIntent.
//
// Deprecated: Use Worker Deployment Versioning instead. See https://docs.temporal.io/worker-versioning
VersioningIntent VersioningIntent
// InitialVersioningBehavior specifies the versioning behavior that the first task of the new run should use.
// For example, choose to AutoUpgrade on continue-as-new instead of inheriting the pinned version of the previous run.
// NOTE: Upgrade-on-Continue-as-New is currently experimental.
InitialVersioningBehavior ContinueAsNewVersioningBehavior
// This is by default nil but may be overridden using NewContinueAsNewErrorWithOptions.
// It specifies the retry policy which gets carried over to the next run.
// If not set, the current workflow's retry policy will be carried over automatically.
//
// NOTES:
// 1. This is always nil when returned from a client as a workflow response.
// 2. Unlike other options that can be overridden using WithWorkflowTaskQueue, WithWorkflowRunTimeout, etc.
// we can't introduce an option, say WithWorkflowRetryPolicy, for backward compatibility.
// See #676 or IntegrationTestSuite::TestContinueAsNewWithWithChildWF for more details.
RetryPolicy *RetryPolicy
}
// ContinueAsNewErrorOptions specifies optional attributes to be carried over to the next run.
//
// Exposed as: [go.temporal.io/sdk/workflow.ContinueAsNewErrorOptions]
ContinueAsNewErrorOptions struct {
// RetryPolicy specifies the retry policy to be used for the next run.
// If nil, the current workflow's retry policy will be used.
RetryPolicy *RetryPolicy
// InitialVersioningBehavior specifies the versioning behavior that the first task of the new run should use.
// For example, choose to AutoUpgrade on continue-as-new instead of inheriting the pinned version of the previous run.
// NOTE: Upgrade-on-Continue-as-New is currently experimental.
InitialVersioningBehavior ContinueAsNewVersioningBehavior
}
// UnknownExternalWorkflowExecutionError can be returned when external workflow doesn't exist
UnknownExternalWorkflowExecutionError struct{}
// ServerError can be returned from server.
//
// Exposed as: [go.temporal.io/sdk/temporal.ServerError]
ServerError struct {
temporalError
msg string
nonRetryable bool
cause error
}
// ActivityError is returned from workflow when activity returned an error.
// Unwrap this error to get actual cause.
//
// Exposed as: [go.temporal.io/sdk/temporal.ActivityError]
ActivityError struct {
temporalError
scheduledEventID int64
startedEventID int64
identity string
activityType *commonpb.ActivityType
activityID string
retryState enumspb.RetryState
cause error
}
// ChildWorkflowExecutionError is returned from workflow when child workflow returned an error.
// Unwrap this error to get actual cause.
//
// Exposed as: [go.temporal.io/sdk/temporal.ChildWorkflowExecutionError]
ChildWorkflowExecutionError struct {
temporalError
namespace string
workflowID string
runID string
workflowType string
initiatedEventID int64
startedEventID int64
retryState enumspb.RetryState
cause error
}
// NexusOperationError is an error returned when a Nexus Operation has failed.
//
// Exposed as: [go.temporal.io/sdk/temporal.NexusOperationError]
NexusOperationError struct {
// The raw proto failure object this error was created from.
Failure *failurepb.Failure
// Error message.
Message string
// ID of the NexusOperationScheduled event.
ScheduledEventID int64
// Endpoint name.
Endpoint string
// Service name.
Service string
// Operation name.
Operation string
// Operation token - may be empty if the operation completed synchronously.
OperationToken string
// Chained cause - typically an ApplicationError or a CanceledError.
Cause error
}
// ChildWorkflowExecutionAlreadyStartedError is set as the cause of
// ChildWorkflowExecutionError when failure is due the child workflow having
// already started.
ChildWorkflowExecutionAlreadyStartedError struct{}
// NamespaceNotFoundError is set as the cause when failure is due namespace not found.
NamespaceNotFoundError struct{}
// WorkflowExecutionError is returned from workflow.
// Unwrap this error to get actual cause.
//
// Exposed as: [go.temporal.io/sdk/temporal.WorkflowExecutionError]
WorkflowExecutionError struct {
workflowID string
runID string
workflowType string
cause error
}
// ActivityNotRegisteredError is returned if worker doesn't support activity type.
ActivityNotRegisteredError struct {
activityType string
supportedTypes []string
}
temporalError struct {
messenger
originalFailure *failurepb.Failure
}
failureHolder interface {
setFailure(*failurepb.Failure)
failure() *failurepb.Failure
}
messenger interface {
message() string
}
)
var (
// Should be "errorString".
goErrType = reflect.TypeOf(errors.New("")).Elem().Name()
// ErrNoData is returned when trying to extract strong typed data while there is no data available.
//
// Exposed as: [go.temporal.io/sdk/temporal.ErrNoData]
ErrNoData = errors.New("no data available")
// ErrTooManyArg is returned when trying to extract strong typed data with more arguments than available data.
ErrTooManyArg = errors.New("too many arguments")
// ErrActivityResultPending is returned from activity's implementation to indicate the activity is not completed when
// activity method returns. Activity needs to be completed by Client.CompleteActivity() separately. For example, if an
// activity require human interaction (like approve an expense report), the activity could return activity.ErrResultPending
// which indicate the activity is not done yet. Then, when the waited human action happened, it needs to trigger something
// that could report the activity completed event to temporal server via Client.CompleteActivity() API.
//
// Exposed as: [go.temporal.io/sdk/activity.ErrResultPending]
ErrActivityResultPending = errors.New("not error: do not autocomplete, using Client.CompleteActivity() to complete")
// ErrScheduleAlreadyRunning is returned if there's already a running (not deleted) Schedule with the same ID
//
// Exposed as: [go.temporal.io/sdk/temporal.ErrScheduleAlreadyRunning]
ErrScheduleAlreadyRunning = errors.New("schedule with this ID is already registered")
// ErrSkipScheduleUpdate is used by a user if they want to skip updating a schedule.
//
// Exposed as: [go.temporal.io/sdk/temporal.ErrSkipScheduleUpdate]
ErrSkipScheduleUpdate = errors.New("skip schedule update")
// ErrMissingWorkflowID is returned when trying to start an async Nexus operation but no workflow ID is set on the request.
ErrMissingWorkflowID = errors.New("workflow ID is unset for Nexus operation")
)
// ApplicationErrorCategory sets the category of the error. The category of the error
// maps to logging/metrics behaviors.
//
// Exposed as: [go.temporal.io/sdk/temporal.ApplicationErrorCategory]
type ApplicationErrorCategory int
const (
// ApplicationErrorCategoryUnspecified represents an error with an unspecified category.
//
// Exposed as: [go.temporal.io/sdk/temporal.ApplicationErrorCategoryUnspecified]
ApplicationErrorCategoryUnspecified ApplicationErrorCategory = iota
// ApplicationErrorCategoryBenign indicates an error that is expected under normal operation and should not trigger alerts.
//
// Exposed as: [go.temporal.io/sdk/temporal.ApplicationErrorCategoryBenign]
ApplicationErrorCategoryBenign
)
// NewApplicationError create new instance of *ApplicationError with message, type, and optional details.
func NewApplicationError(msg string, errType string, nonRetryable bool, cause error, details ...interface{}) error {
return NewApplicationErrorWithOptions(
msg,
errType,
ApplicationErrorOptions{NonRetryable: nonRetryable, Cause: cause, Details: details},
)
}
// Exposed as: [go.temporal.io/sdk/temporal.NewApplicationError], [go.temporal.io/sdk/temporal.NewApplicationErrorWithOptions], [go.temporal.io/sdk/temporal.NewApplicationErrorWithCause], [go.temporal.io/sdk/temporal.NewNonRetryableApplicationError]
func NewApplicationErrorWithOptions(msg string, errType string, options ApplicationErrorOptions) error {
applicationErr := &ApplicationError{
msg: msg,
errType: errType,
cause: options.Cause,
nonRetryable: options.NonRetryable,
nextRetryDelay: options.NextRetryDelay,
category: options.Category,
}
// When return error to user, use EncodedValues as details and data is ready to be decoded by calling Get
details := options.Details
if len(details) == 1 {
if d, ok := details[0].(*EncodedValues); ok {
applicationErr.details = d
return applicationErr
}
}
// When create error for server, use ErrorDetailsValues as details to hold values and encode later
applicationErr.details = ErrorDetailsValues(details)
return applicationErr
}
// NewTimeoutError creates TimeoutError instance.
// Use NewHeartbeatTimeoutError to create heartbeat TimeoutError.
//
// Exposed as: [go.temporal.io/sdk/temporal.NewTimeoutError]
func NewTimeoutError(msg string, timeoutType enumspb.TimeoutType, cause error, lastHeartbeatDetails ...interface{}) error {
timeoutErr := &TimeoutError{
msg: msg,
timeoutType: timeoutType,
cause: cause,
}
if len(lastHeartbeatDetails) == 1 {
if d, ok := lastHeartbeatDetails[0].(*EncodedValues); ok {
timeoutErr.lastHeartbeatDetails = d
return timeoutErr
}
}
timeoutErr.lastHeartbeatDetails = ErrorDetailsValues(lastHeartbeatDetails)
return timeoutErr
}
// NewHeartbeatTimeoutError creates TimeoutError instance.
//
// Exposed as: [go.temporal.io/sdk/temporal.NewHeartbeatTimeoutError]
func NewHeartbeatTimeoutError(details ...interface{}) error {
return NewTimeoutError("heartbeat timeout", enumspb.TIMEOUT_TYPE_HEARTBEAT, nil, details...)
}
// NewCanceledError creates CanceledError instance.
//
// Exposed as: [go.temporal.io/sdk/temporal.NewCanceledError]
func NewCanceledError(details ...interface{}) error {
return NewCanceledErrorWithOptions(CanceledErrorOptions{
Details: details,
})
}
// NewCanceledErrorWithOptions creates CanceledError instance.
//
// Exposed as: [go.temporal.io/sdk/temporal.NewCanceledErrorWithOptions]
func NewCanceledErrorWithOptions(options CanceledErrorOptions) error {
msg := options.Message
if msg == "" {
msg = "canceled"
}
if len(options.Details) == 1 {
if d, ok := options.Details[0].(*EncodedValues); ok {
return &CanceledError{
msg: msg,
details: d,
cause: options.Cause,
}
}
}
return &CanceledError{
msg: msg,
details: ErrorDetailsValues(options.Details),
cause: options.Cause,
}
}
// NewServerError create new instance of *ServerError with message.
func NewServerError(msg string, nonRetryable bool, cause error) error {
return &ServerError{msg: msg, nonRetryable: nonRetryable, cause: cause}
}
// NewActivityError creates ActivityError instance.
func NewActivityError(
scheduledEventID int64,
startedEventID int64,
identity string,
activityType *commonpb.ActivityType,
activityID string,
retryState enumspb.RetryState,
cause error,
) *ActivityError {
return &ActivityError{
scheduledEventID: scheduledEventID,
startedEventID: startedEventID,
identity: identity,
activityType: activityType,
activityID: activityID,
retryState: retryState,
cause: cause,
}
}
// NewChildWorkflowExecutionError creates ChildWorkflowExecutionError instance.
func NewChildWorkflowExecutionError(
namespace string,
workflowID string,
runID string,
workflowType string,
initiatedEventID int64,
startedEventID int64,
retryState enumspb.RetryState,
cause error,
) *ChildWorkflowExecutionError {
return &ChildWorkflowExecutionError{
namespace: namespace,
workflowID: workflowID,
runID: runID,
workflowType: workflowType,
initiatedEventID: initiatedEventID,
startedEventID: startedEventID,
retryState: retryState,
cause: cause,
}
}
// NewWorkflowExecutionError creates WorkflowExecutionError instance.
func NewWorkflowExecutionError(
workflowID string,
runID string,
workflowType string,
cause error,
) *WorkflowExecutionError {
return &WorkflowExecutionError{
workflowID: workflowID,
runID: runID,
workflowType: workflowType,
cause: cause,
}
}
func (e *temporalError) setFailure(f *failurepb.Failure) {
e.originalFailure = f
}
func (e *temporalError) failure() *failurepb.Failure {
return e.originalFailure
}
// IsCanceledError returns whether error in CanceledError.
func IsCanceledError(err error) bool {
var canceledErr *CanceledError
return errors.As(err, &canceledErr)
}
// NewContinueAsNewError creates ContinueAsNewError instance
// If the workflow main function returns this error then the current execution is ended and
// the new execution with same workflow ID is started automatically with options
// provided to this function.
//
// ctx - use context to override any options for the new workflow like run timeout, task timeout, task queue.
// if not mentioned it would use the defaults that the current workflow is using.
// ctx := WithWorkflowRunTimeout(ctx, 30 * time.Minute)
// ctx := WithWorkflowTaskTimeout(ctx, 5 * time.Second)
// ctx := WithWorkflowTaskQueue(ctx, "example-group")
// wfn - workflow function. for new execution it can be different from the currently running.
// args - arguments for the new workflow.
//
// Exposed as: [go.temporal.io/sdk/workflow.NewContinueAsNewError]
func NewContinueAsNewError(ctx Context, wfn interface{}, args ...interface{}) error {
i := getWorkflowOutboundInterceptor(ctx)
// Put header on context before executing
ctx = workflowContextWithNewHeader(ctx)
return i.NewContinueAsNewError(ctx, wfn, args...)
}
// NewContinueAsNewErrorWithOptions creates ContinueAsNewError instance with additional options.
//
// Exposed as: [go.temporal.io/sdk/workflow.NewContinueAsNewErrorWithOptions]
func NewContinueAsNewErrorWithOptions(ctx Context, options ContinueAsNewErrorOptions, wfn interface{}, args ...interface{}) error {
err := NewContinueAsNewError(ctx, wfn, args...)
var continueAsNewErr *ContinueAsNewError
if errors.As(err, &continueAsNewErr) {
if options.RetryPolicy != nil {
continueAsNewErr.RetryPolicy = options.RetryPolicy
}
continueAsNewErr.InitialVersioningBehavior = options.InitialVersioningBehavior
}
return err
}
func (wc *workflowEnvironmentInterceptor) NewContinueAsNewError(
ctx Context,
wfn interface{},
args ...interface{},
) error {
// Validate type and its arguments.
options := getWorkflowEnvOptions(ctx)
if options == nil {
panic("context is missing required options for continue as new")
}
env := getWorkflowEnvironment(ctx)
dc := getDataConverterFromWorkflowContext(ctx)
workflowType, input, err := getValidatedWorkflowFunction(wfn, args, dc, env.GetRegistry())
if err != nil {
panic(err)
}
header, err := workflowHeaderPropagated(ctx, options.ContextPropagators)
if err != nil {
return err
}
return &ContinueAsNewError{
WorkflowType: workflowType,
Input: input,
Header: header,
TaskQueueName: options.TaskQueueName,
WorkflowExecutionTimeout: options.WorkflowExecutionTimeout,
WorkflowRunTimeout: options.WorkflowRunTimeout,
WorkflowTaskTimeout: options.WorkflowTaskTimeout,
VersioningIntent: options.VersioningIntent,
RetryPolicy: nil, // The retry policy can't be propagated like other options due to #676.
InitialVersioningBehavior: options.InitialVersioningBehavior,
}
}
// NewActivityNotRegisteredError creates a new ActivityNotRegisteredError.
func NewActivityNotRegisteredError(activityType string, supportedTypes []string) error {
return &ActivityNotRegisteredError{activityType: activityType, supportedTypes: supportedTypes}
}
// Error from error interface.
func (e *ApplicationError) Error() string {
msg := e.message()
if e.errType != "" {
msg = fmt.Sprintf("%s (type: %s, retryable: %v)", msg, e.errType, !e.nonRetryable)
}
if e.cause != nil {
msg = fmt.Sprintf("%s: %v", msg, e.cause)
}
return msg
}
func (e *ApplicationError) message() string {
return e.msg
}
// Message contains just the message string without extras added by Error().
func (e *ApplicationError) Message() string {
return e.msg
}
// Type returns error type represented as string.
// This type can be passed explicitly to ApplicationError constructor.
// Also any other Go error is converted to ApplicationError and type is set automatically using reflection.
// For example instance of "MyCustomError struct" will be converted to ApplicationError and Type() will return "MyCustomError" string.
func (e *ApplicationError) Type() string {
return e.errType
}
// HasDetails return if this error has strong typed detail data.
func (e *ApplicationError) HasDetails() bool {
return e.details != nil && e.details.HasValues()
}
// Details extracts strong typed detail data of this custom error. If there is no details, it will return ErrNoData.
func (e *ApplicationError) Details(d ...interface{}) error {
if !e.HasDetails() {
return ErrNoData
}
return e.details.Get(d...)
}
// NonRetryable indicated if error is not retryable.
func (e *ApplicationError) NonRetryable() bool {
return e.nonRetryable
}
func (e *ApplicationError) Unwrap() error {
return e.cause
}
// NextRetryDelay returns the delay to wait before retrying the activity.
// a zero value means to use the activities retry policy.
func (e *ApplicationError) NextRetryDelay() time.Duration { return e.nextRetryDelay }
// Category returns the ApplicationErrorCategory of the error.
func (e *ApplicationError) Category() ApplicationErrorCategory {
return e.category
}
// Error from error interface
func (e *TimeoutError) Error() string {
msg := fmt.Sprintf("%s (type: %s)", e.message(), e.timeoutType)
if e.cause != nil {
msg = fmt.Sprintf("%s: %v", msg, e.cause)
}
return msg
}
func (e *TimeoutError) message() string {
return e.msg
}
// Message contains just the message string without extras added by Error().
func (e *TimeoutError) Message() string {
return e.msg
}
func (e *TimeoutError) Unwrap() error {
return e.cause
}
// TimeoutType return timeout type of this error
func (e *TimeoutError) TimeoutType() enumspb.TimeoutType {
return e.timeoutType
}
// HasLastHeartbeatDetails return if this error has strong typed detail data.
func (e *TimeoutError) HasLastHeartbeatDetails() bool {
return e.lastHeartbeatDetails != nil && e.lastHeartbeatDetails.HasValues()
}
// LastHeartbeatDetails extracts strong typed detail data of this error. If there is no details, it will return ErrNoData.
func (e *TimeoutError) LastHeartbeatDetails(d ...interface{}) error {
if !e.HasLastHeartbeatDetails() {
return ErrNoData
}
return e.lastHeartbeatDetails.Get(d...)
}
// Error from error interface
func (e *CanceledError) Error() string {
return e.message()
}
func (e *CanceledError) message() string {
return e.msg
}
func (e *CanceledError) Unwrap() error {
return e.cause
}
// HasDetails return if this error has strong typed detail data.
func (e *CanceledError) HasDetails() bool {
return e.details != nil && e.details.HasValues()
}
// Details extracts strong typed detail data of this error.
func (e *CanceledError) Details(d ...interface{}) error {
if !e.HasDetails() {
return ErrNoData
}
return e.details.Get(d...)
}
func newPanicError(value interface{}, stackTrace string) error {
return &PanicError{value: value, stackTrace: stackTrace}
}
func newWorkflowPanicError(value interface{}, stackTrace string) error {
return &workflowPanicError{value: value, stackTrace: stackTrace}
}
// Error from error interface
func (e *PanicError) Error() string {
return e.message()
}
func (e *PanicError) message() string {
return fmt.Sprintf("%v", e.value)
}
// StackTrace return stack trace of the panic
func (e *PanicError) StackTrace() string {
return e.stackTrace
}
// Error from error interface
func (e *workflowPanicError) Error() string {
return fmt.Sprintf("%v", e.value)
}
// StackTrace return stack trace of the panic
func (e *workflowPanicError) StackTrace() string {
return e.stackTrace
}
// Error from error interface
func (e *ContinueAsNewError) Error() string {
return e.message()
}
func (e *ContinueAsNewError) message() string {
return "continue as new"
}
// newTerminatedError creates NewTerminatedError instance
func newTerminatedError() *TerminatedError {
return &TerminatedError{}
}
// Error from error interface
func (e *TerminatedError) Error() string {
return e.message()
}
func (e *TerminatedError) message() string {
return "terminated"
}
// newUnknownExternalWorkflowExecutionError creates UnknownExternalWorkflowExecutionError instance
func newUnknownExternalWorkflowExecutionError() *UnknownExternalWorkflowExecutionError {
return &UnknownExternalWorkflowExecutionError{}
}
// Error from error interface
func (e *UnknownExternalWorkflowExecutionError) Error() string {
return "unknown external workflow execution"
}
// Error from error interface
func (e *ServerError) Error() string {
msg := e.message()
if e.cause != nil {
msg = fmt.Sprintf("%s: %v", msg, e.cause)
}
return msg
}
func (e *ServerError) message() string {
return e.msg
}
// Message contains just the message string without extras added by Error().
func (e *ServerError) Message() string {
return e.msg
}
func (e *ServerError) Unwrap() error {
return e.cause
}
func (e *ActivityError) Error() string {
msg := fmt.Sprintf("%s (type: %s, scheduledEventID: %d, startedEventID: %d, identity: %s)", e.message(), e.activityType.GetName(), e.scheduledEventID, e.startedEventID, e.identity)
if e.cause != nil {
msg = fmt.Sprintf("%s: %v", msg, e.cause)
}
return msg
}
func (e *ActivityError) message() string {
return "activity error"
}
func (e *ActivityError) Unwrap() error {
return e.cause
}
// ScheduledEventID returns event id of the scheduled workflow task corresponding to the activity.
func (e *ActivityError) ScheduledEventID() int64 {
return e.scheduledEventID
}
// StartedEventID returns event id of the started workflow task corresponding to the activity.
func (e *ActivityError) StartedEventID() int64 {
return e.startedEventID
}
// Identity returns identity of the worker that attempted activity execution.
func (e *ActivityError) Identity() string {
return e.identity
}
// ActivityType returns declared type of the activity.
func (e *ActivityError) ActivityType() *commonpb.ActivityType {
return e.activityType
}
// ActivityID return assigned identifier for the activity.
func (e *ActivityError) ActivityID() string {
return e.activityID
}
// RetryState returns details on why activity failed.
func (e *ActivityError) RetryState() enumspb.RetryState {
return e.retryState
}
// Error from error interface
func (e *ChildWorkflowExecutionError) Error() string {
msg := fmt.Sprintf("%s (type: %s, workflowID: %s, runID: %s, initiatedEventID: %d, startedEventID: %d)",
e.message(), e.workflowType, e.workflowID, e.runID, e.initiatedEventID, e.startedEventID)
if e.cause != nil {
msg = fmt.Sprintf("%s: %v", msg, e.cause)
}
return msg
}
func (e *ChildWorkflowExecutionError) message() string {
return "child workflow execution error"
}
func (e *ChildWorkflowExecutionError) Unwrap() error {
return e.cause
}
// Namespace returns namespace of the child workflow.
func (e *ChildWorkflowExecutionError) Namespace() string {
return e.namespace
}
// WorkflowId returns workflow ID of the child workflow.
func (e *ChildWorkflowExecutionError) WorkflowID() string {
return e.workflowID
}
// RunID returns run ID of the child workflow.
func (e *ChildWorkflowExecutionError) RunID() string {
return e.runID
}
// WorkflowType returns type of the child workflow.
func (e *ChildWorkflowExecutionError) WorkflowType() string {
return e.workflowType
}
// InitiatedEventID returns event ID of the child workflow initiated event.
func (e *ChildWorkflowExecutionError) InitiatedEventID() int64 {
return e.initiatedEventID
}
// StartedEventID returns event ID of the child workflow started event.
func (e *ChildWorkflowExecutionError) StartedEventID() int64 {
return e.startedEventID
}
// RetryState returns details on why child workflow failed.
func (e *ChildWorkflowExecutionError) RetryState() enumspb.RetryState {
return e.retryState
}
// Error implements the error interface.
func (e *NexusOperationError) Error() string {
msg := fmt.Sprintf(
"%s (endpoint: %q, service: %q, operation: %q, operation token: %q, scheduledEventID: %d)",
e.Message, e.Endpoint, e.Service, e.Operation, e.OperationToken, e.ScheduledEventID)
if e.Cause != nil {
msg = fmt.Sprintf("%s: %v", msg, e.Cause)
}
return msg
}
// setFailure implements the failureHolder interface for consistency with other failure based errors..
func (e *NexusOperationError) setFailure(f *failurepb.Failure) {
e.Failure = f
}
// failure implements the failureHolder interface for consistency with other failure based errors.
func (e *NexusOperationError) failure() *failurepb.Failure {
return e.Failure
}
// Unwrap returns the Cause associated with this error.
func (e *NexusOperationError) Unwrap() error {
return e.Cause
}
// Error from error interface
func (*NamespaceNotFoundError) Error() string {
return "namespace not found"