-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacktrace.go
91 lines (74 loc) · 1.51 KB
/
stacktrace.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
package e5
import (
"errors"
"fmt"
"runtime"
"strings"
"unique"
)
type _PCs [64]uintptr
// Stacktrace represents call stack frames
type Stacktrace struct {
handle unique.Handle[_PCs]
}
var _ error = new(Stacktrace)
// Error implements error interface
func (s *Stacktrace) Error() string {
pcs := s.handle.Value()
i := 0
for pcs[i] > 0 {
i++
}
buf := new(strings.Builder)
frames := runtime.CallersFrames(pcs[:i])
firstLine := true
for {
frame, more := frames.Next()
if strings.HasPrefix(frame.Function, "github.com/reusee/e5.") &&
!strings.HasPrefix(frame.Function, "github.com/reusee/e5.Test") {
// internal funcs
if !more {
break
}
continue
}
if firstLine {
fmt.Fprintf(buf, "$ %v %v:%v", frame.Function, frame.File, frame.Line)
firstLine = false
} else {
fmt.Fprintf(buf, "\n& %v %v:%v", frame.Function, frame.File, frame.Line)
}
if !more {
break
}
}
return buf.String()
}
func (s *Stacktrace) Is(err error) bool {
// ignore content
if _, ok := err.(*Stacktrace); ok {
return true
}
return false
}
// WrapStacktrace wraps current stacktrace
var WrapStacktrace = WrapFunc(func(prev error) error {
if prev == nil {
return nil
}
if stacktraceIncluded(prev) {
return prev
}
var pcs _PCs
runtime.Callers(2, pcs[:])
stacktrace := &Stacktrace{
handle: unique.Make(pcs),
}
err := Join(stacktrace, prev)
return err
})
func stacktraceIncluded(err error) bool {
var p *Stacktrace
return errors.Is(err, p)
}
var errStacktrace = errors.New("stacktrace")