-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathoops.go
More file actions
209 lines (173 loc) · 6.27 KB
/
oops.go
File metadata and controls
209 lines (173 loc) · 6.27 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
package oops
import (
"context"
"errors"
"net/http"
"slices"
"time"
)
// Wrap wraps an error into an `oops.OopsError` object that satisfies `error`.
func Wrap(err error) error {
if err == nil {
return nil
}
return newBuilder().Wrap(err)
}
// Wrapf wraps an error into an `oops.OopsError` object that satisfies `error` and formats an error message.
func Wrapf(err error, format string, args ...any) error {
if err == nil {
return nil
}
return newBuilder().Wrapf(err, format, args...)
}
// New returns `oops.OopsError` object that satisfies `error`.
func New(message string) error {
return newBuilder().New(message)
}
// Errorf formats an error and returns `oops.OopsError` object that satisfies `error`.
func Errorf(format string, args ...any) error {
return newBuilder().Errorf(format, args...)
}
func FromContext(ctx context.Context) OopsErrorBuilder {
builder, ok := getBuilderFromContext(ctx)
if !ok {
return newBuilder()
}
return builder
}
func Join(e ...error) error {
return newBuilder().Join(e...)
}
// Recover handle panic and returns `oops.OopsError` object that satisfies `error`.
func Recover(cb func()) (err error) {
return newBuilder().Recover(cb)
}
// Recoverf handle panic and returns `oops.OopsError` object that satisfies `error` and formats an error message.
func Recoverf(cb func(), msg string, args ...any) (err error) {
return newBuilder().Recoverf(cb, msg, args...)
}
// Assert panics if condition is false. Panic payload will be of type oops.OopsError.
// Assertions can be chained.
func Assert(condition bool) OopsErrorBuilder {
return newBuilder().Assert(condition)
}
// Assertf panics if condition is false. Panic payload will be of type oops.OopsError.
// Assertions can be chained.
func Assertf(condition bool, msg string, args ...any) OopsErrorBuilder {
return newBuilder().Assertf(condition, msg, args...)
}
// Code set a code or slug that describes the error.
// Error messages are intended to be read by humans, but such code is expected to
// be read by machines and even transported over different services.
func Code(code any) OopsErrorBuilder {
return newBuilder().Code(code)
}
// Time set the error time.
// Default: `time.Now()`.
func Time(time time.Time) OopsErrorBuilder {
return newBuilder().Time(time)
}
// Since set the error duration.
func Since(time time.Time) OopsErrorBuilder {
return newBuilder().Since(time)
}
// Duration set the error duration.
func Duration(duration time.Duration) OopsErrorBuilder {
return newBuilder().Duration(duration)
}
// In set the feature category or domain.
func In(domain string) OopsErrorBuilder {
return newBuilder().In(domain)
}
// Tags adds multiple tags, describing the feature returning an error.
func Tags(tags ...string) OopsErrorBuilder {
return newBuilder().Tags(tags...)
}
// Trace set a transaction id, trace id or correlation id...
func Trace(trace string) OopsErrorBuilder {
return newBuilder().Trace(trace)
}
// Span represents a unit of work or operation.
func Span(span string) OopsErrorBuilder {
return newBuilder().Span(span)
}
// With supplies a list of attributes declared by pair of key+value.
func With(kv ...any) OopsErrorBuilder {
return newBuilder().With(kv...)
}
// WithContext supplies a list of values declared in context.
func WithContext(ctx context.Context, keys ...any) OopsErrorBuilder {
return newBuilder().WithContext(ctx, keys...)
}
// Hint set a hint for faster debugging.
func Hint(hint string) OopsErrorBuilder {
return newBuilder().Hint(hint)
}
// Public sets a message that is safe to show to an end user.
func Public(public string) OopsErrorBuilder {
return newBuilder().Public(public)
}
// Owner set the name/email of the colleague/team responsible for handling this error.
// Useful for alerting purpose.
func Owner(owner string) OopsErrorBuilder {
return newBuilder().Owner(owner)
}
// User supplies a user id with optional attributes.
// Attributes can be provided as alternating string key/value pairs,
// map[string]any values, and slog.Attr values.
func User(userID string, data ...any) OopsErrorBuilder {
return newBuilder().User(userID, data...)
}
// Tenant supplies a tenant id with optional attributes.
// Attributes can be provided as alternating string key/value pairs,
// map[string]any values, and slog.Attr values.
func Tenant(tenantID string, data ...any) OopsErrorBuilder {
return newBuilder().Tenant(tenantID, data...)
}
// Request supplies a http.Request.
func Request(req *http.Request, withBody bool) OopsErrorBuilder {
return newBuilder().Request(req, withBody)
}
// Response supplies a http.Response.
func Response(res *http.Response, withBody bool) OopsErrorBuilder {
return newBuilder().Response(res, withBody)
}
// CallerSkip sets the number of additional callers to skip when capturing
// the stack trace. This is useful when oops is wrapped in helper functions.
func CallerSkip(skip int) OopsErrorBuilder {
return newBuilder().CallerSkip(skip)
}
// FrameSkip registers a frame filter that excludes matching frames from stack trace
// output. Both file and fun are matched using strings.Contains against the raw values
// returned by runtime.CallersFrames — the absolute file path and the fully-qualified
// function name respectively. An empty string matches anything (i.e., acts as a wildcard).
//
// Filtering is applied at output time (when Stacktrace(), Sources(), or StackFrames()
// is called), not at error creation time. This means patterns registered after an error
// is created will still filter frames from that error's stack trace.
//
// Calling FrameSkip with the same (file, fun) pair more than once is a no-op —
// duplicate entries are silently ignored.
//
// Example:
//
// oops.FrameSkip("myproject/pkg/errutil", "") // match by file path substring
// oops.FrameSkip("", "myproject/pkg/errutil.WrapErr") // match by full function name substring
func FrameSkip(file string, fun string) {
entry := oopsStacktraceFrame{file: file, function: fun}
if slices.Contains(framesSkip, entry) {
return
}
framesSkip = append(framesSkip, entry)
}
// GetPublic returns a message that is safe to show to an end user, or a default generic message.
func GetPublic(err error, defaultPublicMessage string) string {
var oopsError OopsError
if errors.As(err, &oopsError) {
msg := oopsError.Public()
if len(msg) > 0 {
return msg
}
}
return defaultPublicMessage
}