-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
146 lines (124 loc) · 2.82 KB
/
error.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
package zaperr
import (
"fmt"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func New(message string, fields ...zap.Field) error {
return zaperr{
source: errors.New(message),
fields: fields,
}
}
func Errorf(format string, args ...interface{}) error {
return errors.Errorf(format, args...)
}
func Wrap(err error, message string, fields ...zap.Field) error {
if err == nil {
return nil
}
if err, ok := err.(zaperr); ok {
if len(fields) == 0 {
err.source = errors.Wrap(err.source, message)
return err
}
if len(err.fields) == 0 {
return WithFields(errors.Wrap(err.source, message), fields...)
}
return zaperr{
source: err,
fields: fields,
message: message,
}
}
return WithFields(errors.Wrap(err, message), fields...)
}
type zaperr struct {
source error
fields []zap.Field
message string
}
func (e zaperr) Error() string {
if e.message != "" {
return e.message + ": " + e.source.Error()
}
return e.source.Error()
}
func (e zaperr) Cause() error {
return e.source
}
func (e zaperr) Unwrap() error {
return e.Cause()
}
func WithFields(err error, fields ...zap.Field) error {
if err == nil {
return nil
}
if err, ok := err.(zaperr); ok {
err.fields = append(err.fields, fields...)
return err
}
return zaperr{
source: err,
fields: fields,
}
}
func ToField(err error) zap.Field {
return ToNamedField("error", err)
}
func ToNamedField(key string, err error) zap.Field {
if err == nil {
return zap.Skip()
}
if e, ok := err.(zapcore.ObjectMarshaler); ok {
return zap.Object(key, e)
}
return zap.NamedError(key, err)
}
func (e zaperr) MarshalLogObject(enc zapcore.ObjectEncoder) error {
for _, f := range e.fields {
f.AddTo(enc)
}
if e.message != "" {
zap.String("message", e.message).AddTo(enc)
}
ToField(e.source).AddTo(enc)
return nil
}
func (e zaperr) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", e.source)
if e.message != "" {
fmt.Fprint(s, e.message)
}
return
}
fallthrough
case 's', 'q':
fmt.Fprint(s, e.Error())
}
}
type Wrapper struct {
fields []zap.Field
}
func WrapperWith(fields ...zap.Field) Wrapper {
return Wrapper{fields: fields}
}
func (w Wrapper) New(message string, fields ...zap.Field) error {
return New(message, append(fields, w.fields...)...)
}
func (w Wrapper) Errorf(format string, args ...interface{}) error {
return WithFields(Errorf(format, args...), w.fields...)
}
func (w Wrapper) Wrap(err error, message string, fields ...zap.Field) error {
return Wrap(err, message, append(fields, w.fields...)...)
}
func (w Wrapper) WithFields(err error, fields ...zap.Field) error {
return WithFields(err, append(fields, w.fields...)...)
}
func (w Wrapper) WrapperWith(fields ...zap.Field) Wrapper {
return Wrapper{fields: append(w.fields, fields...)}
}