-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec.go
144 lines (122 loc) · 2.9 KB
/
ec.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
// el: error logging
package el
import (
"errors"
"fmt"
"io"
"runtime"
"strings"
)
type node struct {
previous error
info interface{}
file string
line int
fn string
function *runtime.Func
}
// New error chain node.
func newNode(err error, fn string, info interface{}, level int) error {
if err == nil {
switch v := info.(type) {
case nil:
err = ErrVoid
case string:
if len(v) == 0 {
err = ErrVoid
} else {
err = errors.New(v)
}
case error:
err = v
default:
err = errors.New(fmt.Sprintf("%v", info))
}
info = nil
}
pc, file, line, _ := runtime.Caller(level)
return &node{err, info, file, line, fn, runtime.FuncForPC(pc)}
}
// Wrap error with info and stacked error(s).
func Wrap(err error, info interface{}) error {
return newNode(err, "", info, 2)
}
// Wrap error with fn, info and stacked error(s).
func WrapFn(err error, fn string, info interface{}) error {
return newNode(err, fn, info, 2)
}
// Take the string argument as a format string to generate the info for Wrap().
func Wrapf(err error, format string, a ...interface{}) error {
return newNode(err, "", fmt.Sprintf(format, a...), 2)
}
// Take the string argument as a format string to generate the info for WrapFn().
func WrapFnf(err error, fn, format string, a ...interface{}) error {
return newNode(err, fn, fmt.Sprintf(format, a...), 2)
}
const prefixBlank = "\n "
// dump to buffer
func (e *node) dump(out *strings.Builder) *strings.Builder {
e.Dump(out)
return out
}
func (e *node) print(out io.StringWriter) {
fileName, functionName := e.GetName()
_, _ = out.WriteString(fmt.Sprintf("%v:%v: %v()", fileName, e.line, functionName))
if e.info != nil {
info, ok := e.info.(string)
if !ok {
info = fmt.Sprintf("%v", e.info)
}
if len(info) > 0 {
for _, s := range strings.Split(info, "\n") {
_, _ = out.WriteString(prefixBlank)
_, _ = out.WriteString(s)
}
}
}
}
// Dump to output stream
func (e *node) Dump(out io.StringWriter) {
e.print(out)
var err error
for err = e.previous; err != nil; {
switch n := err.(type) {
case *node:
_, _ = out.WriteString("\n")
n.print(out)
err = n.previous
default:
for _, s := range strings.Split(err.Error(), "\n") {
_, _ = out.WriteString(prefixBlank)
_, _ = out.WriteString(s)
}
return
}
}
}
func (e *node) Error() string {
return e.dump(&strings.Builder{}).String()
}
func (e *node) Unwrap() (err error) {
return e.previous
}
func (e *node) Info() interface{} {
return e.info
}
func (e *node) Location() (file string, line int, function *runtime.Func) {
return e.file, e.line, e.function
}
func (e *node) GetName() (fileName, functionName string) {
functionName = e.function.Name()
if e.fn != "" {
if n := strings.LastIndex(functionName, "."); n > 0 {
functionName = functionName[:n+1] + e.fn
}
}
fileName = e.file
return
}
func (e *node) GetLine() (line int) {
line = e.line
return
}