forked from pingcap/errcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
279 lines (248 loc) · 7.16 KB
/
group.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
// Copyright Greg Weber and PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0
package errcode
import (
stderrors "errors"
"fmt"
"github.com/gregwebs/errors"
"github.com/gregwebs/errors/errwrap"
)
// ErrorCodes return all errors (including those grouped) that are of interface ErrorCode.
// It first calls the Errors function.
func ErrorCodes(errIn error) []ErrorCode {
errorCodes := make([]ErrorCode, 0)
for err := range errwrap.UnwrapGroups(errIn) {
if errcode, ok := err.(ErrorCode); ok {
// avoid duplicating codes
if len(errorCodes) == 0 || errorCodes[len(errorCodes)-1].Code().codeStr != errcode.Code().codeStr {
errorCodes = append(errorCodes, errcode)
}
}
}
return errorCodes
}
type multiCode[Err ErrorCode, Other error] struct {
ErrCode Err
rest []Other
}
func (e multiCode[Err, Other]) Error() string {
output := e.ErrCode.Error()
for _, item := range e.rest {
output += "; " + item.Error()
}
return output
}
// Unwrap fullfills the unwrapsError inteface
func (e multiCode[Err, Other]) Unwrap() []error {
rest := make([]error, len(e.rest))
for i, err := range e.rest {
rest[i] = error(err)
}
return append([]error{error(e.ErrCode)}, rest...)
}
// Code fullfills the ErrorCode inteface
func (e multiCode[Err, Other]) Code() Code {
return e.ErrCode.Code()
}
func (e multiCode[Err, Other]) First() Err {
return e.ErrCode
}
// Combine constructs a group that has at least one ErrorCode
// This is "horizontal" composition.
// If you want normal "vertical" composition use the Wrap* functions.
func combineGeneric[Err ErrorCode, Other error](initial Err, others ...Other) *multiCode[Err, Other] {
var rest []Other
for _, other := range others {
if ErrorCode(initial) == nil {
if errCode, ok := error(other).(Err); ok {
initial = errCode
continue
}
}
rest = append(rest, other)
}
if len(rest) == 0 && ErrorCode(initial) == nil {
return nil
}
return &multiCode[Err, Other]{
ErrCode: initial,
rest: rest,
}
}
var _ ErrorCode = (*multiCode[ErrorCode, error])(nil) // assert implements interface
var _ unwrapsError = (*multiCode[ErrorCode, error])(nil) // assert implements interface
var _ fmt.Formatter = (*multiCode[ErrorCode, error])(nil) // assert implements interface
// A multiErrorCode contains at least one ErrorCode and uses that to satisfy the ErrorCode and related interfaces
// The Error method will produce a string of all the errors with a semi-colon separation.
type multiErrorCode struct{ multiCode[ErrorCode, error] }
// A multiUserCode is similar to a multiErrorCode but satisfies UserCode
type multiUserCode struct{ multiCode[UserCode, error] }
func (e multiUserCode) GetUserMsg() string {
return e.ErrCode.GetUserMsg()
}
var _ UserCode = (*multiUserCode)(nil) // assert implements interface
// Combine constructs a single ErrorCode.
// The returned UserCode can have its errors unwrapped via interface{ Unwrap() []error }
func Combine(initial ErrorCode, others ...error) ErrorCode {
if len(others) == 0 && initial != nil {
return initial
}
combined := combineGeneric(initial, others...)
if combined == nil {
return nil
}
multiErrCode := multiCode[ErrorCode, error]{
ErrCode: combined.ErrCode,
rest: combined.rest,
}
return &multiErrorCode{multiErrCode}
}
// CombineUser constructs a single UserCode.
// The returned UserCode can have its errors unwrapped via interface{ Unwrap() []error }
func CombineUser(initial UserCode, others ...error) UserCode {
if len(others) == 0 && initial != nil {
return initial
}
combined := combineGeneric(initial, others...)
if combined == nil {
return nil
}
multiErrCode := multiCode[UserCode, error]{
ErrCode: combined.ErrCode,
rest: combined.rest,
}
return &multiUserCode{multiErrCode}
}
type unwrapsError interface {
Unwrap() []error
}
// This interface is checked by errors.As
type asAny interface {
As(any) bool
}
// CodeChain resolves wrapped errors down to the first ErrorCode.
// An error that is a grouping with multiple codes will have its error codes combined to a multiErrorCode.
// If the given error is not an ErrorCode, a ContextChain will be returned with Top set to the given error.
// This allows the return object to maintain a full Error() message.
func CodeChain(errInput error) ErrorCode {
checkError := func(err error) ErrorCode {
if errCode, ok := err.(ErrorCode); ok {
return errCode
}
as, asOK := err.(asAny)
{
var ecAs ErrorCode
if asOK && as.As(ecAs) {
return ecAs
}
}
eg, egOK := err.(unwrapsError)
if !egOK && asOK && as.As(eg) {
egOK = true
}
if egOK {
group := []ErrorCode{}
for _, errItem := range eg.Unwrap() {
if itemCode := CodeChain(errItem); itemCode != nil {
group = append(group, itemCode)
}
}
if len(group) > 0 {
if len(group) == 1 {
return group[0]
} else {
errs := make([]error, len(group[1:]))
for i, errCode := range group[1:] {
errs[i] = error(errCode)
}
return Combine(group[0], errs...)
}
}
}
return nil
}
// In this case there is no need for ChainContext
if errCode, ok := errInput.(ErrorCode); ok {
return errCode
}
err := errInput
for err != nil {
if errCode := checkError(err); errCode != nil {
return ChainContext{errCode, errInput}
}
err = stderrors.Unwrap(err)
}
return nil
}
// ChainContext is returned by ErrorCodeChain
// to retain the full wrapped error message of the error chain.
// If you annotated an ErrorCode with additional information, it is retained in the Top field.
// The Top field is used for the Error() and Unwrap() methods.
type ChainContext struct {
ErrorCode
Top error
}
// Error satisfies the Error interface
func (err ChainContext) Error() string {
return err.Top.Error()
}
// Unwrap satisfies the errors package Unwrap function
func (err ChainContext) Unwrap() error {
if wrapped := stderrors.Unwrap(err.Top); wrapped != nil {
return wrapped
}
return err.ErrorCode
}
var _ ErrorCode = (*ChainContext)(nil)
var _ unwrapError = (*ChainContext)(nil)
// Format implements the Formatter interface
func (err ChainContext) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", err.ErrorCode)
if errors.HasStack(err.ErrorCode) {
fmt.Fprintf(s, "%v", err.Top)
} else {
fmt.Fprintf(s, "%+v", err.Top)
}
return
}
if s.Flag('#') {
fmt.Fprintf(s, "ChainContext{Code: %#v, Top: %#v}", err.ErrorCode, err.Top)
return
}
fallthrough
case 's':
fmt.Fprintf(s, "Code: %s. Top Error: %s", err.ErrorCode.Code().CodeStr(), err.Top)
case 'q':
fmt.Fprintf(s, "Code: %q. Top Error: %q", err.ErrorCode.Code().CodeStr(), err.Top)
}
}
// Format implements the Formatter interface
func (e multiCode[Err, Other]) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", e.ErrCode)
if errors.HasStack(e.ErrCode) {
for _, nextErr := range e.rest {
fmt.Fprintf(s, "%v", nextErr)
}
} else {
for _, nextErr := range e.rest {
fmt.Fprintf(s, "%+v", nextErr)
}
}
return
}
fallthrough
case 's':
fmt.Fprintf(s, "%s\n", e.ErrCode)
fmt.Fprintf(s, "%s", e.rest)
case 'q':
fmt.Fprintf(s, "%q\n", e.ErrCode)
fmt.Fprintf(s, "%q\n", e.rest)
}
}