Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
Latest commit
177 lines (159 loc) · 4.54 KB
/
Copy patherrors.go
File metadata and controls
177 lines (159 loc) · 4.54 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
// Package errors provides errors that have stack-traces.
// This package is a copy from github.com/go-errors/errors
package errors
import (
"bytes"
"fmt"
"reflect"
"runtime"
)
// The maximum number of stackframes on any error.
varMaxStackDepth=50
// Error is an error with an attached stacktrace. It can be used
// wherever the builtin error interface is expected.
typeCommonErrorstruct {
Errerror
stack []uintptr
frames []StackFrame
prefixstring
}
typeErrorinterface {
error
Stack() []byte
ErrorStack() string
StackFrames() []StackFrame
TypeName() string
}
// New makes an Error from the given value. If that value is already an
// error then it will be used directly, if not, it will be passed to
// fmt.Errorf("%v"). The stacktrace will point to the line of code that
// called New.
funcNew(einterface{}) *CommonError {
varerrerror
switche:=e.(type) {
caseerror:
err=e
default:
err=fmt.Errorf("%v", e)
}
stack:=make([]uintptr, MaxStackDepth)
length:=runtime.Callers(2, stack[:])
return&CommonError{
Err: err,
stack: stack[:length],
}
}
// Wrap makes an Error from the given value. If that value is already an
// error then it will be used directly, if not, it will be passed to
// fmt.Errorf("%v"). The skip parameter indicates how far up the stack
// to start the stacktrace. 0 is from the current call, 1 from its caller, etc.
funcWrap(einterface{}, skipint) *CommonError {
varerrerror
switche:=e.(type) {
case*CommonError:
returne
caseerror:
err=e
default:
err=fmt.Errorf("%v", e)
}
stack:=make([]uintptr, MaxStackDepth)
length:=runtime.Callers(2+skip, stack[:])
return&CommonError{
Err: err,
stack: stack[:length],
}
}
funcUnwrap(eerror) error {
varerrerror
switche:=e.(type) {
case*CommonError:
err=e.Err
caseerror:
err=e
default:
err=fmt.Errorf("%v", e)
}
returnerr
}
// WrapPrefix makes an Error from the given value. If that value is already an
// error then it will be used directly, if not, it will be passed to
// fmt.Errorf("%v"). The prefix parameter is used to add a prefix to the
// error message when calling Error(). The skip parameter indicates how far
// up the stack to start the stacktrace. 0 is from the current call,
// 1 from its caller, etc.
funcWrapPrefix(einterface{}, prefixstring, skipint) *CommonError {
err:=Wrap(e, skip)
iferr.prefix!="" {
err.prefix=fmt.Sprintf("%s: %s", prefix, err.prefix)
} else {
err.prefix=prefix
}
returnerr
}
// Is detects whether the error is equal to a given error. Errors
// are considered equal by this function if they are the same object,
// or if they both contain the same error inside an errors.Error.
funcIs(eerror, originalerror) bool {
ife==original {
returntrue
}
ife, ok:=e.(*CommonError); ok {
returnIs(e.Err, original)
}
iforiginal, ok:=original.(*CommonError); ok {
returnIs(e, original.Err)
}
returnfalse
}
// Errorf creates a new error with the given message. You can use it
// as a drop-in replacement for fmt.Errorf() to provide descriptive
// errors in return values.
funcErrorf(formatstring, a...interface{}) *CommonError {
returnWrap(fmt.Errorf(format, a...), 1)
}
// Error returns the underlying error's message.
func (err*CommonError) Error() string {
msg:=err.Err.Error()
iferr.prefix!="" {
msg=fmt.Sprintf("%s: %s", err.prefix, msg)
}
returnmsg
}
// Stack returns the callstack formatted the same way that go does
// in runtime/debug.Stack()
func (err*CommonError) Stack() []byte {
varbuf bytes.Buffer
for_, frame:=rangeerr.StackFrames() {
buf.WriteString(frame.String())
}
returnbuf.Bytes()
}
// ErrorStack returns a string that contains both the
// error message and the callstack.
func (err*CommonError) ErrorStack() string {
returnerr.Error() +"\n"+string(err.Stack())
}
// TypeErrorStack returns a string that contains both the
// error message and the callstack.
func (err*CommonError) TypeErrorStack() string {
returnerr.TypeName() +" "+err.Error() +"\n"+string(err.Stack())
}
// StackFrames returns an array of frames containing information about the
// stack.
func (err*CommonError) StackFrames() []StackFrame {
iferr.frames==nil {
err.frames=make([]StackFrame, len(err.stack))
fori, pc:=rangeerr.stack {
err.frames[i] =NewStackFrame(pc)
}
}
returnerr.frames
}
// TypeName returns the type this error. e.g. *errors.stringError.
func (err*CommonError) TypeName() string {
if_, ok:=err.Err.(uncaughtPanic); ok {
return"panic"
}
returnreflect.TypeOf(err.Err).String()
}