-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
298 lines (256 loc) · 7.99 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package errors
import (
"fmt"
"runtime"
"errors"
"go.opencensus.io/trace"
)
// Overwrite these values during build via -ldflags.
var (
// VERSION is the app-global version.
VERSION = "UNKNOWN"
// COMMIT is the app-global commit id.
COMMIT = "UNKNOWN"
// BRANCH is the app-global git branch.
BRANCH = "UNKNOWN"
)
const (
wrappedFunctionCallDepth = 2
)
// Tracer represents an error that has TraceContext and SourceLocation.
type Tracer interface {
SourceLocation() SourceLocation
TraceContext() TraceContext
SetTraceContext(trace.SpanContext)
SetSourceLocation(depth int)
}
// TraceContext is used to provide a tracing context to an object for logging purposes.
// This is helpful for Developers to link Stackdriver traces to Stackdriver logs.
// See https://cloud.google.com/trace/docs/viewing-details.
// See https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry.
type TraceContext struct {
TraceID string `json:"trace"`
SpanID string `json:"spanId"`
}
// SourceLocation provides the information where the actual error happened in the code.
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogEntrySourceLocation
type SourceLocation struct {
Function string `json:"function"`
File string `json:"file"`
Line int `json:"line"`
Version string `json:"version,omitempty"`
Commit string `json:"commit,omitempty"`
Branch string `json:"branch,omitempty"`
}
// NewSourceLocation creates a SourceLocation using stdlib runtime.Caller.
func NewSourceLocation(depth int) SourceLocation {
function, file, line, _ := runtime.Caller(depth)
return SourceLocation{
runtime.FuncForPC(function).Name(), file, line, VERSION, COMMIT, BRANCH,
}
}
// As is a drop-in replacement for errors.As method.
func As(target error, dest interface{}) bool {
return errors.As(target, dest)
}
// Is is a drop-in replacement for errors.Is method.
func Is(err, target error) bool {
return errors.Is(err, target)
}
// ErrorTracer represents an error with tracing.
type ErrorTracer interface {
error
// Unwrap is used to implement the Go 1.13 error wrapping technique.
// This makes the implementation of ErrorTracer to make use of errors.Is and errors.As.
Unwrap() error
Tracer
}
// Compile time implementation check.
var _ ErrorTracer = &errorContext{}
type errorContext struct {
err error
sourceLocation SourceLocation
traceContext TraceContext
}
func (e *errorContext) Unwrap() error {
return e.err
}
func (e *errorContext) Error() string {
return e.err.Error()
}
func (e *errorContext) SourceLocation() SourceLocation {
return e.sourceLocation
}
func (e *errorContext) SetSourceLocation(depth int) {
e.sourceLocation = NewSourceLocation(depth)
}
func (e *errorContext) TraceContext() TraceContext {
return e.traceContext
}
func (e *errorContext) SetTraceContext(t trace.SpanContext) {
e.traceContext = TraceContext{
TraceID: t.TraceID.String(),
SpanID: t.SpanID.String(),
}
}
// NewCaller wraps errors.New with a specified caller depth.
func NewCaller(depth int, m string) error {
err := &errorContext{
err: errors.New(m),
sourceLocation: NewSourceLocation(depth),
}
return err
}
// NewCallerT wraps errors.New with a specified caller depth and a span trace context.
func NewCallerT(depth int, span *trace.Span, m string) error {
err := &errorContext{
err: errors.New(m),
sourceLocation: NewSourceLocation(depth),
}
return annotate(err, span)
}
// NewCallerf wraps fmt.Errorf with a specified caller depth.
func NewCallerf(depth int, m string, args ...interface{}) error {
err := &errorContext{
err: fmt.Errorf(m, args...),
sourceLocation: NewSourceLocation(depth),
}
return err
}
// NewCallerfT wraps fmt.Errorf with a specified caller depth and a span trace context.
func NewCallerfT(depth int, span *trace.Span, m string, args ...interface{}) error {
err := &errorContext{
err: fmt.Errorf(m, args...),
sourceLocation: NewSourceLocation(depth),
}
return annotate(err, span)
}
// WrapCaller wraps fmt.Errorf with a specified caller depth.
func WrapCaller(depth int, e error, m string) error {
err := &errorContext{
err: fmt.Errorf("%s: %w", m, e),
sourceLocation: NewSourceLocation(depth),
}
return err
}
// WrapCallerT wraps fmt.Errorf with a specified caller depth with a span trace context.
func WrapCallerT(depth int, span *trace.Span, e error, m string) error {
err := &errorContext{
err: fmt.Errorf("%s: %w", m, e),
sourceLocation: NewSourceLocation(depth),
}
return annotate(err, span)
}
// WrapCallerf wraps fmt.Errorf with a specified caller depth.
func WrapCallerf(depth int, e error, format string, args ...interface{}) error {
m := fmt.Sprintf(format, args...)
err := &errorContext{
err: fmt.Errorf("%s: %w", m, e),
sourceLocation: NewSourceLocation(depth),
}
return err
}
// WrapCallerfT wraps fmt.Errorf with a specified caller depth with a span trace context.
func WrapCallerfT(depth int, span *trace.Span, e error, format string, args ...interface{}) error {
m := fmt.Sprintf(format, args...)
err := &errorContext{
err: fmt.Errorf("%s: %w", m, e),
sourceLocation: NewSourceLocation(depth),
}
return annotate(err, span)
}
// New is the drop-in replacement for errors.New.
func New(m string) error {
err := &errorContext{
err: errors.New(m),
sourceLocation: NewSourceLocation(wrappedFunctionCallDepth),
}
return err
}
// NewT wraps errors.New with a span trace context.
func NewT(span *trace.Span, m string) error {
err := &errorContext{
err: errors.New(m),
sourceLocation: NewSourceLocation(wrappedFunctionCallDepth),
}
return annotate(err, span)
}
// Errorf wraps fmt.Errorf.
func Errorf(m string, args ...interface{}) error {
err := &errorContext{
err: fmt.Errorf(m, args...),
sourceLocation: NewSourceLocation(wrappedFunctionCallDepth),
}
return err
}
// ErrorfT wraps fmt.Errorf with a span trace context.
func ErrorfT(span *trace.Span, m string, args ...interface{}) error {
err := &errorContext{
err: fmt.Errorf(m, args...),
sourceLocation: NewSourceLocation(wrappedFunctionCallDepth),
}
return annotate(err, span)
}
// Wrap wraps an error fmt.Errorf with `%w` without formatting.
func Wrap(e error, m string) error {
err := &errorContext{
err: fmt.Errorf("%s: %w", m, e),
sourceLocation: NewSourceLocation(wrappedFunctionCallDepth),
}
return err
}
// WrapT wraps an error with a span trace context.
func WrapT(span *trace.Span, e error, m string) error {
err := &errorContext{
err: fmt.Errorf("%s: %w", m, e),
sourceLocation: NewSourceLocation(wrappedFunctionCallDepth),
}
return annotate(err, span)
}
// Wrap wraps fmt.Errorf with `%w` with formatting.
func Wrapf(e error, f string, args ...interface{}) error {
m := fmt.Sprintf(f, args...)
err := &errorContext{
err: fmt.Errorf("%s: %w", m, e),
sourceLocation: NewSourceLocation(wrappedFunctionCallDepth),
}
return err
}
// WrapfT is Wrapf with a trace context.
func WrapfT(span *trace.Span, e error, f string, args ...interface{}) error {
m := fmt.Sprintf(f, args...)
err := &errorContext{
err: fmt.Errorf("%s: %w", m, e),
sourceLocation: NewSourceLocation(wrappedFunctionCallDepth),
}
return annotate(err, span)
}
func annotate(e *errorContext, span *trace.Span) error {
if span == nil {
return e
}
// Add the trace ID and span ID.
ctx := span.SpanContext()
e.traceContext = TraceContext{
TraceID: ctx.TraceID.String(),
SpanID: ctx.SpanID.String(),
}
// Add OpenCensus span annotation.
src := e.SourceLocation()
span.Annotate(
[]trace.Attribute{
trace.StringAttribute("function", src.Function),
trace.StringAttribute("file", src.File),
trace.Int64Attribute("line", int64(src.Line)),
trace.StringAttribute("version", src.Version),
trace.StringAttribute("commit", src.Commit),
trace.StringAttribute("branch", src.Branch),
},
"Error: "+e.Error(),
)
// Generic error
span.SetStatus(trace.Status{
Code: trace.StatusCodeUnknown,
})
return e
}