-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexamples_test.go
208 lines (178 loc) · 7.69 KB
/
examples_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package errors_test
import (
"encoding/json"
"fmt"
grpcCodes "google.golang.org/grpc/codes"
grpcErrors "google.golang.org/grpc/status"
"github.com/bdlm/errors/v2"
)
func ExampleNew() {
err := errors.New("this is an error message")
fmt.Println(err)
// Output: this is an error message
}
func ExampleE_Format_string() {
err := loadConfig()
fmt.Println(err)
// Output: service configuration could not be loaded
}
func ExampleE_Format_stringPreformat() {
err := loadConfig()
fmt.Printf("% v", err)
// Output: service configuration could not be loaded
}
func ExampleE_Format_stringDetail() {
err := loadConfig()
fmt.Printf("%-v", err)
// Output: service configuration could not be loaded - #0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig);
}
func ExampleE_Format_stringTrace() {
err := loadConfig()
fmt.Printf("%+v", err)
// Output: service configuration could not be loaded - #0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig); could not decode configuration data - #1 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig); could not read configuration file - #2 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig); read: end of input - #3 n/a
}
func ExampleE_Format_stringDetailPreformat() {
err := loadConfig()
fmt.Printf("% -v", err)
// Output: service configuration could not be loaded - #0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig);
}
func ExampleE_Format_stringTracePreformat() {
err := loadConfig()
fmt.Printf("% +v", err)
// Output: service configuration could not be loaded - #0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig);
// could not decode configuration data - #1 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig);
// could not read configuration file - #2 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig);
// read: end of input - #3 n/a
}
func ExampleE_Format_json() {
err := loadConfig()
fmt.Printf("%#v", err)
// Output: [{"error":"service configuration could not be loaded"}]
}
func ExampleE_Format_jsonPreformat() {
err := loadConfig()
fmt.Printf("% #v", err)
// Output: [
// {
// "error": "service configuration could not be loaded"
// }
// ]
}
func ExampleE_Format_jsonDetail() {
err := loadConfig()
fmt.Printf("%#-v", err)
// Output: [{"caller":"#0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig)","error":"service configuration could not be loaded"}]
}
func ExampleE_Format_jsonDetailPreformat() {
err := loadConfig()
fmt.Printf("% #-v", err)
// Output: [
// {
// "caller": "#0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig)",
// "error": "service configuration could not be loaded"
// }
// ]
}
func ExampleE_Format_jsonTrace() {
err := loadConfig()
fmt.Printf("%#+v", err)
// Output: [{"caller":"#0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig)","error":"service configuration could not be loaded"},{"caller":"#1 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig)","error":"could not decode configuration data"},{"caller":"#2 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig)","error":"could not read configuration file"},{"caller":"#3 n/a","error":"read: end of input"}]
}
func ExampleE_Format_jsonTracePreformat() {
err := loadConfig()
fmt.Printf("% #+v", err)
// Output: [
// {
// "caller": "#0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig)",
// "error": "service configuration could not be loaded"
// },
// {
// "caller": "#1 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig)",
// "error": "could not decode configuration data"
// },
// {
// "caller": "#2 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig)",
// "error": "could not read configuration file"
// },
// {
// "caller": "#3 n/a",
// "error": "read: end of input"
// }
// ]
}
func ExampleE_MarshalJSON_marshal() {
err := loadConfig()
jsn, _ := json.Marshal(err)
fmt.Println(string(jsn))
// Output: [{"caller":"#0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig)","error":"service configuration could not be loaded"},{"caller":"#1 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig)","error":"could not decode configuration data"},{"caller":"#2 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig)","error":"could not read configuration file"},{"caller":"#3 n/a","error":"read: end of input"}]
}
func ExampleE_MarshalJSON_marshalIndent() {
err := loadConfig()
jsn, _ := json.MarshalIndent(err, "", " ")
fmt.Println(string(jsn))
// Output: [
// {
// "caller": "#0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig)",
// "error": "service configuration could not be loaded"
// },
// {
// "caller": "#1 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig)",
// "error": "could not decode configuration data"
// },
// {
// "caller": "#2 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig)",
// "error": "could not read configuration file"
// },
// {
// "caller": "#3 n/a",
// "error": "read: end of input"
// }
// ]
}
func ExampleUnwrap() {
err1 := errors.New("error 1")
err2 := errors.Wrap(err1, "error 2")
err := errors.Unwrap(err2)
fmt.Println(err)
// Output: error 1
}
func ExampleUnwrap_iterateStack() {
err := loadConfig()
// Iterate through an error stack, last in - first out.
for err != nil {
fmt.Printf("%+v\n", err)
err = errors.Unwrap(err)
}
// Output: service configuration could not be loaded - #0 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig); could not decode configuration data - #1 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig); could not read configuration file - #2 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig); read: end of input - #3 n/a
// could not decode configuration data - #0 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig); could not read configuration file - #1 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig); read: end of input - #2 n/a
// could not read configuration file - #0 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig); read: end of input - #1 n/a
// read: end of input - #0 n/a
}
func ExampleWrap() {
// Wrap an error with additional metadata.
err := loadConfig()
err = errors.Wrap(err, "loadConfig returned an error")
fmt.Printf("% +v", err)
// Output: loadConfig returned an error - #0 examples_test.go:180 (github.com/bdlm/errors/v2_test.ExampleWrap);
// service configuration could not be loaded - #1 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig);
// could not decode configuration data - #2 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig);
// could not read configuration file - #3 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig);
// read: end of input - #4 n/a
}
func ExampleWrapE() {
var internalServerError = grpcErrors.Error(
grpcCodes.Internal,
"internal server error",
)
// Wrap an error with another error to maintain context.
err := loadConfig()
if nil != err {
err = errors.WrapE(err, internalServerError)
}
fmt.Printf("% +v", err)
// Output: rpc error: code = Internal desc = internal server error - #0 examples_test.go:199 (github.com/bdlm/errors/v2_test.ExampleWrapE);
// service configuration could not be loaded - #1 mocks_test.go:16 (github.com/bdlm/errors/v2_test.loadConfig);
// could not decode configuration data - #2 mocks_test.go:21 (github.com/bdlm/errors/v2_test.decodeConfig);
// could not read configuration file - #3 mocks_test.go:26 (github.com/bdlm/errors/v2_test.readConfig);
// read: end of input - #4 n/a
}