-
Notifications
You must be signed in to change notification settings - Fork 14
/
example_test.go
140 lines (116 loc) · 2.63 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
package errors_test
import (
"fmt"
"os"
"emperror.dev/errors"
)
// nolint: unused
func ExampleSentinel() {
const ErrSomething = errors.Sentinel("something went wrong")
// Output:
}
func ExampleAs() {
if _, err := os.Open("non-existing"); err != nil {
var pathError *os.PathError
if errors.As(err, &pathError) {
fmt.Println("Failed at path:", pathError.Path)
} else {
fmt.Println(err)
}
}
// Output:
// Failed at path: non-existing
}
func ExampleIs() {
ErrSomething := errors.NewPlain("something")
if err := errors.Wrap(ErrSomething, "error"); err != nil {
if errors.Is(err, ErrSomething) {
fmt.Println("something went wrong")
} else {
fmt.Println(err)
}
}
// Output:
// something went wrong
}
func ExampleUnwrap() {
ErrSomething := errors.NewPlain("something")
if err := errors.WithMessage(ErrSomething, "error"); err != nil {
if errors.Unwrap(err) == ErrSomething {
fmt.Println("something went wrong")
} else {
fmt.Println(err)
}
}
// Output:
// something went wrong
}
func ExampleCause() {
ErrSomething := errors.NewPlain("something")
if err := errors.Wrap(ErrSomething, "error"); err != nil {
if errors.Cause(err) == ErrSomething {
fmt.Println("something went wrong")
} else {
fmt.Println(err)
}
}
// Output:
// something went wrong
}
func ExampleCombine() {
err := errors.Combine(
errors.NewPlain("call 1 failed"),
nil, // successful request
errors.NewPlain("call 3 failed"),
nil, // successful request
errors.NewPlain("call 5 failed"),
)
fmt.Printf("%+v", err)
// Output:
// the following errors occurred:
// - call 1 failed
// - call 3 failed
// - call 5 failed
}
func ExampleCombine_loop() {
var errs []error
for i := 1; i < 6; i++ {
if i%2 == 0 {
continue
}
err := errors.NewPlain(fmt.Sprintf("call %d failed", i))
errs = append(errs, err)
}
err := errors.Combine(errs...)
fmt.Printf("%+v", err)
// Output:
// the following errors occurred:
// - call 1 failed
// - call 3 failed
// - call 5 failed
}
func ExampleAppend() {
var err error
err = errors.Append(err, errors.NewPlain("call 1 failed"))
err = errors.Append(err, errors.NewPlain("call 2 failed"))
fmt.Println(err)
// Output:
// call 1 failed; call 2 failed
}
func ExampleGetErrors() {
err := errors.Combine(
nil, // successful request
errors.NewPlain("call 2 failed"),
errors.NewPlain("call 3 failed"),
)
err = errors.Append(err, nil) // successful request
err = errors.Append(err, errors.NewPlain("call 5 failed"))
errs := errors.GetErrors(err)
for _, err := range errs {
fmt.Println(err)
}
// Output:
// call 2 failed
// call 3 failed
// call 5 failed
}