-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
131 lines (113 loc) · 2.79 KB
/
errors.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
package errors
import (
"fmt"
"reflect"
)
type Data map[string]any
// New returns an error with the provided message.
func New(msg string) error {
return &Err{
Message: msg,
Stack: callers(),
}
}
// Errord returns an error with additional data and the provided message.
func Errord(data Data, msg string) error {
return &Err{
Message: msg,
Data: data,
Stack: callers(),
}
}
// Errorf returns an error with the provided format specifier.
func Errorf(format string, args ...any) error {
return &Err{
Message: fmt.Sprintf(format, args...),
Stack: callers(),
}
}
// Errordf returns an error with additional data and the provided format specifier.
func Errordf(data Data, format string, args ...any) error {
return &Err{
Message: fmt.Sprintf(format, args...),
Data: data,
Stack: callers(),
}
}
// Wrap returns an error wrapping err and adding the provided format specifier.
func Wrap(err error, msg string) error {
return &Err{
Message: msg,
Stack: callers(),
Cause: err,
}
}
// Wrapd returns an error wrapping err, adding additional data and the provided message.
func Wrapd(err error, data Data, msg string) error {
return &Err{
Message: msg,
Data: data,
Stack: callers(),
Cause: err,
}
}
// Wrapf returns an error wrapping err and adding the provided format specifier.
func Wrapf(err error, format string, args ...any) error {
return &Err{
Message: fmt.Sprintf(format, args...),
Stack: callers(),
Cause: err,
}
}
// Wrapdf returns an error wrapping err, adding additional data and the provided format specifier.
func Wrapdf(err error, data Data, format string, args ...any) error {
return &Err{
Message: fmt.Sprintf(format, args...),
Data: data,
Stack: callers(),
Cause: err,
}
}
// WithStack adds a stack trace to the provided error if it is an Err or *Err.
func WithStack(err error) error {
if e, ok := err.(Err); ok {
e.Stack = callers()
return e
} else if e, ok := err.(*Err); ok {
e.Stack = callers()
return e
} else {
return err
}
}
// WithCause adds a cause to the provided error if it is an Err or *Err.
func WithCause(err error, cause error) error {
if e, ok := err.(Err); ok {
e.Cause = cause
return e
} else if e, ok := err.(*Err); ok {
e.Cause = cause
return e
} else {
return err
}
}
// IsErrComposition returns true if the provided error is a composition of Err or *Err.
func IsErrComposition(err error) bool {
typeOfErr := reflect.TypeOf(err)
if typeOfErr.Kind() == reflect.Pointer {
typeOfErr = typeOfErr.Elem()
}
if typeOfErr.Kind() != reflect.Struct {
return false
}
for i := 0; i < typeOfErr.NumField(); i++ {
if typeOfErr.Field(i).Type == reflect.TypeOf(Err{}) {
return true
}
if typeOfErr.Field(i).Type == reflect.TypeOf((*Err)(nil)) {
return true
}
}
return false
}