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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use BuilderError;
use Error as JsonError;
use ;
use Error as ReqwestError;
/// Distinguishes between different types of errors carried by the [Error] struct.
/// For a non-exhaustive example, see [BuilderError].
///
/// # Example
/// ```
/// // This example requires the sync feature.
///
/// use lemon_bugsnag_rs::common::builder::client_builder::ClientBuilder;
/// use lemon_bugsnag_rs::common::error::ErrorType;
/// use lemon_bugsnag_rs::common::builder::error::BuilderError;
/// use lemon_bugsnag_rs::common::traits::decider_trait_sync::DeciderTraitSync;
/// use lemon_bugsnag_rs::common::traits::backend_builder_trait_sync::BackendBuilderTraitSync;
///
/// match ClientBuilder::error().reqwest().blocking().build() {
/// Ok(_) => {
/// // Do stuff
/// },
/// Err(error) => {
/// match error.error_type() {
/// // Handle errors as appropriate
/// ErrorType::ClientBuildError(builder_error) => {
/// // This error contains another enum to further
/// // distinguish the exact cause of the problem.
/// match builder_error {
/// // Both of these error types provide { context, fields }
/// // which give more information about the error. These
/// // fields are ommitted for brevity in this example.
/// BuilderError::MissingField { .. } => (),
/// BuilderError::ConflictingField { .. } => ()
/// }
/// },
/// ErrorType::ClientBuildErrorForeign => (),
/// ErrorType::ClientRequestError => (),
/// ErrorType::SerializeError => (),
/// ErrorType::Unknown => (),
/// }
/// }
/// }
/// ```
/// Error struct used to consolidate and standardize errors from multiple
/// sources.
///
/// # Example
/// ```
/// use lemon_bugsnag_rs::common::builder::client_builder::ClientBuilder;
/// use lemon_bugsnag_rs::common::traits::backend_builder_trait_sync::BackendBuilderTraitSync;
/// use lemon_bugsnag_rs::common::error::ErrorType;
/// use std::error::Error;
///
/// let client_builder = ClientBuilder::error();
///
/// match client_builder.ureq().build() {
/// Ok(_client) => {
/// // This is where you would handle a successful build
/// },
/// Err(error) => {
/// // Log this to your local logging system
/// // my_logger.log_error(builder_error.error);
///
/// // Process nested error, if any.
/// if error.source().is_some() {
/// let source_error = error.source().as_ref();
/// // Do stuff with the source error.
/// }
///
/// // Handle the error.
/// match error.error_type() {
/// ErrorType::ClientBuildError(builder_error) => {
/// // This is where the builder error is handled.
/// },
/// _ => {
/// // There are other types of error that you might want to
/// // handle in production code. However, those additional
/// // errors are irrelevant to this example.
/// },
/// }
/// },
/// };
/// ```
/// Can be used to hold multiple [Error]s in cases where a function may generate
/// more than one user-addressable error in a single call. For example,
/// [ClientBuilderReqwestSessionSync's](
/// crate::session::reqwest::builder::blocking::ClientBuilderReqwestSessionSync
/// ) implementation of [BackendBuilderTrait::build](
/// crate::common::traits::backend_builder_trait_sync::BackendBuilderTraitSync::build
/// ) can return both [MissingField](
/// crate::common::builder::error::BuilderError::MissingField
/// ) and [ConflictingField](
/// crate::common::builder::error::BuilderError::ConflictingField
/// ) variants of [BuilderError] at the same
/// time.
///
/// # Example
/// ```
/// // This example requires the optional-builders feature.
/// // This example requires the convenience-intos feature.
/// // This example requires the session-client feature.
///
/// use chrono::DateTime;
///
/// use lemon_bugsnag_rs::{
/// common::traits::{backend_builder_trait_sync::BackendBuilderTraitSync, client_trait_sync::ClientTraitSync},
/// session::{
/// payload::{
/// notifier::Notifier, session_counts::session_count::SessionCount, sessions::Session,
/// },
/// session_client_builder::SessionClientBuilder,
/// },
/// };
///
/// use lemon_bugsnag_rs::common::builder::client_builder::ClientBuilder;
///
/// let timestamp = chrono::Utc::now();
///
/// let mut client_builder = ClientBuilder::session();
///
/// let session = Session {
/// id: "0123".to_string()
/// , started_at: timestamp
/// , user: None
/// };
///
/// let mut session_count_builder = SessionCount::builder();
/// let session_count = session_count_builder
/// .set_started_at(timestamp)
/// .set_sessions_started(4)
/// .build().unwrap();
///
/// // We configure both session and sesion_counts, which will result in a
/// // ConflictingField error when we call build() on client_builder.
/// // We also failed to configure the notifier, app, and device fields, which
/// // will result in a MissingField error when we try to build() this struct.
/// client_builder
/// .set_sessions(session)
/// .add_session_count(session_count);
///
/// let result = client_builder.ureq().build();
///
/// match result {
/// Ok(mut client) => {
/// // In a production environment, you would want to gracefully
/// // handle potential Error values when unwrapping.
/// client.send().unwrap();
/// },
/// Err(error_stack) => {
/// error_stack.errors.into_iter().for_each(|error| {
/// match error.error_type() {
/// lemon_bugsnag_rs::common::error::ErrorType::ClientBuildError(builder_error) => {
/// match builder_error {
/// lemon_bugsnag_rs::common::builder::error::BuilderError::MissingField {context, fields } => {
/// // In this example, we encounter this error
/// // because we did not configure all required
/// // fields on the SessionBuilder.
///
/// // Handle MissingField error, here.
/// },
/// lemon_bugsnag_rs::common::builder::error::BuilderError::ConflictingField { context, fields } => {
/// // In this example, we encounter this error
/// // because we configured both the session
/// // and session_counts keys, which are
/// // mutually exclusive.
///
/// // Handle ConflictingField error, here.
/// },
/// }
/// },
///
/// // These are other possible errors that are not relevant
/// // to the error stack example.
/// lemon_bugsnag_rs::common::error::ErrorType::ClientBuildErrorForeign => {},
/// lemon_bugsnag_rs::common::error::ErrorType::ClientRequestError => {},
/// lemon_bugsnag_rs::common::error::ErrorType::SerializeError => {},
/// lemon_bugsnag_rs::common::error::ErrorType::Unknown => {},
/// // Reserved for future error types.
/// _ => {}
/// }
/// });
/// },
/// }
/// ```