-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy patherror.go
More file actions
103 lines (86 loc) · 4.44 KB
/
Copy patherror.go
File metadata and controls
103 lines (86 loc) · 4.44 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
package restate
import (
"fmt"
"github.com/restatedev/sdk-go/internal/errors"
)
// Code is a numeric status code for an error, matching HTTP status code semantics.
type Code = errors.Code
// ErrorCodeOption sets the [Code] on an error. It is shared: pass it to either
// [ToTerminalError] or [ToRetryableError].
type ErrorCodeOption = errors.CodeOption
// WithErrorCode sets the [Code] of a terminal or retryable error. Pass it to
// [ToTerminalError] or [ToRetryableError].
func WithErrorCode(code Code) ErrorCodeOption {
return errors.WithCode(code)
}
// TerminalError finishes an invocation (or a Run function) with a failure result
// instead of being retried. By default, Restate retries the invocation or Run
// function forever unless a terminal error is returned.
//
// It carries a status code, a message and optional metadata, accessible via the
// Code, Message and Metadata methods, and implements the error interface. Use
// [TerminalErrorf] or [ToTerminalError] to construct one.
type TerminalError = errors.TerminalError
// TerminalErrorOption customizes a [TerminalError]. Pass it to [ToTerminalError].
type TerminalErrorOption = errors.TerminalErrorOption
// ToTerminalError converts err into a [TerminalError], so that returning it from a
// handler or Run finishes the invocation with a failure result instead of being
// retried.
//
// IMPORTANT: this does NOT wrap err. A [TerminalError] carries no nested error and is
// not part of err's chain: errors.Unwrap, errors.Is and errors.As will not reach err
// through the result. Only the message, err.Error(), is copied.
//
// It returns nil if err is nil; if err already is, or wraps, a [TerminalError] and no
// options are given, that [TerminalError] is returned unchanged. The code defaults to
// 500 unless set with [WithErrorCode]; metadata can be attached with [WithMetadata].
func ToTerminalError(err error, opts ...TerminalErrorOption) TerminalError {
return errors.ToTerminalError(err, opts...)
}
// TerminalErrorf builds a [TerminalError] whose message is fmt.Sprintf(format, a...).
// To attach a code or metadata, build the message with fmt.Errorf and pass it to
// [ToTerminalError] with the relevant options.
func TerminalErrorf(format string, a ...any) TerminalError {
return errors.NewTerminalError(fmt.Sprintf(format, a...))
}
// IsTerminalError reports whether err is, or wraps, a [TerminalError] - ie, that
// returning it in a handler or Run function will finish the invocation with the
// error as a result.
func IsTerminalError(err error) bool {
return errors.IsTerminalError(err)
}
// AsTerminalError casts the current error to [TerminalError] if any.
func AsTerminalError(err error) TerminalError {
return errors.AsTerminalError(err)
}
// RetryableError finishes an attempt with a non-terminal failure: the invocation (or a
// Run closure) is retried rather than completed. It carries a [Code] and a message,
// wraps the underlying error, and implements the error interface. Returning one from a
// handler or Run closure is equivalent to returning any non-terminal error - Restate
// retries - except that its code is carried through. Use [RetryableErrorf] or
// [ToRetryableError] to construct one.
type RetryableError = errors.RetryableError
// RetryableErrorOption customizes a [RetryableError]. Pass it to [ToRetryableError].
type RetryableErrorOption = errors.RetryableErrorOption
// ToRetryableError converts err into a [RetryableError]. It returns nil if err is nil;
// if err already is, or wraps, a [RetryableError] and no options are given, that
// [RetryableError] is returned unchanged; otherwise err is wrapped (errors.Unwrap,
// errors.Is and errors.As reach err through the result). The code defaults to 500 unless
// set with [WithErrorCode].
func ToRetryableError(err error, opts ...RetryableErrorOption) RetryableError {
return errors.ToRetryableError(err, opts...)
}
// RetryableErrorf builds a [RetryableError] whose message is fmt.Sprintf(format, a...).
// To attach a code, build the message with fmt.Errorf and pass it to [ToRetryableError]
// with [WithErrorCode].
func RetryableErrorf(format string, a ...any) RetryableError {
return errors.NewRetryableError(fmt.Errorf(format, a...))
}
// IsRetryableError reports whether err is, or wraps, a [RetryableError].
func IsRetryableError(err error) bool {
return errors.IsRetryableError(err)
}
// AsRetryableError casts the current error to [RetryableError] if any.
func AsRetryableError(err error) RetryableError {
return errors.AsRetryableError(err)
}