-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathstatus_source.go
258 lines (214 loc) · 6.83 KB
/
status_source.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
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
package status
import (
"context"
"crypto/x509"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"syscall"
grpccodes "google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
)
// Source type defines the status source.
type Source string
const (
// SourcePlugin status originates from plugin.
SourcePlugin Source = "plugin"
// SourceDownstream status originates from downstream service.
SourceDownstream Source = "downstream"
// DefaultSource is the default [Source] that should be used when it is not explicitly set.
DefaultSource Source = SourcePlugin
)
func NewErrorWithSource(err error, source Source) ErrorWithSource {
return ErrorWithSource{
source: source,
err: err,
}
}
// IsValid return true if es is [SourceDownstream] or [SourcePlugin].
func (s Source) IsValid() bool {
return s == SourceDownstream || s == SourcePlugin
}
// String returns the string representation of s. If s is not valid, [DefaultSource] is returned.
func (s Source) String() string {
if !s.IsValid() {
return string(DefaultSource)
}
return string(s)
}
// ErrorSourceFromStatus returns a [Source] based on provided HTTP status code.
func SourceFromHTTPStatus(statusCode int) Source {
switch statusCode {
case http.StatusMethodNotAllowed,
http.StatusNotAcceptable,
http.StatusPreconditionFailed,
http.StatusRequestEntityTooLarge,
http.StatusRequestHeaderFieldsTooLarge,
http.StatusRequestURITooLong,
http.StatusExpectationFailed,
http.StatusUpgradeRequired,
http.StatusRequestedRangeNotSatisfiable,
http.StatusNotImplemented:
return SourcePlugin
}
return SourceDownstream
}
type ErrorWithSource struct {
source Source
err error
}
// DownstreamError creates a new error with status [SourceDownstream].
func DownstreamError(err error) error {
return NewErrorWithSource(err, SourceDownstream)
}
// DownstreamError creates a new error with status [SourcePlugin].
func PluginError(err error) error {
return NewErrorWithSource(err, SourcePlugin)
}
// DownstreamErrorf creates a new error with status [SourceDownstream] and formats
// according to a format specifier and returns the string as a value that satisfies error.
func DownstreamErrorf(format string, a ...any) error {
return DownstreamError(fmt.Errorf(format, a...))
}
func (e ErrorWithSource) ErrorSource() Source {
return e.source
}
// @deprecated Use [ErrorSource] instead.
func (e ErrorWithSource) Source() Source {
return e.source
}
func (e ErrorWithSource) Error() string {
return e.err.Error()
}
// Implements the interface used by [errors.Is].
func (e ErrorWithSource) Is(err error) bool {
if errWithSource, ok := err.(ErrorWithSource); ok {
return errWithSource.ErrorSource() == e.source
}
return false
}
func (e ErrorWithSource) Unwrap() error {
return e.err
}
func IsPluginError(err error) bool {
e := ErrorWithSource{
source: SourcePlugin,
}
return errors.Is(err, e)
}
// IsDownstreamError return true if provided error is an error with downstream source or
// a timeout error or a cancelled error.
func IsDownstreamError(err error) bool {
e := ErrorWithSource{
source: SourceDownstream,
}
if errors.Is(err, e) {
return true
}
return isHTTPTimeoutError(err) || IsCancelledError(err)
}
// IsDownstreamHTTPError return true if provided error is an error with downstream source or
// a HTTP timeout error or a cancelled error or a connection reset/refused error or dns not found error.
func IsDownstreamHTTPError(err error) bool {
return IsDownstreamError(err) ||
isConnectionResetOrRefusedError(err) ||
isDNSNotFoundError(err) ||
isTLSCertificateVerificationError(err) ||
isHTTPEOFError(err)
}
// InCancelledError returns true if err is context.Canceled or is gRPC status Canceled.
func IsCancelledError(err error) bool {
return errors.Is(err, context.Canceled) || grpcstatus.Code(err) == grpccodes.Canceled
}
func isHTTPTimeoutError(err error) bool {
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
return true
}
return errors.Is(err, os.ErrDeadlineExceeded) // replacement for os.IsTimeout(err)
}
func isConnectionResetOrRefusedError(err error) bool {
var netErr *net.OpError
if errors.As(err, &netErr) {
var sysErr *os.SyscallError
if errors.As(netErr.Err, &sysErr) {
return errors.Is(sysErr.Err, syscall.ECONNRESET) || errors.Is(sysErr.Err, syscall.ECONNREFUSED)
}
}
return false
}
func isDNSNotFoundError(err error) bool {
var dnsError *net.DNSError
if errors.As(err, &dnsError) && dnsError.IsNotFound {
return true
}
return false
}
// isTLSCertificateVerificationError checks if the error is related to TLS certificate verification.
func isTLSCertificateVerificationError(err error) bool {
var certErr *x509.CertificateInvalidError
var unknownAuthErr x509.UnknownAuthorityError
// Directly check for CertificateInvalidError or UnknownAuthorityError
if errors.As(err, &certErr) || errors.As(err, &unknownAuthErr) {
return true
}
// Check if the error is wrapped in a *url.Error
var urlErr *url.Error
if errors.As(err, &urlErr) {
// Check the underlying error in urlErr
if errors.As(urlErr.Err, &certErr) || errors.As(urlErr.Err, &unknownAuthErr) {
return true
}
}
return false
}
// isHTTPEOFError returns true if the error is an EOF error inside of url.Error or net.OpError, indicating the connection was closed prematurely by server
func isHTTPEOFError(err error) bool {
var netErr *net.OpError
if errors.As(err, &netErr) {
return errors.Is(netErr.Err, io.EOF)
}
var urlErr *url.Error
if errors.As(err, &urlErr) {
return errors.Is(urlErr.Err, io.EOF)
}
return false
}
type sourceCtxKey struct{}
// SourceFromContext returns the source stored in the context.
// If no source is stored in the context, [DefaultSource] is returned.
func SourceFromContext(ctx context.Context) Source {
value, ok := ctx.Value(sourceCtxKey{}).(*Source)
if ok {
return *value
}
return DefaultSource
}
// InitSource initialize the source for the context.
func InitSource(ctx context.Context) context.Context {
s := DefaultSource
return context.WithValue(ctx, sourceCtxKey{}, &s)
}
// WithSource mutates the provided context by setting the source to
// s. If the provided context does not have a source, the context
// will not be mutated and an error returned. This means that [InitSource]
// has to be called before this function.
func WithSource(ctx context.Context, s Source) error {
v, ok := ctx.Value(sourceCtxKey{}).(*Source)
if !ok {
return errors.New("the provided context does not have a status source")
}
*v = s
return nil
}
// WithDownstreamSource mutates the provided context by setting the source to
// [SourceDownstream]. If the provided context does not have a source, the context
// will not be mutated and an error returned. This means that [InitSource] has to be
// called before this function.
func WithDownstreamSource(ctx context.Context) error {
return WithSource(ctx, SourceDownstream)
}