-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
212 lines (178 loc) · 5.09 KB
/
errors.go
File metadata and controls
212 lines (178 loc) · 5.09 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
208
209
210
211
212
// Package errors provides a structured and extensible way to create, wrap, and manage errors
// in Go applications. It includes support for adding contextual information, managing error
// hierarchies, and setting attributes such as severity, HTTP status codes, and custom error codes.
//
// The package is designed to enhance error handling by allowing developers to attach additional
// metadata to errors, wrap underlying errors with more context, and facilitate debugging and
// logging. It also supports integration with alerting systems through the Alarm method.
//
// Key features include:
// - Wrapping errors with additional context.
// - Setting custom attributes like severity, status codes, and business codes.
// - Managing error stacks and hierarchies.
// - Sending alerts for critical errors.
// - Support for custom key-value pairs to enrich error information.
// - Integration with predefined error types for common scenarios.
// - Serialization errors for easy logging.
package errors
import (
se "errors"
"reflect"
)
// metadata holds the metadata for an error, including its message, severity level, etc.
type metadata struct {
// message holds the final error's message.
message string
// severity holds the severity level of the error.
severity SeverityLevel
// statusCode holds the HTTP status code recommended for an HTTP response if specified.
statusCode int
// code holds the application-specific error code.
code string
// protected marks the error as protected to avoid certain types of modifications or exposure.
protected bool
}
func (a *metadata) empty() bool {
return a.message == "" &&
a.severity == 0 &&
a.statusCode == 0 && a.code == "" &&
!a.protected
}
func (a *metadata) equal(b metadata) bool {
return a.message == b.message &&
a.severity == b.severity &&
a.statusCode == b.statusCode && a.code == b.code &&
a.protected == b.protected
}
// New creates and returns a standard Go error using the built-in errors.New function.
//
// This is implemented to maintain compatibility with existing Go error handling practices.
// However, the design philosophy of this package does not encourage the use of errors.New(msg)
// as commonly practiced in Go. Instead, it promotes the use of structured and enriched error
// handling mechanisms provided by this package.
func New(msg string) error {
return se.New(msg)
}
// Wrap wraps an existing error with a new message, effectively creating
// a new error that includes the previous error.
func Wrap(err error, message string) *Error {
var res Error
if err != nil {
res.err = err
switch x := err.(type) {
case *Error:
res = *x
x.pureWrapper = false
case *ErrorTemplate:
res.metadata = x.metadata
res.fields = cloneMap(x.fields)
case error:
break
default:
break
}
}
res.message = message
if len(res.stack) == 0 {
res.stack = CallerFramesFunc(1)
}
return &res
}
// Is checks if the error is of the same type as the target error.
func Is(err error, target error) bool {
if err == target {
return true
}
if err == nil || target == nil {
return err == target
}
switch x := err.(type) {
case *Error:
return is(x, target)
case *ErrorTemplate:
return is(x.toError(), target)
}
return se.Is(err, target)
}
// is checks if two custom errors are equal based on their attributes or if their wrapped errors are equal.
func is(e *Error, target error) bool {
switch t := target.(type) {
case *Error:
if t.pureWrapper {
return is(e, t.err)
}
if e.metadata.equal(t.metadata) || e == t {
return true
}
case *ErrorTemplate:
if t.metadata.equal(e.metadata) {
return true
}
default:
return se.Is(e.err, target)
}
if e.err == nil {
return false
}
switch x := e.err.(type) {
case *Error:
return is(x, target)
case *ErrorTemplate:
return is(x.toError(), target)
}
return se.Is(e.err, target)
}
// As checks if the error can be cast to a target type.
func As(err error, target any) bool {
if err == nil {
return false
}
if target == nil {
panic("axkit/errors: target cannot be nil")
}
if reflect.TypeOf(target).Kind() != reflect.Ptr {
panic("axkit/errors: target must be a non-nil pointer")
}
if e, ok := err.(*Error); ok {
return as(e, target)
}
if _, ok := err.(*ErrorTemplate); ok {
panic("axkit/errors: error cannot be a pointer to a ErrorTemplate")
}
return se.As(err, target)
}
// as assists the As function in casting errors to the target type, accounting for wrapped errors.
func as(e *Error, target any) bool {
switch t := target.(type) {
case **Error:
if e.metadata.equal((*t).metadata) || e == *t {
*t = e
return true
}
if e.err == nil {
return false
}
switch x := e.err.(type) {
case *Error:
return as(x, target)
case *ErrorTemplate:
if e.metadata.equal(x.metadata) {
*t = (*x).toError()
return true
}
}
case **ErrorTemplate, *ErrorTemplate:
panic("axkit/errors: target cannot be a pointer to a PredefinedError")
}
return se.As(e, target)
}
func cloneMap(m map[string]any) map[string]any {
if m == nil {
return nil
}
clone := make(map[string]any, len(m))
for k, v := range m {
clone[k] = v
}
return clone
}