-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors_stack.go
More file actions
83 lines (76 loc) · 2.04 KB
/
errors_stack.go
File metadata and controls
83 lines (76 loc) · 2.04 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
package getstream
import (
"fmt"
"io"
"runtime"
)
// stackErr wraps an error with a single captured stack frame at the
// wrap site. It is fully compatible with errors.Is / errors.As / errors.Unwrap.
//
// The %+v verb on fmt.Sprintf prints the full chain including the captured
// frame; %v / %s print only the formatted message of this layer.
type stackErr struct {
msg string
cause error
pc [32]uintptr
n int
}
// stackWrap wraps err with the call site captured via runtime.Callers.
// The wrap site is rendered by Format("%+v") and accessible via
// StackTrace(). Use stackWrap instead of fmt.Errorf("...: %w", ...) in
// user-facing paths so the original wrap site survives the cause chain.
func stackWrap(err error, msg string) error {
if err == nil {
return nil
}
se := &stackErr{msg: msg, cause: err}
se.n = runtime.Callers(2, se.pc[:])
return se
}
func (e *stackErr) Error() string {
if e.msg == "" {
return e.cause.Error()
}
return e.msg + ": " + e.cause.Error()
}
func (e *stackErr) Unwrap() error { return e.cause }
// StackTrace returns the captured frames as "func\n\tfile:line" entries.
// Returns nil if no frames were captured.
func (e *stackErr) StackTrace() []string {
if e.n == 0 {
return nil
}
frames := runtime.CallersFrames(e.pc[:e.n])
var out []string
for {
f, more := frames.Next()
if f.Function != "" || f.File != "" {
out = append(out, fmt.Sprintf("%s\n\t%s:%d", f.Function, f.File, f.Line))
}
if !more {
break
}
}
return out
}
// Format implements fmt.Formatter. The %+v verb prints message + stack frames
// plus the wrapped cause (recursively if the cause also implements %+v).
// %v / %s print the flat error string.
func (e *stackErr) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
_, _ = io.WriteString(s, e.Error())
for _, frame := range e.StackTrace() {
_, _ = io.WriteString(s, "\n")
_, _ = io.WriteString(s, frame)
}
return
}
fallthrough
case 's':
_, _ = io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}