-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patherrors.go
More file actions
207 lines (173 loc) · 3.89 KB
/
errors.go
File metadata and controls
207 lines (173 loc) · 3.89 KB
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
package clouderrors
import (
stderrors "errors"
"fmt"
"runtime"
"strconv"
"strings"
pkgerrors "github.com/pkg/errors"
)
type ValidationError struct {
Message string
}
var _ error = ValidationError{}
func NewValidationError(message string) ValidationError {
return ValidationError{Message: message}
}
func (v ValidationError) Error() string {
return v.Message
}
var New = stderrors.New
var Errorf = fmt.Errorf
func Wrap(err error, msg string) error {
if err == nil {
return nil
}
return Errorf("%s: %w", msg, err)
}
var As = stderrors.As
var Unwrap = stderrors.Unwrap
func Unwraps(err error) []error {
u, ok := err.(interface {
Unwrap() []error
})
if !ok {
return nil
}
return u.Unwrap()
}
func Root(err error) error {
for Unwrap(err) != nil {
err = Unwrap(err)
}
joinedErrs := Unwraps(err)
if len(joinedErrs) == 0 {
return err
}
return Roots(joinedErrs)
}
func Roots(errs []error) error {
if len(errs) == 0 {
return nil
}
rootedErrs := make([]error, len(errs))
for i, e := range errs {
rootedErrs[i] = Root(e)
}
return Join(rootedErrs...)
}
// flattens error tree
func Flatten(err error) []error {
if err == nil {
return nil
}
joinedErrs := Unwraps(err)
if joinedErrs == nil {
return []error{err}
}
flatErrs := []error{}
for _, e := range joinedErrs {
flatErrs = append(flatErrs, Flatten(e)...)
}
return flatErrs
}
// var ReturnTrace = errtrace.Wrap
func Join(errs ...error) error {
noNilErrs := make([]error, 0, len(errs))
for _, err := range errs {
if err != nil {
noNilErrs = append(noNilErrs, err)
}
}
if len(noNilErrs) == 0 {
return nil
}
if len(noNilErrs) == 1 {
return noNilErrs[0]
}
return stderrors.Join(errs...) //nolint:wrapcheck // this is a wrapper
}
// if multi err, combine similar errors
func CombineByString(err error) error {
if err == nil {
return nil
}
errs := Flatten(err)
mapE := make(map[string]error)
mapEList := []error{}
for _, e := range errs {
_, ok := mapE[e.Error()]
if !ok {
mapE[e.Error()] = e
mapEList = append(mapEList, e)
}
}
return Join(mapEList...)
}
var Is = stderrors.Is
var WrapAndTrace = WrapAndTraceInMsg
func WrapAndTraceInMsg(err error) error {
if err == nil {
return nil
}
return pkgerrors.Wrap(err, makeErrorMessage("", 0)) // this wrap also adds a stacktrace which can be nice
}
func WrapAndTrace2[T any](t T, err error) (T, error) {
if err == nil {
return t, nil
}
return t, pkgerrors.Wrap(err, makeErrorMessage("", 0))
}
func makeErrorMessage(message string, skip int) string {
skip += 2
pc, file, line, _ := runtime.Caller(skip)
funcName := "unknown"
fn := runtime.FuncForPC(pc)
if fn != nil {
funcName = fn.Name()
}
lineNum := strconv.Itoa(line)
return fmt.Sprintf("[error] %s\n%s\n%s:%s\n", message, funcName, file, lineNum)
}
func HandleErrDefer(f func() error) {
_ = f()
// logger.L().Error("", zap.Error(err))
}
func ErrorContainsAny(err error, substrs ...string) bool {
for _, substr := range substrs {
if ErrorContains(err, substr) {
return true
}
}
return false
}
func ErrorContains(err error, substr string) bool {
return err != nil && strings.Contains(err.Error(), substr)
}
func IsErrorExcept(err error, errs ...error) bool {
return err != nil && !IsAny(err, errs...)
}
func IsErrorExceptSubstr(err error, substr ...string) bool {
return err != nil && !ErrorContainsAny(err, substr...)
}
func IsAny(err error, errs ...error) bool {
for _, e := range errs {
if Is(err, e) {
return true
}
}
return false
}
// TruncateErrorForLogging truncates a long error message to a more manageable size
// while preserving the most important parts of the error message
func TruncateErrorForLogging(err error, maxLength int) error {
if err == nil {
return nil
}
errStr := err.Error()
if len(errStr) <= maxLength {
return err
}
// Otherwise truncate with indication
return New(fmt.Sprintf("ERROR (truncated): %s... (truncated)", errStr[:maxLength-20]))
}