forked from alexkappa/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
139 lines (124 loc) · 3.18 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
package errors
import (
"bytes"
"path/filepath"
"runtime"
"strconv"
)
type Error interface {
// Message returns the error message of the error.
Message() string
// Inner returns the inner error that this error wraps.
Inner() error
// Stack returns the stack trace that led to the error.
Stack() Frames
// Error satisfies the standard library error interface.
Error() string
}
// Type errtype is the default implementation of the Error interface. It is not
// exported so users can only use it via the New or Wrap functions.
type errtype struct {
M string `json:"message"`
I error `json:"inner,omitempty"`
S Frames `json:"stack,omitempty"`
}
// Message returns the error message of the error.
func (t *errtype) Message() string {
return t.M
}
// Inner returns the inner error that this error wraps.
func (t *errtype) Stack() Frames {
return t.S
}
// Stack returns the stack trace that led to the error.
func (t *errtype) Inner() error {
return t.I
}
// Error satisfies the standard library error interface.
func (t *errtype) Error() string {
var buf bytes.Buffer
buf.WriteString(t.M)
if t.I != nil {
buf.WriteByte('.')
buf.WriteByte(' ')
buf.WriteString(t.I.Error())
}
if t.S != nil {
buf.WriteByte(' ')
buf.WriteByte('[')
buf.WriteString(t.S.String())
buf.WriteByte(']')
}
return buf.String()
}
// New creates a new Error with the supplied message.
func New(message string) Error {
return new(message, 3)
}
func new(message string, skip int) Error {
return &errtype{
M: message,
S: Stack(skip),
}
}
// Wrap creates a new Error that wraps err.
func Wrap(err error, message string) Error {
return wrap(err, message, 3)
}
func wrap(err error, message string, skip int) Error {
if errT, ok := err.(*errtype); ok {
errT.S = nil // drop the stack trace of the inner error.
} else {
err = &errtype{M: err.Error()}
}
return &errtype{
M: message,
I: err,
S: Stack(skip),
}
}
// Frame contains information for a single stack frame.
type Frame struct {
// File is the path to the file of the caller.
File string `json:"file"`
// Line is the line in the file where the function call was made.
Line int `json:"line"`
// Func is the name of the caller.
Func string `json:"func"`
}
type Frames []Frame
// String is used to satisfy the fmt.Stringer interface. It formats the stack
// trace as a comma separated list of "file:line function".
func (f Frames) String() string {
var buf bytes.Buffer
for i, frame := range f {
buf.WriteString(frame.Func)
buf.WriteByte('(')
buf.WriteString(frame.File)
buf.WriteByte(':')
buf.WriteString(strconv.Itoa(frame.Line))
buf.WriteByte(')')
if i < len(f)-1 {
buf.WriteByte(',')
}
}
return buf.String()
}
// Stack returns the stack trace of the function call, while skipping the first
// skip frames.
func Stack(skip int) Frames {
callers := make([]uintptr, 10)
n := runtime.Callers(skip, callers)
callers = callers[:n-2] // skip runtime.main and runtime.goexit function calls
frames := make(Frames, len(callers))
for i, caller := range callers {
fn := runtime.FuncForPC(caller)
file, line := fn.FileLine(caller)
frames[i] = Frame{
File: filepath.Base(file),
Line: line,
Func: fn.Name(),
}
}
return frames
}