forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
87 lines (66 loc) · 1.68 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
package errors_test
import (
"fmt"
"os"
"github.com/pkg/errors"
)
func ExampleNew() {
err := errors.New("whoops")
fmt.Println(err)
// Output: whoops
}
func ExampleNew_fprint() {
err := errors.New("whoops")
errors.Fprint(os.Stdout, err)
// Output: github.com/pkg/errors/example_test.go:18: whoops
}
func ExampleWrap() {
cause := errors.New("whoops")
err := errors.Wrap(cause, "oh noes")
fmt.Println(err)
// Output: oh noes: whoops
}
func fn() error {
e1 := errors.New("error")
e2 := errors.Wrap(e1, "inner")
e3 := errors.Wrap(e2, "middle")
return errors.Wrap(e3, "outer")
}
func ExampleCause() {
err := fn()
fmt.Println(err)
fmt.Println(errors.Cause(err))
// Output: outer: middle: inner: error
// error
}
func ExampleFprint() {
err := fn()
errors.Fprint(os.Stdout, err)
// Output: github.com/pkg/errors/example_test.go:36: outer
// github.com/pkg/errors/example_test.go:35: middle
// github.com/pkg/errors/example_test.go:34: inner
// github.com/pkg/errors/example_test.go:33: error
}
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")
errors.Fprint(os.Stdout, err)
// Output: github.com/pkg/errors/example_test.go:67: whoops: foo
}
func ExampleError_Stacktrace() {
type Stacktrace interface {
Stacktrace() []errors.Frame
}
err, ok := errors.Cause(fn()).(Stacktrace)
if !ok {
panic("oops, err does not implement Stacktrace")
}
st := err.Stacktrace()
fmt.Printf("%+v", st[0:2]) // top two framces
// Output: [github.com/pkg/errors/example_test.go:33 github.com/pkg/errors/example_test.go:78]
}