blob: fab7dc28a8b744aa6be0792f0e1efb5e7030df1d (
plain) (
blame)
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
|
package xerrors
import (
"errors"
"fmt"
"io"
"a.yandex-team.ru/library/go/x/xruntime"
)
func writeStackTrace(w io.Writer, stacktrace *xruntime.StackTrace) {
for _, frame := range stacktrace.Frames() {
if frame.Function != "" {
_, _ = fmt.Fprintf(w, " %s\n ", frame.Function)
}
if frame.File != "" {
_, _ = fmt.Fprintf(w, " %s:%d\n", frame.File, frame.Line)
}
}
}
type ErrorStackTrace interface {
StackTrace() *xruntime.StackTrace
}
// StackTraceOfEffect returns last stacktrace that was added to error chain (furthest from the root error).
// Guarantees that returned value has valid StackTrace object (but not that there are any frames).
func StackTraceOfEffect(err error) ErrorStackTrace {
var st ErrorStackTrace
for {
if !As(err, &st) {
return nil
}
if st.StackTrace() != nil {
return st
}
err = st.(error)
err = errors.Unwrap(err)
}
}
// StackTraceOfCause returns first stacktrace that was added to error chain (closest to the root error).
// Guarantees that returned value has valid StackTrace object (but not that there are any frames).
func StackTraceOfCause(err error) ErrorStackTrace {
var res ErrorStackTrace
var st ErrorStackTrace
for {
if !As(err, &st) {
return res
}
if st.StackTrace() != nil {
res = st
}
err = st.(error)
err = errors.Unwrap(err)
}
}
// NextStackTracer returns next error with stack trace.
// Guarantees that returned value has valid StackTrace object (but not that there are any frames).
func NextStackTrace(st ErrorStackTrace) ErrorStackTrace {
var res ErrorStackTrace
for {
err := st.(error)
err = errors.Unwrap(err)
if !As(err, &res) {
return nil
}
if res.StackTrace() != nil {
return res
}
}
}
|