forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
162 lines (140 loc) · 3.62 KB
/
example_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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package errors_test
import (
stderrors "errors"
"fmt"
"strings"
"github.com/gregwebs/errors"
"github.com/gregwebs/stackfmt"
)
func ExampleNew() {
err := errors.New("whoops")
fmt.Println(err)
// Output: whoops
}
// Returns the first 'n' lines of a given string, where each line is separated by '\n'.
func firstNLines(s string, n int) string {
allLines := strings.SplitN(s, "\n", n+1)
if n > len(allLines) {
n = len(allLines)
}
return strings.Join(allLines[0:n], "\n")
}
func functionLines(s string) string {
lines := []string{}
for _, line := range strings.SplitAfter(s, "\n") {
// function name
if strings.HasPrefix(line, "github.com") {
lines = append(lines, line)
// File name (different on different machine)
} else if strings.HasPrefix(line, "\t") || len(lines) == 0 {
continue
} else {
break
}
}
return strings.Join(lines, "")
}
func ExampleWrapNoStack() {
cause := stderrors.New("whoops")
err := errors.WrapNoStack(cause, "oh noes")
fmt.Printf("%+v", err)
// Output:
// whoops
// oh noes
}
func ExampleAddStack() {
// stderrors is the standard library errors: no stack trace
cause := stderrors.New("whoops")
fmt.Print(firstNLines(fmt.Sprintf("%+v\n", cause), 2))
// Add a stack trace to it
err := errors.AddStack(cause)
fmt.Print(firstNLines(fmt.Sprintf("%+v\n", err), 2))
// Output:
// whoops
// whoops
// github.com/gregwebs/errors_test.ExampleAddStack
}
func ExampleAddStackSkip() {
// stderrors is the standard library errors: no stack trace
inner := func() {
cause := stderrors.New("whoops")
err := errors.AddStack(cause)
fmt.Print(functionLines(fmt.Sprintf("%+v\n", err)))
fmt.Println("---")
// Add a stack trace to it
err = errors.AddStackSkip(cause, 1)
fmt.Print(functionLines(fmt.Sprintf("%+v\n", err)))
}
inner()
// Output:
// github.com/gregwebs/errors_test.ExampleAddStackSkip.func1
// github.com/gregwebs/errors_test.ExampleAddStackSkip
// ---
// github.com/gregwebs/errors_test.ExampleAddStackSkip
}
func ExampleWrap() {
cause := errors.New("whoops")
err := errors.Wrap(cause, "oh noes")
fmt.Println(err)
// Output: oh noes: whoops
}
func newWrappedErr() error {
e1 := errors.New("cause")
e2 := errors.Wrap(e1, "inner")
e3 := errors.Wrap(e2, "middle")
return errors.Wrap(e3, "outer")
}
func ExampleCause() {
err := newWrappedErr()
fmt.Println(err)
fmt.Println(errors.Cause(err))
// Output: outer: middle: inner: cause
// cause
}
func ExampleWrapf() {
cause := errors.New("whoops")
err := errors.Wrapf(cause, "oh noes #%d", 2)
fmt.Println(err)
// Output: oh noes #2: whoops
}
func ExampleErrorf() {
err := errors.Errorf("whoops: %s", "foo")
verbose := fmt.Sprintf("%+v", err)
fmt.Print(strings.Join(strings.SplitN(verbose, "\n", 3)[0:2], "\n"))
// Output:
// whoops: foo
// github.com/gregwebs/errors_test.ExampleErrorf
}
func Example_stackTrace() {
type stackTracer interface {
StackTrace() stackfmt.StackTrace
}
err, ok := errors.Cause(newWrappedErr()).(stackTracer)
if !ok {
panic("oops, err does not implement stackTracer")
}
st := err.StackTrace()
fmt.Print(functionLines(fmt.Sprintf("%+v", st)))
// Output:
// github.com/gregwebs/errors_test.newWrappedErr
// github.com/gregwebs/errors_test.Example_stackTrace
}
type ErrEmpty struct{}
func (et ErrEmpty) Error() string {
return "empty"
}
func ExampleAsType() {
err := errors.Wrap(ErrEmpty{}, "failed")
cause, _ := errors.AsType[ErrEmpty](err)
fmt.Printf("%v", cause)
// Output: empty
}
func ExampleIsNil() {
var empty *ErrEmpty = nil //nolint:staticcheck
var err error = empty
fmt.Println(err == nil) //nolint:staticcheck
fmt.Println(errors.IsNil(err))
// Output:
// false
// true
}