-
Notifications
You must be signed in to change notification settings - Fork 0
/
errm.go
215 lines (189 loc) · 5.38 KB
/
errm.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
// Package errm is a library for convinient and easy use of errors in a golang code.
package errm
import (
"fmt"
"io"
"strings"
"github.com/rotisserie/eris"
)
type errorImpl struct {
err error
}
func newError(err error) errorImpl {
return errorImpl{err: err}
}
// Error implements error interface, it just returns error message with applied fields in field=val format.
func (e errorImpl) Error() string {
return e.err.Error()
}
// String is a wrapper of Error method.
func (e errorImpl) String() string {
return e.Error()
}
// StackForLogger returns slice ["stack", "[...]"] that can be used as fields for logger if you want to log stack trace.
func (e errorImpl) StackForLogger() []any {
jsonErr := ToJSON(e.err)
root, ok := jsonErr["root"].(map[string]any)
if !ok {
return nil
}
return []any{"stack", root["stack"]}
}
// Format is used to handle %+v in formatted print, that will print stack trace.
func (e errorImpl) Format(s fmt.State, verb rune) {
var withTrace bool
switch verb {
case 'v':
if s.Flag('+') {
withTrace = true
}
default:
break
}
str := eris.ToString(e.err, withTrace)
_, _ = io.WriteString(s, str)
}
// New creates a new error with a static message and pairs of fields in a field=val format.
func New(msg string, fields ...any) error {
return newError(eris.New(buildErrorMessage(msg, fields)))
}
// Errorf creates a new error with a formatted message and pairs of fields in a field=val format.
func Errorf(msg string, args ...any) error {
args, fields := separateArgsAndFields(msg, args)
if len(args) == 0 {
return New(msg, fields...)
}
return newError(eris.Errorf(buildErrorMessage(msg, fields), args...))
}
// Wrap adds additional context to all error types while maintaining the type of the original error;
// It also adds pairs of fields in a field=val format to message.
func Wrap(err error, msg string, fields ...any) error {
if err == nil {
return New(msg, fields...)
}
return newError(eris.Wrap(unwrap(err), buildErrorMessage(msg, fields)))
}
// Wrapf adds additional context to all error types while maintaining the type of the original error;
// It waits for formatted input and also adds pairs of fields in a field=val format to message.
func Wrapf(err error, msg string, args ...any) error {
if err == nil {
return Errorf(msg, args...)
}
args, fields := separateArgsAndFields(msg, args)
if len(args) == 0 {
return Wrap(err, msg, args...)
}
return newError(eris.Wrapf(unwrap(err), buildErrorMessage(msg, fields), args...))
}
// Is reports whether any error in err's chain matches target.
func Is(err, target error, targets ...error) bool {
var set setError
if eris.As(err, &set) {
return set.Has(target, targets...)
}
var list listError
if eris.As(err, &list) {
return list.Has(target, targets...)
}
res := eris.Is(unwrap(err), unwrap(target))
if !res && len(targets) > 0 {
for _, t := range targets {
if eris.Is(unwrap(err), unwrap(t)) {
return true
}
}
}
return res
}
// Contains reports whether any error in err's chain contains target string.
func Contains(err error, target string) bool {
return err != nil && strings.Contains(eris.ToString(unwrap(err), false), target)
}
// ContainsErr reports whether any error in err's chain contains target string representation.
func ContainsErr(err, target error) bool {
return target != nil && Contains(err, eris.ToString(unwrap(target), false))
}
// ToJSON returns a JSON formatted map for a given error.
func ToJSON(err error) map[string]any {
return eris.ToJSON(unwrap(err), true)
}
// StackForLogger returns slice ["stack", "[...]"] that can be used as fields for logger if you want to log stack trace.
func StackForLogger(err error) []any {
jsonErr := ToJSON(err)
root, ok := jsonErr["root"].(map[string]any)
if !ok {
return nil
}
return []any{"stack", root["stack"]}
}
// Check returns true if the provided error is the one that was created using methods from this package.
func Check(err error) bool {
return eris.As(err, &errorImpl{})
}
func unwrap(err error) error {
var errObject errorImpl
if eris.As(err, &errObject) {
err = errObject.err
}
return err
}
var fieldAverageLength = 8
func buildErrorMessage(baseErr string, fields []any) string {
if len(fields) < 2 {
return baseErr
}
out := strings.Builder{}
out.Grow(len(baseErr) + len(fields)*(fieldAverageLength+1))
out.WriteString(baseErr)
for i := 0; i < len(fields); i += 2 {
if len(fields) <= i+1 {
break
}
key, ok := fields[i].(string)
if !ok {
continue
}
out.WriteRune(' ')
out.WriteString(key)
out.WriteRune('=')
out.WriteString(fmt.Sprint(fields[i+1]))
}
return out.String()
}
func separateArgsAndFields(msg string, args []any) ([]any, []any) {
var fields []any
numberOfFormats := strings.Count(msg, "%")
if numberOfFormats == 0 {
return nil, args
}
if numberOfFormats <= len(args) {
fields = args[numberOfFormats:]
args = args[:numberOfFormats]
}
return args, fields
}
// JoinErrors joins error messages using '; ' as separator (instead of '\n' like errors.Join() does).
//
// a := errm.New("first error")
// b := errm.New("second error")
// JoinErrors(a, b) // "first error; second error"
func JoinErrors(errs ...error) error {
var b []byte
for i, err := range errs {
if err == nil {
continue
}
msg := err.Error()
if msg == "" {
continue
}
if i > 0 {
b = append(b, ';', ' ')
}
b = append(b, msg...)
}
if len(b) > 0 {
return New(string(b))
}
return nil
}