-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrs.go
324 lines (295 loc) · 8.64 KB
/
errs.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
// Package errs implements functions to manipulate error instances.
package errs
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"runtime"
"strconv"
"strings"
)
const (
nilAngleString = "<nil>"
)
// Error type is a implementation of error interface.
// This type is for wrapping cause error instance.
type Error struct {
wrapFlag bool
Err error
Cause error
Context map[string]interface{}
}
var _ error = (*Error)(nil) //Error type is compatible with error interface
var _ fmt.Stringer = (*Error)(nil) //Error type is compatible with fmt.Stringer interface
var _ fmt.GoStringer = (*Error)(nil) //Error type is compatible with fmt.GoStringer interface
var _ fmt.Formatter = (*Error)(nil) //Error type is compatible with fmt.Formatter interface
var _ json.Marshaler = (*Error)(nil) //Error type is compatible with json.Marshaler interface
// ErrorContextFunc type is self-referential function type for New and Wrap functions. (functional options pattern)
type ErrorContextFunc func(*Error)
// New function returns an error instance with message and context informations.
func New(msg string, opts ...ErrorContextFunc) error {
if len(msg) == 0 {
return nil
}
return newError(errors.New(msg), false, 2, opts...)
}
// Wrap function returns a wrapping error instance with context informations.
func Wrap(err error, opts ...ErrorContextFunc) error {
if err == nil {
return nil
}
return newError(err, true, 2, opts...)
}
// newError returns error instance. (internal)
func newError(err error, wrapFlag bool, depth int, opts ...ErrorContextFunc) error {
we := &Error{Err: err, wrapFlag: wrapFlag}
//caller function name
if fname, _, _ := caller(depth); len(fname) > 0 {
we = we.SetContext("function", fname)
}
//other params
for _, opt := range opts {
opt(we)
}
return we
}
// WithContext function returns ErrorContextFunc function value.
// This function is used in New and Wrap functions that represents context (key/value) data.
func WithContext(name string, value interface{}) ErrorContextFunc {
return func(e *Error) {
_ = e.SetContext(name, value)
}
}
// WithCause function returns ErrorContextFunc function value.
// This function is used in New and Wrap functions that represents context (key/value) data.
func WithCause(err error) ErrorContextFunc {
return func(e *Error) {
_ = e.SetCause(err)
}
}
// SetContext method sets context information
func (e *Error) SetContext(name string, value interface{}) *Error {
if e == nil {
return e
}
if e.Context == nil {
e.Context = map[string]interface{}{}
}
if len(name) > 0 {
e.Context[name] = value
}
return e
}
// SetCause method sets cause error instance
func (e *Error) SetCause(err error) *Error {
if e == nil {
return e
}
e.Cause = err
return e
}
// Unwrap method returns cause error in Error instance.
// This method is used in errors.Unwrap function.
func (e *Error) Unwrap() error {
if e == nil {
return nil
}
if e.Cause == nil {
if e.wrapFlag {
return e.Err
}
return errors.Unwrap(e.Err)
}
return e.Cause
}
// Is method reports whether any error in error's chain matches cause of target error.
// This method is used in errors.Is function.
func (e *Error) Is(target error) bool {
if e == target {
return true
}
if e != nil {
if errors.Is(e.Err, target) {
return true
}
if errors.Is(e.Cause, target) {
return true
}
}
return false
}
// Error method returns error message.
// This method is a implementation of error interface.
func (e *Error) Error() string {
if e == nil {
return nilAngleString
}
errMsg := e.Err.Error()
var causeMsg string
if e.Cause != nil {
causeMsg = e.Cause.Error()
}
if len(causeMsg) == 0 {
return errMsg
}
if len(errMsg) == 0 {
return causeMsg
}
return strings.Join([]string{errMsg, causeMsg}, ": ")
}
// String method returns error message.
// This method is a implementation of fmt.Stringer interface.
func (e *Error) String() string {
return e.Error()
}
// GoString method returns serialize string of Error.
// This method is a implementation of fmt.GoStringer interface.
func (e *Error) GoString() string {
if e == nil {
return nilAngleString
}
return fmt.Sprintf("%T{Err:%#v, Cause:%#v, Context:%#v}", e, e.Err, e.Cause, e.Context)
}
// MarshalJSON method returns serialize string of Error with JSON format.
// This method is implementation of json.Marshaler interface.
func (e *Error) MarshalJSON() ([]byte, error) {
return []byte(e.EncodeJSON()), nil
}
// EncodeJSON method returns serialize string of Error with JSON format.
func (e *Error) EncodeJSON() string {
if e == nil {
return "null"
}
elms := []string{}
elms = append(elms, strings.Join([]string{`"Type":`, strconv.Quote(reflect.TypeOf(e).String())}, ""))
msgBuf := &bytes.Buffer{}
json.HTMLEscape(msgBuf, bytes.Join([][]byte{[]byte(`"Err":`), []byte(EncodeJSON(e.Err))}, []byte{}))
elms = append(elms, msgBuf.String())
if len(e.Context) > 0 {
if b, err := json.Marshal(e.Context); err == nil {
elms = append(elms, string(bytes.Join([][]byte{[]byte(`"Context":`), b}, []byte{})))
}
}
if e.Cause != nil && !reflect.ValueOf(e.Cause).IsZero() {
elms = append(elms, strings.Join([]string{`"Cause":`, EncodeJSON(e.Cause)}, ""))
}
return strings.Join([]string{"{", strings.Join(elms, ","), "}"}, "")
}
// Format method returns formatted string of Error instance.
// This method is a implementation of fmt.Formatter interface.
func (e *Error) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case s.Flag('#'):
_, _ = strings.NewReader(e.GoString()).WriteTo(s)
case s.Flag('+'):
_, _ = strings.NewReader(e.EncodeJSON()).WriteTo(s)
default:
_, _ = strings.NewReader(e.Error()).WriteTo(s)
}
case 's':
_, _ = strings.NewReader(e.String()).WriteTo(s)
default:
fmt.Fprintf(s, `%%!%c(%s)`, verb, e.GoString())
}
}
// Cause function finds cause error in target error instance.
//
// Deprecated: should not be used
func Cause(err error) error {
for err != nil {
unwraped := errors.Unwrap(err)
if unwraped == nil {
return err
}
err = unwraped
}
return err
}
// Unwraps function finds cause errors ([]error slice) in target error instance.
func Unwraps(err error) []error {
if err == nil {
return nil
}
if es, ok := err.(interface {
Unwrap() []error
}); ok {
return es.Unwrap()
}
if e := errors.Unwrap(err); e != nil {
return []error{e}
}
return nil
}
// caller returns caller info.
func caller(depth int) (string, string, int) {
pc, src, line, ok := runtime.Caller(depth + 1)
if !ok {
return "", "", 0
}
return runtime.FuncForPC(pc).Name(), src, line
}
// EncodeJSON function dumps out error instance with JSON format.
func EncodeJSON(err error) string {
if e, ok := err.(*Error); ok {
return e.EncodeJSON()
}
if e, ok := err.(json.Marshaler); ok {
b, ee := json.Marshal(e)
if ee != nil {
return encodeJSON(err)
}
return strings.TrimSpace(string(b))
}
return encodeJSON(err)
}
func encodeJSON(err error) string {
if err == nil {
return "null"
}
elms := []string{}
elms = append(elms, strings.Join([]string{`"Type":`, strconv.Quote(reflect.TypeOf(err).String())}, ""))
msgBuf := &bytes.Buffer{}
json.HTMLEscape(msgBuf, bytes.Join([][]byte{[]byte(`"Msg":`), []byte(strconv.Quote(err.Error()))}, []byte{}))
elms = append(elms, msgBuf.String())
switch x := err.(type) {
case interface{ Unwrap() error }:
unwraped := x.Unwrap()
if err != nil {
elms = append(elms, strings.Join([]string{`"Cause":`, EncodeJSON(unwraped)}, ""))
}
case interface{ Unwrap() []error }:
unwraped := x.Unwrap()
if len(unwraped) > 0 {
causes := []string{}
for _, c := range unwraped {
causes = append(causes, EncodeJSON(c))
}
elms = append(elms, strings.Join([]string{`"Cause":[`, strings.Join(causes, ","), "]"}, ""))
}
}
return strings.Join([]string{"{", strings.Join(elms, ","), "}"}, "")
}
// Is is conpatible with errors.Is.
func Is(err, target error) bool { return errors.Is(err, target) }
// As is conpatible with errors.As.
func As(err error, target interface{}) bool { return errors.As(err, target) }
// Unwrap is conpatible with errors.Unwrap.
func Unwrap(err error) error { return errors.Unwrap(err) }
/* Copyright 2019-2023 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/