This repository has been archived by the owner on Apr 23, 2018. It is now read-only.
forked from gravitational/trace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.go
381 lines (333 loc) · 9.12 KB
/
trace.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package trace implements utility functions for capturing debugging
// information about file and line in error reports and logs.
package trace
import (
"encoding/json"
"fmt"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"golang.org/x/net/context"
)
var debug int32
// SetDebug turns on/off debugging mode, that causes Fatalf to panic
func SetDebug(enabled bool) {
if enabled {
atomic.StoreInt32(&debug, 1)
} else {
atomic.StoreInt32(&debug, 0)
}
}
// IsDebug returns true if debug mode is on, false otherwize
func IsDebug() bool {
return atomic.LoadInt32(&debug) == 1
}
// Wrap takes the original error and wraps it into the Trace struct
// memorizing the context of the error.
func Wrap(err error, args ...interface{}) Error {
if len(args) > 0 {
format := args[0]
args = args[1:]
return WrapWithMessage(err, format, args...)
}
return wrapWithDepth(err, 2)
}
// Unwrap unwraps error to it's original error
func Unwrap(err error) error {
if terr, ok := err.(Error); ok {
return terr.OrigError()
}
return err
}
// UserMessage returns user-friendly part of the error
func UserMessage(err error) string {
if err == nil {
return ""
}
if wrap, ok := err.(Error); ok {
return wrap.UserMessage()
}
return err.Error()
}
// DebugReport returns debug report with all known information
// about the error including stack trace if it was captured
func DebugReport(err error) string {
if err == nil {
return ""
}
if wrap, ok := err.(Error); ok {
return wrap.DebugReport()
}
return err.Error()
}
// WrapWithMessage wraps the original error into Error and adds user message if any
func WrapWithMessage(err error, message interface{}, args ...interface{}) Error {
trace := wrapWithDepth(err, 3)
if trace != nil {
trace.AddUserMessage(message, args...)
}
return trace
}
func wrapWithDepth(err error, depth int) Error {
if err == nil {
return nil
}
var trace Error
if wrapped, ok := err.(Error); ok {
trace = wrapped
} else {
trace = newTrace(depth+1, err)
}
return trace
}
// Errorf is similar to fmt.Errorf except that it captures
// more information about the origin of error, such as
// callee, line number and function that simplifies debugging
func Errorf(format string, args ...interface{}) (err error) {
err = fmt.Errorf(format, args...)
trace := wrapWithDepth(err, 2)
trace.AddUserMessage(format, args...)
return trace
}
// Fatalf - If debug is false Fatalf calls Errorf. If debug is
// true Fatalf calls panic
func Fatalf(format string, args ...interface{}) error {
if IsDebug() {
panic(fmt.Sprintf(format, args))
} else {
return Errorf(format, args)
}
}
func newTrace(depth int, err error) *TraceErr {
var pc [32]uintptr
count := runtime.Callers(depth+1, pc[:])
traces := make(Traces, count)
for i := 0; i < count; i++ {
fn := runtime.FuncForPC(pc[i])
filePath, line := fn.FileLine(pc[i])
traces[i] = Trace{
Func: fn.Name(),
Path: filePath,
Line: line,
}
}
return &TraceErr{
err,
traces,
"",
}
}
// Traces is a list of trace entries
type Traces []Trace
// SetTraces adds new traces to the list
func (s Traces) SetTraces(traces ...Trace) {
s = append(s, traces...)
}
// Func returns first function in trace list
func (s Traces) Func() string {
if len(s) == 0 {
return ""
}
return s[0].Func
}
// Func returns just function name
func (s Traces) FuncName() string {
if len(s) == 0 {
return ""
}
fn := filepath.ToSlash(s[0].Func)
idx := strings.LastIndex(fn, "/")
if idx == -1 || idx == len(fn)-1 {
return fn
}
return fn[idx+1:]
}
// Loc points to file/line location in the code
func (s Traces) Loc() string {
if len(s) == 0 {
return ""
}
return s[0].String()
}
// String returns debug-friendly representaton of trace stack
func (s Traces) String() string {
if len(s) == 0 {
return ""
}
out := make([]string, len(s))
for i, t := range s {
out[i] = fmt.Sprintf("\t%v:%v %v", t.Path, t.Line, t.Func)
}
return strings.Join(out, "\n")
}
// Trace stores structured trace entry, including file line and path
type Trace struct {
// Path is a full file path
Path string `json:"path"`
// Func is a function name
Func string `json:"func"`
// Line is a code line number
Line int `json:"line"`
}
// String returns debug-friendly representation of this trace
func (t *Trace) String() string {
dir, file := filepath.Split(t.Path)
dirs := strings.Split(filepath.ToSlash(filepath.Clean(dir)), "/")
if len(dirs) != 0 {
file = filepath.Join(dirs[len(dirs)-1], file)
}
return fmt.Sprintf("%v:%v", file, t.Line)
}
// TraceErr contains error message and some additional
// information about the error origin
type TraceErr struct {
Err error `json:"error"`
Traces `json:"traces"`
Message string `json:"message,omitemtpy"`
}
type RawTrace struct {
Err json.RawMessage `json:"error"`
Traces `json:"traces"`
Message string `json:"message"`
}
// AddUserMessage adds user-friendly message describing the error nature
func (e *TraceErr) AddUserMessage(formatArg interface{}, rest ...interface{}) {
newMessage := fmt.Sprintf(fmt.Sprintf("%v", formatArg), rest...)
if len(e.Message) == 0 {
e.Message = newMessage
} else {
e.Message = strings.Join([]string{e.Message, newMessage}, ", ")
}
}
// UserMessage returns user-friendly error message
func (e *TraceErr) UserMessage() string {
if e.Message != "" {
return e.Message
}
return e.Err.Error()
}
// DebugReport returns develeoper-friendly error report
func (e *TraceErr) DebugReport() string {
return fmt.Sprintf("\nERROR REPORT:\nOriginal Error: %T %v\nStack Trace:\n%v\nUser Message: %v\n", e.Err, e.Err.Error(), e.Traces.String(), e.Message)
}
// Error returns user-friendly error message when not in debug mode
func (e *TraceErr) Error() string {
if IsDebug() {
return e.DebugReport()
}
return e.UserMessage()
}
// OrigError returns original wrapped error
func (e *TraceErr) OrigError() error {
err := e.Err
// this is not an endless loop because I'm being
// paranoid, this is a safe protection against endless
// loops
for i := 0; i < maxHops; i++ {
newerr, ok := err.(Error)
if !ok {
break
}
if newerr.OrigError() != err {
err = newerr.OrigError()
}
}
return err
}
// maxHops is a max supported nested depth for errors
const maxHops = 50
// Error is an interface that helps to adapt usage of trace in the code
// When applications define new error types, they can implement the interface
// So error handlers can use OrigError() to retrieve error from the wrapper
type Error interface {
error
// OrigError returns original error wrapped in this error
OrigError() error
// AddMessage adds formatted user-facing message
// to the error, depends on the implementation,
// usually works as fmt.Sprintf(formatArg, rest...)
// but implementations can choose another way, e.g. treat
// arguments as structured args
AddUserMessage(formatArg interface{}, rest ...interface{})
// UserMessage returns user-friendly error message
UserMessage() string
// DebugReport returns develeoper-friendly error report
DebugReport() string
}
// NewAggregate creates a new aggregate instance from the specified
// list of errors
func NewAggregate(errs ...error) error {
// filter out possible nil values
var nonNils []error
for _, err := range errs {
if err != nil {
nonNils = append(nonNils, err)
}
}
if len(nonNils) == 0 {
return nil
}
return wrapWithDepth(aggregate(nonNils), 2)
}
// NewAggregateFromChannel creates a new aggregate instance from the provided
// errors channel.
//
// A context.Context can be passed in so the caller has the ability to cancel
// the operation. If this is not desired, simply pass context.Background().
func NewAggregateFromChannel(errCh chan error, ctx context.Context) error {
var errs []error
Loop:
for {
select {
case err, ok := <-errCh:
if !ok { // the channel is closed, time to exit
break Loop
}
errs = append(errs, err)
case <-ctx.Done():
break Loop
}
}
return NewAggregate(errs...)
}
// Aggregate interface combines several errors into one error
type Aggregate interface {
error
// Errors obtains the list of errors this aggregate combines
Errors() []error
}
// aggregate implements Aggregate
type aggregate []error
// Error implements the error interface
func (r aggregate) Error() string {
if len(r) == 0 {
return ""
}
output := r[0].Error()
for i := 1; i < len(r); i++ {
output = fmt.Sprintf("%v, %v", output, r[i])
}
return output
}
// Errors obtains the list of errors this aggregate combines
func (r aggregate) Errors() []error {
return []error(r)
}
// IsAggregate returns whether this error of Aggregate error type
func IsAggregate(err error) bool {
_, ok := Unwrap(err).(Aggregate)
return ok
}