-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunwrap_error.go
71 lines (59 loc) · 2.03 KB
/
unwrap_error.go
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
package temporalex
import (
"errors"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
)
type UnwrapErrType string
const (
UnwrapErrTypeNone UnwrapErrType = ""
UnwrapErrTypeFailure UnwrapErrType = "failure"
UnwrapErrTypePanic UnwrapErrType = "panic"
UnwrapErrTypeCancellation UnwrapErrType = "cancellation"
UnwrapErrTypeTimeout UnwrapErrType = "timeout"
)
var (
ErrSystemCancellation = errors.New("system cancellation")
ErrTimeout = errors.New("timed out")
)
type PanicError struct {
Message string `json:"message"`
StackTrace string `json:"stackTrace"`
}
func (e PanicError) Error() string { return e.Message }
// UnwrapError takes an error from a workflow or activity and unwraps the Temporal skeleton to leave the base error
// This utilizes UnwrapCustomError to extract the strongly-typed error across workflow/activity boundaries
func UnwrapError(inputErr error) (UnwrapErrType, string, error) {
curErr := inputErr
var appErr *temporal.ApplicationError
if errors.As(curErr, &appErr) {
curErr = UnwrapCustomError(appErr)
}
var panicErr *temporal.PanicError
if errors.As(curErr, &panicErr) {
return UnwrapErrTypePanic, panicErr.Error(), PanicError{
Message: panicErr.Error(),
StackTrace: panicErr.StackTrace(),
}
}
// These can error when a workflow's context has `Done()`
if errors.Is(curErr, workflow.ErrCanceled) {
return UnwrapErrTypeCancellation, ErrSystemCancellation.Error(), ErrSystemCancellation
} else if errors.Is(curErr, workflow.ErrDeadlineExceeded) {
return UnwrapErrTypeTimeout, ErrTimeout.Error(), ErrTimeout
}
var canceledErr *temporal.CanceledError
if errors.As(curErr, &canceledErr) {
var msg string
canceledErr.Details(&msg)
return UnwrapErrTypeCancellation, msg, curErr
}
var wflowErr *temporal.WorkflowExecutionError
var actErr *temporal.ActivityError
if errors.As(curErr, &wflowErr) {
curErr = wflowErr.Unwrap()
} else if errors.As(curErr, &actErr) {
curErr = actErr.Unwrap()
}
return UnwrapErrTypeFailure, curErr.Error(), curErr
}