Repository navigation
Expand file tree
/
Copy patherror.go
More file actions
219 lines (192 loc) · 7.57 KB
/
Copy patherror.go
File metadata and controls
219 lines (192 loc) · 7.57 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
package gush
import (
"errors"
"regexp"
"strings"
"go.temporal.io/api/serviceerror"
"go.temporal.io/sdk/temporal"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// ApplicationError is an error with a type name that retry policies can match.
type ApplicationError = temporal.ApplicationError
// PanicError is an unrecovered panic during a step.
type PanicError = temporal.PanicError
// NewApplicationError creates a retryable error. Pass errType to WithNoRetry to stop
// retrying errors of this type on a particular run.
func NewApplicationError(message, errType string, details ...any) *ApplicationError {
return temporal.NewApplicationError(message, errType, details...).(*ApplicationError)
}
// NewPermanentError creates an error that fails its flow or step immediately, whatever
// the retry policy says.
func NewPermanentError(message, errType string, cause error, details ...any) *ApplicationError {
return temporal.NewNonRetryableApplicationError(message, errType, cause, details...).(*ApplicationError)
}
// IsApplicationError reports whether err is an ApplicationError.
func IsApplicationError(err error) bool {
return temporal.IsApplicationError(err)
}
// IsPermanentError reports whether err is an ApplicationError that is not retried.
func IsPermanentError(err error) bool {
var applicationError *ApplicationError
if errors.As(err, &applicationError) {
return applicationError.NonRetryable()
}
return false
}
// IsFlowRunningError reports whether a flow did not start because one with the same ID is
// running. A child flow gets this error, and so does a client using WithErrorIfRunning. A
// step using such a client must check the error itself, because a flow cannot recognize it
// after it crosses the step.
func IsFlowRunningError(err error) bool {
return temporal.IsWorkflowExecutionAlreadyStartedError(err)
}
// IsInvalidConfigError reports whether a request was refused as invalid, so sending it again
// cannot succeed: Gush found settings Temporal refuses, including in a flow that has since
// failed on them, or the server answered InvalidArgument. The server gives that answer for
// more than settings, such as an input over its blob size limit, 2MiB by default. A request over
// gRPC's message size limit, 4MiB by default, is refused before it reaches Temporal, and that
// counts too. Past a flow it sees through Abort, NewPermanentError and %w, but not errors.Join
// or several %w, and not a client's refusal inside a step, whose text is all that crosses the
// step.
func IsInvalidConfigError(err error) bool {
if errors.Is(err, errInvalidConfig) {
return true
}
var rejected *serviceerror.InvalidArgument
if errors.As(err, &rejected) {
return true
}
if isMessageTooLarge(err) {
return true
}
// A flow can wrap the failure in an error of its own before returning it, so look at every
// ApplicationError in the chain rather than the outermost.
var applicationError *ApplicationError
for errors.As(err, &applicationError) {
if applicationError.Type() == invalidConfigErrorType && applicationError.NonRetryable() {
return true
}
err = applicationError.Unwrap()
}
return false
}
// messageTooLargePrefixes are how gRPC words a refusal of a message over its size limit. The
// code alone, ResourceExhausted, cannot tell, because Temporal also uses it for rate limits.
var messageTooLargePrefixes = []string{
"grpc: received message larger than max",
"grpc: message after decompression larger than max",
"grpc: received message after decompression larger than max",
"grpc: trying to send message larger than max",
"grpc: message too large",
"trying to send message larger than max",
}
// isMessageTooLarge reports whether gRPC refused a request for its size. The SDK gives such an
// error its own type and does not retry it, but nothing outside the SDK can name that type.
func isMessageTooLarge(err error) bool {
var withStatus interface{ GRPCStatus() *status.Status }
if !errors.As(err, &withStatus) {
return false
}
grpcStatus := withStatus.GRPCStatus()
if grpcStatus == nil || grpcStatus.Code() != codes.ResourceExhausted {
return false
}
for _, prefix := range messageTooLargePrefixes {
if strings.HasPrefix(grpcStatus.Message(), prefix) {
return true
}
}
return false
}
// IsPanicError reports whether err is a PanicError.
func IsPanicError(err error) bool {
return temporal.IsPanicError(err)
}
// programError carries the short "[type] message" text of an ApplicationError while
// keeping the ApplicationError itself reachable through Unwrap, so errors.As,
// IsApplicationError and IsPermanentError still see its type, retryability and details.
type programError struct {
message string
appErr *ApplicationError
}
func (e *programError) Error() string {
return e.message
}
func (e *programError) Unwrap() error {
return e.appErr
}
// temporalError returns the error a flow or step hands back to Temporal, which only reads
// the type and retryability of an ApplicationError it is given directly. Anything else
// would be recorded as an untyped, retryable error, so an ApplicationError found inside a
// wrapped error is passed on as one that keeps the wrapper's message. Its details stay on
// the wrapped error, which is that one's cause.
func temporalError(err error) error {
programErr, ok := err.(*programError)
if ok {
return programErr.appErr
}
var appErr *ApplicationError
if !errors.As(err, &appErr) {
return err
}
switch err.(type) {
case *ApplicationError, *temporal.ActivityError, *temporal.ChildWorkflowExecutionError:
return err
}
options := temporal.ApplicationErrorOptions{
NonRetryable: appErr.NonRetryable(),
Cause: appErr,
NextRetryDelay: appErr.NextRetryDelay(),
Category: appErr.Category(),
}
return temporal.NewApplicationErrorWithOptions(wrappedMessage(err, appErr), appErr.Type(), options)
}
// wrappedMessage returns err's text with the wrapped error reduced to its plain message,
// because the type is recorded separately and would otherwise appear twice.
func wrappedMessage(err error, appErr *ApplicationError) string {
message := strings.Replace(err.Error(), appErr.Error(), appErr.Message(), 1)
var programErr *programError
if errors.As(err, &programErr) {
message = strings.Replace(message, programErr.message, appErr.Message(), 1)
}
return message
}
// unwrapError extracts the program's own error from Temporal's error chain, which
// otherwise reads "activity error (type: ..., scheduledEventID: ...): message (type: ...,
// retryable: ...)". An ApplicationError stays reachable through errors.As.
func unwrapError(err error) error {
if err == nil {
return nil
}
var appErr *ApplicationError
if errors.As(err, &appErr) {
msg := appErr.Message()
errType := appErr.Type()
if errType != "" {
msg = "[" + errType + "] " + msg
}
programErr := &programError{message: msg, appErr: appErr}
return programErr
}
current := err
for {
unwrapped := errors.Unwrap(current)
if unwrapped == nil {
return current
}
current = unwrapped
}
}
// temporalErrorType matches what the SDK's errors add to their text, such as
// "(type: Declined, retryable: true)".
var temporalErrorType = regexp.MustCompile(` \(type: [^()]*\)`)
// abortError is the permanent error behind Abort on flows and steps. Its message is the
// cause's text, which keeps what the cause wraps, without the SDK's additions.
func abortError(errType string, cause []error) *ApplicationError {
if len(cause) == 0 || cause[0] == nil {
return NewPermanentError("unknown error", errType, nil)
}
message := temporalErrorType.ReplaceAllString(cause[0].Error(), "")
return NewPermanentError(message, errType, cause[0])
}