-
Notifications
You must be signed in to change notification settings - Fork 0
/
pperr.go
99 lines (75 loc) · 1.71 KB
/
pperr.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
package pperr
import (
"fmt"
"io"
"os"
"strings"
"github.com/pkg/errors"
)
func Print(err error) {
Fprint(os.Stdout, err)
}
func PrintFunc(err error, puts Printer) {
FprintFunc(os.Stdout, err, puts)
}
func Sprint(err error) string {
var buf strings.Builder
Fprint(&buf, err)
return buf.String()
}
func SprintFunc(err error, puts Printer) string {
var buf strings.Builder
FprintFunc(&buf, err, puts)
return buf.String()
}
func Fprint(w io.Writer, err error) {
FprintFunc(w, err, DefaultPrinter)
}
func FprintFunc(w io.Writer, err error, puts Printer) {
for _, e := range extractErrorSets(err, nil) {
puts(w, e.Error, e.Frames, e.Parent)
}
}
func ExtractErrorSets(err error) ErrorSets {
return extractErrorSets(err, nil)
}
func extractErrorSets(err error, parent Frames) []ErrorSet {
if err == nil {
return nil
}
realErr := err
var frames Frames
if withStack, ok := err.(interface{ StackTrace() errors.StackTrace }); ok {
frames = ExtractFrames(withStack.StackTrace())
if withCause, ok := withStack.(interface{ Unwrap() error }); ok {
realErr = withCause.Unwrap()
}
}
var errs []ErrorSet
if withCause, ok := realErr.(interface{ Unwrap() error }); ok {
var causeParent Frames
if frames != nil {
causeParent = frames
} else {
causeParent = parent
}
if es := extractErrorSets(withCause.Unwrap(), causeParent); es != nil {
errs = es
}
}
errs = append(errs, ErrorSet{Error: err, Frames: frames, Parent: parent})
return errs
}
func CauseType(err error) string {
for {
wrappedErr, ok := err.(interface{ Unwrap() error })
if !ok {
return fmt.Sprintf("%T", err)
}
cause := wrappedErr.Unwrap()
if cause == nil {
return fmt.Sprintf("%T", err)
}
err = cause
}
}