-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_test.go
114 lines (94 loc) · 3 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
package errors_test
import (
"fmt"
"github.com/memsql/errors"
)
func ExampleExpand() {
// mustBeEven is at odds with odds.
mustBeEven := func(input int) error {
if (input % 2) == 1 {
return errors.New("not even")
}
return nil
}
// processEvenNumber uses errors.Expand() to add details when returning non-nil error.
processEvenNumber := func(input int) (err error) {
//
// Expand() adds details to non-nil err, leaves nil error as-is.
//
defer errors.Expand(&err, "failed to process number (%d)", input)
return mustBeEven(input)
}
err := processEvenNumber(1) // fails because not even
fmt.Println(err)
err2 := processEvenNumber(42) // succeeds
fmt.Println(err2)
// Output:
// failed to process number (1): not even
// <nil>
}
func ExampleExpunge() {
// mustBeEven is at odds with odds.
mustBeEven := func(input int) error {
if (input % 2) == 1 {
return errors.New("not even")
}
return nil
}
// processEvenNumber uses errors.Expand() to add details when returning non-nil error.
processEvenNumber := func(input int) (err error) {
defer errors.Expand(&err, "failed to process number (%d)", input)
return mustBeEven(input)
}
// ProcessSecretNumber uses errors.Expunge to control details presented when returning non-nil error from a
// public-facing API.
processSecretNumber := func(input int) (err error) {
//
// Expunge() redacts details from non-nil err, leaves nil error as-is.
//
defer errors.Expunge(&err, "unexpected error")
return processEvenNumber(input)
}
err := processSecretNumber(1) // fails because not even
fmt.Println(err)
err2 := processSecretNumber(42) // succeeds
fmt.Println(err2)
// Output:
// unexpected error: failed to process number
// <nil>
}
func ExampleExpungeOnce() {
// mustBeEven is at odds with odds.
mustBeEven := func(input int) error {
if (input % 2) == 1 {
return errors.New("not even")
}
return nil
}
// processEvenNumber uses errors.Expand() to add details when returning non-nil error.
processEvenNumber := func(input int) (err error) {
defer errors.Expand(&err, "failed to process number (%d)", input)
return mustBeEven(input)
}
// ProcessSecretNumber uses errors.Expunge to control details presented when returning non-nil error from a
// public-facing API.
processSecretNumber := func(input int) (err error) {
//
// ExpungeOnce() redacts details from non-nil err, leaves nil error as-is. When multiple ExpungeOnce() are
// deferred, only one will affect the error.
//
defer errors.ExpungeOnce(&err, "secret number must be even")
if err = processEvenNumber(input); err != nil {
return err
}
defer errors.ExpungeOnce(&err, "secret number must be divisible by 4")
return processEvenNumber(input / 2)
}
err := processSecretNumber(1) // fails because not even
fmt.Println(err)
err2 := processSecretNumber(42) // fails because not divisible by 4
fmt.Println(err2)
// Output:
// secret number must be even: failed to process number
// secret number must be divisible by 4: failed to process number
}