-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
406 lines (332 loc) · 11.5 KB
/
errors.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
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
package errdetails
import (
"errors"
"time"
"github.com/ClaudiaJ/errdetails/details"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
)
var errUnknown = errors.New("unknown error")
// Known Status Code errors for use as target of errors.Is().
//
// Prefer constructing new errors with New constructor.
var (
ErrCanceled error = &errCodeError{error: errUnknown, Code: codes.Canceled}
ErrUnknown error = &errCodeError{error: errUnknown, Code: codes.Unknown}
ErrInvalidArgument error = &errCodeError{error: errUnknown, Code: codes.InvalidArgument}
ErrDeadlineExceeded error = &errCodeError{error: errUnknown, Code: codes.DeadlineExceeded}
ErrNotFound error = &errCodeError{error: errUnknown, Code: codes.NotFound}
ErrAlreadyExists error = &errCodeError{error: errUnknown, Code: codes.AlreadyExists}
ErrPermissionDenied error = &errCodeError{error: errUnknown, Code: codes.PermissionDenied}
ErrResourceExhausted error = &errCodeError{error: errUnknown, Code: codes.ResourceExhausted}
ErrFailedPrecondition error = &errCodeError{error: errUnknown, Code: codes.FailedPrecondition}
ErrAborted error = &errCodeError{error: errUnknown, Code: codes.Aborted}
ErrOutOfRange error = &errCodeError{error: errUnknown, Code: codes.OutOfRange}
ErrUnimplemented error = &errCodeError{error: errUnknown, Code: codes.Unimplemented}
ErrInternal error = &errCodeError{error: errUnknown, Code: codes.Internal}
ErrUnavailable error = &errCodeError{error: errUnknown, Code: codes.Unavailable}
ErrDataLoss error = &errCodeError{error: errUnknown, Code: codes.DataLoss}
ErrUnauthenticated error = &errCodeError{error: errUnknown, Code: codes.Unauthenticated}
)
// New creates a new error from an Status Error Code.
// Resulting errors can be checked with errors.Is to match exported implementations.
//
// This implementation is specific to gRPC Status codes, but the same example
// can be applied for any other Flag-like wrapping.
func New(code codes.Code, msg string, details ...Details) error {
return WithDetails(&errCodeError{
Code: code,
error: errors.New(msg),
}, details...)
}
// errCodeError enriches an error with status codes.
type errCodeError struct {
error
codes.Code
}
// Is implements errors.Is, matches a target error if it implements errorCode
// and the error code matches instance of errCodeError error code.
func (e *errCodeError) Is(target error) bool {
if v, ok := target.(*errCodeError); ok {
return v.Code == e.Code
}
return false
}
// GRPCStatus implements interface required for status.FromError to turn the
// error into a gRPC Status.
func (e *errCodeError) GRPCStatus() *status.Status {
return status.New(e.Code, e.Error())
}
// StatusCode translates the gRPC Status Code to an equivilent HTTP status code.
func (e *errCodeError) StatusCode() int {
return runtime.HTTPStatusFromCode(e.Code)
}
// Unwrap implements errors.Unwrap interface.
func (e *errCodeError) Unwrap() error {
return e.error
}
// RequestInfoError is an error including metadata about the request that
// a client can attach when filing a bug or providing other forms of feedback.
type RequestInfoError interface {
error
details.RequestInfo
}
var _ RequestInfoError = (*errRequestInfo)(nil)
type errRequestInfo struct {
error
*errdetails.RequestInfo
}
// Unwrap implements errors.Unwrap interface.
func (e *errRequestInfo) Unwrap() error {
return e.error
}
// HelpfulError is an error including links to documentation relevant to the
// error or API.
type HelpfulError interface {
error
WithLinks(...details.HelpLink) HelpfulError
GetLinks() []details.HelpLink
}
type errHelpLink struct {
error
*errdetails.Help
}
// Unwrap implements errors.Unwrap interface.
func (h *errHelpLink) Unwrap() error {
return h.error
}
// WithLinks adds links to relavant documentation.
func (h *errHelpLink) WithLinks(links ...details.HelpLink) HelpfulError {
v := make([]*errdetails.Help_Link, len(links))
for idx, link := range links {
if helpLink, ok := link.(*errdetails.Help_Link); ok {
v[idx] = helpLink
continue
}
v[idx] = &errdetails.Help_Link{
Description: link.GetDescription(),
Url: link.GetUrl(),
}
}
h.Help.Links = append(h.Help.Links, v...)
return h
}
// GetLinks retrieves all links annotated into the helpful error.
func (e *errHelpLink) GetLinks() []details.HelpLink {
links := make([]details.HelpLink, len(e.Help.Links))
for k, v := range e.Help.Links {
links[k] = v
}
return links
}
// BadRequestError is an error indicating the client had made a bad request,
// and includes details of each violation of the field validation rules not
// satisfied by the request.
type BadRequestError interface {
error
WithViolation(violation ...details.FieldViolation) BadRequestError
GetViolations() []details.FieldViolation
}
var _ BadRequestError = (*errBadRequest)(nil)
type errBadRequest struct {
error
*errdetails.BadRequest
}
// Unwrap implement errors.Unwrap
func (e *errBadRequest) Unwrap() error {
return e.error
}
// WithViolation appends field violations to a bad request error.
func (e *errBadRequest) WithViolation(violations ...details.FieldViolation) BadRequestError {
v := make([]*errdetails.BadRequest_FieldViolation, len(violations))
for idx, fieldViolation := range violations {
if violation, ok := fieldViolation.(*errdetails.BadRequest_FieldViolation); ok {
v[idx] = violation
continue
}
v[idx] = &errdetails.BadRequest_FieldViolation{
Field: fieldViolation.GetField(),
Description: fieldViolation.GetDescription(),
}
}
e.BadRequest.FieldViolations = append(e.BadRequest.FieldViolations, v...)
return e
}
func (e *errBadRequest) GetViolations() []details.FieldViolation {
violations := make([]details.FieldViolation, len(e.BadRequest.FieldViolations))
for k, v := range e.BadRequest.FieldViolations {
violations[k] = v
}
return violations
}
// DebugError is an error including debug information indicating where an error
// occurred and any additional details provided by the server.
type DebugError interface {
error
details.DebugInfo
}
var _ DebugError = (*errDebugInfo)(nil)
type errDebugInfo struct {
error
*errdetails.DebugInfo
}
// Unwrap implement errors.Unwrap
func (e *errDebugInfo) Unwrap() error {
return e.error
}
// CausedError is an error describing the cause of an error with structured details.
type CausedError interface {
error
details.Info
}
var _ CausedError = (*errInfo)(nil)
type errInfo struct {
error
*errdetails.ErrorInfo
}
// Unwrap implements errors.Unwrap interface.
func (e *errInfo) Unwrap() error {
return e.error
}
// LocalizedError is an error including a localized error message that is safe
// to return to the user.
type LocalizedError interface {
error
details.LocalizedMessage
}
var _ LocalizedError = (*localizedError)(nil)
type localizedError struct {
error
*errdetails.LocalizedMessage
}
// Unwrap implements errors.Unwrap interface.
func (e *localizedError) Unwrap() error {
return e.error
}
// FailedPreconditionError is an error describing what preconditions have failed.
//
// An example being a Terms of Service acknowledgement that may be required
// before using a particular API or service, responses from the service will
// indicate that the precondition has not been met.
type FailedPreconditionError interface {
error
WithViolation(...details.PreconditionViolation) FailedPreconditionError
GetViolations() []details.PreconditionViolation
}
var _ FailedPreconditionError = (*errPreconditionFailed)(nil)
type errPreconditionFailed struct {
error
*errdetails.PreconditionFailure
}
// Unwrap implements errors.Unwrap interface.
func (e *errPreconditionFailed) Unwrap() error {
return e.error
}
// WithViolation adds PreconditionViolations to the FailedPreconditionError.
func (e *errPreconditionFailed) WithViolation(violations ...details.PreconditionViolation) FailedPreconditionError {
v := make([]*errdetails.PreconditionFailure_Violation, len(violations))
for idx, fieldViolation := range violations {
if violation, ok := fieldViolation.(*errdetails.PreconditionFailure_Violation); ok {
v[idx] = violation
continue
}
v[idx] = &errdetails.PreconditionFailure_Violation{
Type: fieldViolation.GetType(),
Subject: fieldViolation.GetSubject(),
Description: fieldViolation.GetDescription(),
}
}
e.PreconditionFailure.Violations = append(e.PreconditionFailure.Violations, v...)
return e
}
// GetViolations gets all the PreconditionViolations on the FailedPreconditionError.
func (e *errPreconditionFailed) GetViolations() []details.PreconditionViolation {
violations := make([]details.PreconditionViolation, len(e.PreconditionFailure.Violations))
for k, v := range e.PreconditionFailure.Violations {
violations[k] = v
}
return violations
}
// FailedQuotaError is an error describing a quota check failed.
type FailedQuotaError interface {
error
WithViolation(...details.QuotaViolation) FailedQuotaError
GetViolations() []details.QuotaViolation
}
var _ FailedQuotaError = (*errQuotaFailure)(nil)
type errQuotaFailure struct {
error
*errdetails.QuotaFailure
}
// Unwrap implements errors.Unwrap interface.
func (e *errQuotaFailure) Unwrap() error {
return e.error
}
// WithViolation adds quota violations to the FailedQuotaError.
func (e *errQuotaFailure) WithViolation(violations ...details.QuotaViolation) FailedQuotaError {
v := make([]*errdetails.QuotaFailure_Violation, len(violations))
for idx, fieldViolation := range violations {
if violation, ok := fieldViolation.(*errdetails.QuotaFailure_Violation); ok {
v[idx] = violation
continue
}
v[idx] = &errdetails.QuotaFailure_Violation{
Subject: fieldViolation.GetSubject(),
Description: fieldViolation.GetDescription(),
}
}
e.QuotaFailure.Violations = append(e.QuotaFailure.Violations, v...)
return e
}
// GetViolations gets all the QuotaViolations on the FailedQuotaError.
func (e *errQuotaFailure) GetViolations() []details.QuotaViolation {
violations := make([]details.QuotaViolation, len(e.QuotaFailure.Violations))
for k, v := range e.QuotaFailure.Violations {
violations[k] = v
}
return violations
}
// ResourceInfoError is an error that describes the resource that is being accessed.
type ResourceInfoError interface {
error
details.ResourceInfo
}
var _ ResourceInfoError = (*errResourceInfo)(nil)
type errResourceInfo struct {
error
*errdetails.ResourceInfo
}
// Unwrap implements errors.Unwrap interface.
func (e *errResourceInfo) Unwrap() error {
return e.error
}
// RetriableError is an error that describes when a client may retry a failed request.
//
// The retry delay represents a minimum duration in which the client is recommended to wait.
// It is always recommended the client should use exponential backoff when retrying.
type RetriableError interface {
error
WithDelay(time.Duration) RetriableError
GetRetryDelay() time.Duration
}
var _ RetriableError = (*errRetryInfo)(nil)
type errRetryInfo struct {
error
*errdetails.RetryInfo
}
// Unwrap implements errors.Unwrap interface.
func (e *errRetryInfo) Unwrap() error {
return e.error
}
// WithDelay sets a recommended retry delay on the RetriableError.
func (e *errRetryInfo) WithDelay(d time.Duration) RetriableError {
e.RetryInfo.RetryDelay = durationpb.New(d)
return e
}
// GetRetryDelay gets the recommended retry delay on the RetriableError.
func (e *errRetryInfo) GetRetryDelay() time.Duration {
return e.RetryInfo.RetryDelay.AsDuration()
}