forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
56 lines (43 loc) · 1 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
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
}