OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / pkg / errors / example_test.go
1 package errors_test
2
3 import (
4         "fmt"
5
6         "github.com/pkg/errors"
7 )
8
9 func ExampleNew() {
10         err := errors.New("whoops")
11         fmt.Println(err)
12
13         // Output: whoops
14 }
15
16 func ExampleNew_printf() {
17         err := errors.New("whoops")
18         fmt.Printf("%+v", err)
19
20         // Example output:
21         // whoops
22         // github.com/pkg/errors_test.ExampleNew_printf
23         //         /home/dfc/src/github.com/pkg/errors/example_test.go:17
24         // testing.runExample
25         //         /home/dfc/go/src/testing/example.go:114
26         // testing.RunExamples
27         //         /home/dfc/go/src/testing/example.go:38
28         // testing.(*M).Run
29         //         /home/dfc/go/src/testing/testing.go:744
30         // main.main
31         //         /github.com/pkg/errors/_test/_testmain.go:106
32         // runtime.main
33         //         /home/dfc/go/src/runtime/proc.go:183
34         // runtime.goexit
35         //         /home/dfc/go/src/runtime/asm_amd64.s:2059
36 }
37
38 func ExampleWithMessage() {
39         cause := errors.New("whoops")
40         err := errors.WithMessage(cause, "oh noes")
41         fmt.Println(err)
42
43         // Output: oh noes: whoops
44 }
45
46 func ExampleWithStack() {
47         cause := errors.New("whoops")
48         err := errors.WithStack(cause)
49         fmt.Println(err)
50
51         // Output: whoops
52 }
53
54 func ExampleWithStack_printf() {
55         cause := errors.New("whoops")
56         err := errors.WithStack(cause)
57         fmt.Printf("%+v", err)
58
59         // Example Output:
60         // whoops
61         // github.com/pkg/errors_test.ExampleWithStack_printf
62         //         /home/fabstu/go/src/github.com/pkg/errors/example_test.go:55
63         // testing.runExample
64         //         /usr/lib/go/src/testing/example.go:114
65         // testing.RunExamples
66         //         /usr/lib/go/src/testing/example.go:38
67         // testing.(*M).Run
68         //         /usr/lib/go/src/testing/testing.go:744
69         // main.main
70         //         github.com/pkg/errors/_test/_testmain.go:106
71         // runtime.main
72         //         /usr/lib/go/src/runtime/proc.go:183
73         // runtime.goexit
74         //         /usr/lib/go/src/runtime/asm_amd64.s:2086
75         // github.com/pkg/errors_test.ExampleWithStack_printf
76         //         /home/fabstu/go/src/github.com/pkg/errors/example_test.go:56
77         // testing.runExample
78         //         /usr/lib/go/src/testing/example.go:114
79         // testing.RunExamples
80         //         /usr/lib/go/src/testing/example.go:38
81         // testing.(*M).Run
82         //         /usr/lib/go/src/testing/testing.go:744
83         // main.main
84         //         github.com/pkg/errors/_test/_testmain.go:106
85         // runtime.main
86         //         /usr/lib/go/src/runtime/proc.go:183
87         // runtime.goexit
88         //         /usr/lib/go/src/runtime/asm_amd64.s:2086
89 }
90
91 func ExampleWrap() {
92         cause := errors.New("whoops")
93         err := errors.Wrap(cause, "oh noes")
94         fmt.Println(err)
95
96         // Output: oh noes: whoops
97 }
98
99 func fn() error {
100         e1 := errors.New("error")
101         e2 := errors.Wrap(e1, "inner")
102         e3 := errors.Wrap(e2, "middle")
103         return errors.Wrap(e3, "outer")
104 }
105
106 func ExampleCause() {
107         err := fn()
108         fmt.Println(err)
109         fmt.Println(errors.Cause(err))
110
111         // Output: outer: middle: inner: error
112         // error
113 }
114
115 func ExampleWrap_extended() {
116         err := fn()
117         fmt.Printf("%+v\n", err)
118
119         // Example output:
120         // error
121         // github.com/pkg/errors_test.fn
122         //         /home/dfc/src/github.com/pkg/errors/example_test.go:47
123         // github.com/pkg/errors_test.ExampleCause_printf
124         //         /home/dfc/src/github.com/pkg/errors/example_test.go:63
125         // testing.runExample
126         //         /home/dfc/go/src/testing/example.go:114
127         // testing.RunExamples
128         //         /home/dfc/go/src/testing/example.go:38
129         // testing.(*M).Run
130         //         /home/dfc/go/src/testing/testing.go:744
131         // main.main
132         //         /github.com/pkg/errors/_test/_testmain.go:104
133         // runtime.main
134         //         /home/dfc/go/src/runtime/proc.go:183
135         // runtime.goexit
136         //         /home/dfc/go/src/runtime/asm_amd64.s:2059
137         // github.com/pkg/errors_test.fn
138         //        /home/dfc/src/github.com/pkg/errors/example_test.go:48: inner
139         // github.com/pkg/errors_test.fn
140         //        /home/dfc/src/github.com/pkg/errors/example_test.go:49: middle
141         // github.com/pkg/errors_test.fn
142         //      /home/dfc/src/github.com/pkg/errors/example_test.go:50: outer
143 }
144
145 func ExampleWrapf() {
146         cause := errors.New("whoops")
147         err := errors.Wrapf(cause, "oh noes #%d", 2)
148         fmt.Println(err)
149
150         // Output: oh noes #2: whoops
151 }
152
153 func ExampleErrorf_extended() {
154         err := errors.Errorf("whoops: %s", "foo")
155         fmt.Printf("%+v", err)
156
157         // Example output:
158         // whoops: foo
159         // github.com/pkg/errors_test.ExampleErrorf
160         //         /home/dfc/src/github.com/pkg/errors/example_test.go:101
161         // testing.runExample
162         //         /home/dfc/go/src/testing/example.go:114
163         // testing.RunExamples
164         //         /home/dfc/go/src/testing/example.go:38
165         // testing.(*M).Run
166         //         /home/dfc/go/src/testing/testing.go:744
167         // main.main
168         //         /github.com/pkg/errors/_test/_testmain.go:102
169         // runtime.main
170         //         /home/dfc/go/src/runtime/proc.go:183
171         // runtime.goexit
172         //         /home/dfc/go/src/runtime/asm_amd64.s:2059
173 }
174
175 func Example_stackTrace() {
176         type stackTracer interface {
177                 StackTrace() errors.StackTrace
178         }
179
180         err, ok := errors.Cause(fn()).(stackTracer)
181         if !ok {
182                 panic("oops, err does not implement stackTracer")
183         }
184
185         st := err.StackTrace()
186         fmt.Printf("%+v", st[0:2]) // top two frames
187
188         // Example output:
189         // github.com/pkg/errors_test.fn
190         //      /home/dfc/src/github.com/pkg/errors/example_test.go:47
191         // github.com/pkg/errors_test.Example_stackTrace
192         //      /home/dfc/src/github.com/pkg/errors/example_test.go:127
193 }
194
195 func ExampleCause_printf() {
196         err := errors.Wrap(func() error {
197                 return func() error {
198                         return errors.Errorf("hello %s", fmt.Sprintf("world"))
199                 }()
200         }(), "failed")
201
202         fmt.Printf("%v", err)
203
204         // Output: failed: hello world
205 }