-
Notifications
You must be signed in to change notification settings - Fork 13
/
example_tree_test.go
72 lines (63 loc) · 1.37 KB
/
example_tree_test.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
package errtrace_test
import (
"errors"
"fmt"
"strings"
"braces.dev/errtrace"
"braces.dev/errtrace/internal/tracetest"
)
func normalErr(i int) error {
return fmt.Errorf("std err %v", i)
}
func wrapNormalErr(i int) error {
return errtrace.Wrap(normalErr(i))
}
func nestedErrorList(i int) error {
return errors.Join(
normalErr(i),
wrapNormalErr(i+1),
)
}
func Example_tree() {
errs := errtrace.Wrap(errors.Join(
normalErr(1),
wrapNormalErr(2),
nestedErrorList(3),
))
got := errtrace.FormatString(errs)
// make trace agnostic to environment-specific location
// and less sensitive to line number changes.
fmt.Println(trimTrailingSpaces(tracetest.MustClean(got)))
// Output:
// +- std err 1
// |
// +- std err 2
// |
// | braces.dev/errtrace_test.wrapNormalErr
// | /path/to/errtrace/example_tree_test.go:1
// |
// | +- std err 3
// | |
// | +- std err 4
// | |
// | | braces.dev/errtrace_test.wrapNormalErr
// | | /path/to/errtrace/example_tree_test.go:1
// | |
// +- std err 3
// | std err 4
// |
// std err 1
// std err 2
// std err 3
// std err 4
//
// braces.dev/errtrace_test.Example_tree
// /path/to/errtrace/example_tree_test.go:2
}
func trimTrailingSpaces(s string) string {
lines := strings.Split(s, "\n")
for i := range lines {
lines[i] = strings.TrimRight(lines[i], " \t")
}
return strings.Join(lines, "\n")
}