-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
185 lines (163 loc) · 2.89 KB
/
error.go
File metadata and controls
185 lines (163 loc) · 2.89 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
178
179
180
181
182
183
184
185
package btw
import (
"errors"
"fmt"
"path"
"runtime"
"strings"
)
var (
MaxStackDepth int = 256
MaxColumnWidth int = 25
)
type Error struct {
Err error
Stack []Frame
Context []Layer
}
type Frame struct {
Func string
File string
Line int
}
type Layer struct {
Func string
Values []string
}
func (e *Error) Error() string {
return e.Err.Error()
}
func (e *Error) Unwrap() error {
return e.Err
}
func Trace(err error) error {
if err == nil {
return nil
}
e, ok := err.(*Error)
if !ok || len(e.Stack) != 0 {
e = &Error{Err: err}
}
pc := make([]uintptr, MaxStackDepth)
n := runtime.Callers(2, pc)
if n == 0 {
return e
}
frames := runtime.CallersFrames(pc[:n])
for {
f, more := frames.Next()
e.Stack = append(e.Stack, Frame{
Func: f.Function,
File: f.File,
Line: f.Line,
})
if !more {
break
}
}
return e
}
func Attach(err error, ctx ...interface{}) error {
if err == nil {
return nil
}
e, ok := err.(*Error)
if !ok {
e = &Error{Err: err}
}
ctx = ctx[:len(ctx)/2*2]
if len(ctx) == 0 {
return e
}
var l Layer
if pc, _, _, ok := runtime.Caller(1); ok {
l.Func = runtime.FuncForPC(pc).Name()
} else {
l.Func = "???"
}
l.Values = make([]string, len(ctx))
for i, v := range ctx {
l.Values[i] = fmt.Sprintf("%+v", v)
}
e.Context = append(e.Context, l)
return e
}
func Format(err error) string {
b := new(strings.Builder)
fmt.Fprintf(b, "error: %v", err)
level := 1
for err != nil {
e, _ := err.(*Error)
if e != nil {
formatStack(level, e, b)
formatContext(level, e, b)
level++
}
err = errors.Unwrap(err)
}
return b.String()
}
func formatStack(l int, e *Error, b *strings.Builder) {
var count int
var prev string
for i := 0; i < len(e.Stack); i++ {
curr := fmt.Sprintf("\n%sfrom %s() at %s:%d",
strings.Repeat("\t", l),
path.Base(e.Stack[i].Func),
e.Stack[i].File,
e.Stack[i].Line,
)
if prev == curr {
count++
continue
}
if count != 0 {
fmt.Fprintf(b, "\n%s ... * %d",
strings.Repeat("\t", l), count+1,
)
count = 0
}
b.WriteString(curr)
prev = curr
}
}
func formatContext(l int, e *Error, b *strings.Builder) {
var maxWidth [2]int
for _, layer := range e.Context {
for i, item := range layer.Values {
if n := len(item); n <= MaxColumnWidth {
maxWidth[i%2] = max(maxWidth[i%2], n)
}
}
}
tags := make(map[string]struct{})
for _, layer := range e.Context {
for i := 0; i < len(layer.Values)-1; i += 2 {
if _, ok := tags[layer.Values[i]]; ok {
continue
}
tags[layer.Values[i]] = struct{}{}
fmt.Fprintf(b, "\n%s[ %-[2]*[3]s ]",
strings.Repeat("\t", l),
maxWidth[0],
layer.Values[i],
)
fmt.Fprintf(b, " %[1]*[2]s",
maxWidth[1],
layer.Values[i+1],
)
if i == 0 {
fmt.Fprintf(b, " by %s()",
path.Base(layer.Func),
)
}
}
}
}
func max(a, b int) int {
if a > b {
return a
} else {
return b
}
}