-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
191 lines (161 loc) · 4 KB
/
error.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
package errors
import (
"bytes"
"fmt"
"runtime"
"strings"
"github.com/sirupsen/logrus"
)
// MaxStackDepth represents the maximum number of stackframes on any error
var MaxStackDepth = 50
// Context type, used to pass to `WithContext`.
type Context map[string]interface{}
// Error implements error interface
type Error struct {
msg string
stack []uintptr
fatal bool
ctx Context
frames []StackFrame
}
// New returns a new Error pointer
func New(format string, a ...interface{}) *Error {
stack := make([]uintptr, MaxStackDepth)
length := runtime.Callers(2, stack[:])
return &Error{
msg: fmt.Sprintf(format, a...),
stack: stack[:length],
fatal: false,
ctx: Context{},
}
}
// AddContext adds context to the Error
func (e *Error) AddContext(ctx Context) *Error {
for k, v := range ctx {
e.ctx[k] = v
}
return e
}
// String renders a context
func (c Context) String() string {
var contexts []string
for k, v := range c {
contexts = append(contexts, fmt.Sprintf("%s:%s", k, v))
}
return strings.Join(contexts, " ")
}
// Ctx is a shortcut for AddContext for adding a single
// key/val pair
func (e *Error) Ctx(key string, val interface{}) *Error {
e.ctx[key] = val
return e
}
// GetCtx returns error context
func (e *Error) GetCtx() Context {
return e.ctx
}
// Error implements error golang interface
func (e *Error) Error() string {
return e.msg
}
// ErrorWithCtx will return a string of the error with its context
func (e *Error) ErrorWithCtx() string {
return fmt.Sprintf("[%s] %s", e.ctx, e.msg)
}
// Stack returns the callstack formatted the same way that go does
// in runtime/debug.Stack()
func (e *Error) Stack() []byte {
buf := bytes.Buffer{}
for _, frame := range e.StackFrames() {
buf.WriteString(frame.String())
}
return buf.Bytes()
}
// ErrorStack returns a string that contains both the
// error message and the callstack.
func (e *Error) ErrorStack() string {
return e.Error() + "\n" + string(e.Stack())
}
// StackFrames returns an array of frames containing information about the
// stack.
func (e *Error) StackFrames() []StackFrame {
if e.frames == nil {
e.frames = make([]StackFrame, len(e.stack))
for i, pc := range e.stack {
e.frames[i] = NewStackFrame(pc)
}
}
return e.frames
}
// Fatal set error as fatal
func (e *Error) Fatal() *Error {
e.fatal = true
return e
}
// IsFatal returns true if the error is a fatal one
func (e *Error) IsFatal() bool {
return e.fatal
}
// Wrap is a shortcut for WrapSkip(e, 0)
func Wrap(e interface{}) *Error {
return WrapSkip(e, 1)
}
// WrapSkip takes an error or a string and return an Error
// object
func WrapSkip(e interface{}, skip int) *Error {
var err error
switch e := e.(type) {
case *Error:
return e
case error:
err = e
default:
err = fmt.Errorf("%v", e)
}
stack := make([]uintptr, MaxStackDepth)
length := runtime.Callers(2+skip, stack[:])
return &Error{
msg: err.Error(),
stack: stack[:length],
fatal: false,
ctx: Context{},
}
}
// LogErrors logs error with debug information
func LogErrors(log *logrus.Entry, e error) {
switch err := e.(type) {
case *Error:
if err.IsFatal() {
log.WithFields(logrus.Fields(err.GetCtx())).Error(err.Error())
log.WithFields(logrus.Fields(err.GetCtx())).Debug(err.ErrorStack())
} else {
log.WithFields(logrus.Fields(err.GetCtx())).Warning(err.Error())
log.WithFields(logrus.Fields(err.GetCtx())).Debug(err.ErrorStack())
}
case *Collector:
if err.IsFatal() {
for _, e := range err.Errors {
log.WithFields(logrus.Fields(e.GetCtx())).Error(e.Error())
log.WithFields(logrus.Fields(e.GetCtx())).Debug(e.ErrorStack())
}
} else {
for _, e := range err.Errors {
log.WithFields(logrus.Fields(e.GetCtx())).Warning(e.Error())
log.WithFields(logrus.Fields(e.GetCtx())).Debug(e.ErrorStack())
}
}
case error:
log.Error(err.Error())
}
}
// IsFatal checks if the error is fatal
func IsFatal(e error) bool {
switch err := e.(type) {
case *Error:
return err.IsFatal()
case *Collector:
return err.IsFatal()
default:
return true
}
}