This repository has been archived by the owner on Aug 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_test.go
134 lines (104 loc) · 2.8 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
package errors_test
import (
"fmt"
"github.com/jjeffery/errors"
)
func Example() {
err := errors.New("first error").With(
"card", "ace",
"suite", "spades",
)
fmt.Println(err)
err = errors.Wrap(err, "second error").With(
"piece", "rook",
"color", "black",
)
fmt.Println(err)
// Output:
// first error card=ace suite=spades
// second error piece=rook color=black: first error card=ace suite=spades
}
var userID = "u1"
var documentID = "d1"
func ExampleWith() {
// ... if a function has been called with userID and DocumentID ...
errors := errors.With("userID", userID, "documentID", documentID)
n, err := doOneThing()
if err != nil {
// will include key value pairs for userID and document ID
fmt.Println(errors.Wrap(err, "cannot do one thing"))
}
if err := doAnotherThing(n); err != nil {
// will include key value pairs for userID, document ID and n
fmt.Println(errors.Wrap(err, "cannot do another thing").With("n", n))
}
if !isValid(userID) {
// will include key value pairs for userID and document ID
fmt.Println(errors.New("invalid user"))
}
// Output:
// cannot do one thing userID=u1 documentID=d1: doOneThing: unable to finish
// cannot do another thing userID=u1 documentID=d1 n=0: doAnotherThing: not working properly
// invalid user userID=u1 documentID=d1
}
func doOneThing() (int, error) {
return 0, fmt.Errorf("doOneThing: unable to finish")
}
func doAnotherThing(n int) error {
return fmt.Errorf("doAnotherThing: not working properly")
}
var NotFound error
func getNameOfThing() string {
return "!not-valid"
}
func thingExists(name string) bool {
return false
}
func doSomethingWithThing(name string) error {
return nil
}
func isValidName(name string) bool {
return false
}
func isValid(s string) bool {
return false
}
func ExampleNew() {
name := getNameOfThing()
if !isValidName(name) {
fmt.Println(errors.New("invalid name").With("name", name))
}
// Output:
// invalid name name="!not-valid"
}
func doSomething() error {
return fmt.Errorf("not implemented")
}
func doSomethingWith(name string) error {
return fmt.Errorf("permission denied")
}
func ExampleWrap() {
if err := doSomething(); err != nil {
fmt.Println(errors.Wrap(err, "cannot do something"))
}
name := "otherthings.dat"
if err := doSomethingWith(name); err != nil {
fmt.Println(errors.Wrap(err, "cannot do something with").With("name", name))
}
// Output:
// cannot do something: not implemented
// cannot do something with name="otherthings.dat": permission denied
}
func ExampleCause() {
// tests if an error is a not found error
type notFounder interface {
NotFound() bool
}
err := getError()
if notFound, ok := errors.Cause(err).(notFounder); ok {
fmt.Printf("Not found: %v", notFound.NotFound())
}
}
func getError() error {
return fmt.Errorf("not a not found error")
}