| 1 |
# error-graph |
| 2 |
|
| 3 |
Allows non-fatal errors in a tree of subfunctions to easily be collected by a caller |
| 4 |
|
| 5 |
Provides the `error_graph::ErrorList<E>` type to hold a list of non-fatal errors |
| 6 |
that occurred while a function was running. |
| 7 |
|
| 8 |
It has a `subwriter()` method that can be passed as a parameter to |
| 9 |
a subfunction and allows that subfunction to record all the non-fatal errors it encounters. |
| 10 |
When the subfunction is done running, its error list will be mapped to the caller's error type |
| 11 |
and added to the caller's `ErrorList` automatically. |
| 12 |
|
| 13 |
Since subfunctions may in-turn also use the `subwriter()` |
| 14 |
function on the writter given to them by their caller, this creates a tree of non-fatal errors |
| 15 |
that occurred during the execution of an entire call graph. |
| 16 |
|
| 17 |
# Usage |
| 18 |
|
| 19 |
``` |
| 20 |
# use error_graph::{ErrorList, WriteErrorList, strategy::{DontCare, ErrorOccurred}}; |
| 21 |
enum UpperError { |
| 22 |
Upper, |
| 23 |
Middle(ErrorList<MiddleError>), |
| 24 |
} |
| 25 |
enum MiddleError { |
| 26 |
Middle, |
| 27 |
Lower(ErrorList<LowerError>), |
| 28 |
} |
| 29 |
enum LowerError { |
| 30 |
Lower, |
| 31 |
} |
| 32 |
fn upper() { |
| 33 |
let mut errors = ErrorList::default(); |
| 34 |
errors.push(UpperError::Upper); |
| 35 |
// Map the ErrorList<MiddleError> to our UpperError::Middle variant |
| 36 |
middle(errors.subwriter(UpperError::Middle)); |
| 37 |
errors.push(UpperError::Upper); |
| 38 |
|
| 39 |
// Some callers just don't want to know if things went wrong or not |
| 40 |
middle(DontCare); |
| 41 |
|
| 42 |
// Some callers are only interested in whether an error occurred or not |
| 43 |
let mut error_occurred = ErrorOccurred::default(); |
| 44 |
middle(&mut error_occurred); |
| 45 |
if error_occurred.as_bool() { |
| 46 |
errors.push(UpperError::Upper); |
| 47 |
} |
| 48 |
} |
| 49 |
fn middle(mut errors: impl WriteErrorList<MiddleError>) { |
| 50 |
// We can pass a sublist by mutable reference if we need to manipulate it before and after |
| 51 |
let mut sublist = errors.sublist(MiddleError::Lower); |
| 52 |
lower(&mut sublist); |
| 53 |
let num_errors = sublist.len(); |
| 54 |
sublist.finish(); |
| 55 |
if num_errors > 10 { |
| 56 |
errors.push(MiddleError::Middle); |
| 57 |
} |
| 58 |
// We can pass a reference directly to our error list for peer functions |
| 59 |
middle_2(&mut errors); |
| 60 |
} |
| 61 |
fn middle_2(mut errors: impl WriteErrorList<MiddleError>) { |
| 62 |
errors.push(MiddleError::Middle); |
| 63 |
} |
| 64 |
fn lower(mut errors: impl WriteErrorList<LowerError>) { |
| 65 |
errors.push(LowerError::Lower); |
| 66 |
} |
| 67 |
``` |
| 68 |
|
| 69 |
# Motivation |
| 70 |
|
| 71 |
In most call graphs, a function that encounters an error will early-return and pass an |
| 72 |
error type to its caller. The caller will often respond by passing that error further up the |
| 73 |
call stack up to its own caller (possibly after wrapping it in its own error type). That |
| 74 |
continues so-on-and-so-forth until some caller finally handles the error, returns from `main`, |
| 75 |
or panics. Ultimately, the result is that some interested caller will receive a linear chain of |
| 76 |
errors that led to the failure. |
| 77 |
|
| 78 |
But, not all errors are fatal -- Sometimes, a function might be able to continue working after |
| 79 |
it encounters an error and still be able to at-least-partially achieve its goals. Calling it |
| 80 |
again - or calling other functions in the same API - is still permissible and may also result |
| 81 |
in full or partial functionality. |
| 82 |
|
| 83 |
In that case, the function may still choose to return `Result::Ok`; however, that leaves the |
| 84 |
function with a dilemma -- How can it report the non-fatal errors to the caller? |
| 85 |
|
| 86 |
1. **Return a tuple in its `Result::Ok` type**: that wouldn't capture the non-fatal errors in |
| 87 |
the case that a fatal error occurs, so it would also have to be added to the `Result::Err` |
| 88 |
type as well. |
| 89 |
|
| 90 |
That adds a bunch of boilerplate, as the function needs to allocate the list and map it |
| 91 |
into the return type for every error return and good return. It also makes the function |
| 92 |
signature much more noisy. |
| 93 |
|
| 94 |
2. **Take a list as a mutable reference?**: Better, but now the caller has to allocate the |
| 95 |
list, and there's no way for it to opt out if it doesn't care about the non-fatal errors. |
| 96 |
|
| 97 |
3. **Maybe add an `Option` to it?** Okay, so a parameter like `errors: Option<&mut Vec<E>>`? |
| 98 |
Getting warmer, but now the child has to do a bunch of |
| 99 |
`if let Some(v) = errors { v.push(error); }` all over the place. |
| 100 |
|
| 101 |
And what about the caller side of it? For a simple caller, the last point isn't too bad: The |
| 102 |
caller just has to allocate the list, pass `Some(&mut errors)` to the child, and check it upon |
| 103 |
return. |
| 104 |
|
| 105 |
But often, the caller itself is keeping its own list of non-fatal errors and may also be a |
| 106 |
subfunction to some other caller, and so-on-and-so-forth. In this case, we no longer have |
| 107 |
a simple chain of errors, but instead we have a tree of errors -- Each level in the tree |
| 108 |
contains all the non-fatal errors that occurred during execution of a function and all |
| 109 |
subfunctions in its call graph. |
| 110 |
|
| 111 |
# Solution |
| 112 |
|
| 113 |
The main behavior we want is captured by the `WriteErrorList` trait in this crate. It can be |
| 114 |
passed as a parameter to any function that wants to be able to report non-fatal errors to its |
| 115 |
caller, and it gives the caller flexibility to decide what it wants to do with that |
| 116 |
information. |
| 117 |
|
| 118 |
The main concrete type in this crate is `ErrorList`, which stores a list of a single type of |
| 119 |
error. Any time a list of errors needs to be stored in memory, this is the type to use. It will |
| 120 |
usually be created by the top-level caller using `ErrorList::default`, and any subfunction will |
| 121 |
give an `ErrorList` of its own error type to the `map_fn` that was passed in by its caller upon |
| 122 |
return. |
| 123 |
|
| 124 |
However, `ErrorList` should rarely be passed as a parameter to a function, as that wouldn't |
| 125 |
provide the caller with the flexiblity to decide what strategy it actually wants |
| 126 |
to use when collecting its subfunction's non-fatal errors. The caller may want to pass direct |
| 127 |
reference to its own error list, it may want to pass a `Sublist` type that automatically |
| 128 |
pushes the subfunction's error list to its own error list after mapping, or it may want to |
| 129 |
pass the `DontCare` type if it doesn't want to know anything about the |
| 130 |
subfunction's non-fatal errors. |
| 131 |
|
| 132 |
Instead, subfunctions should take `impl WriteErrorList<E>` as a parameter. |
| 133 |
This allows any of those types above, as well as mutable references to those types, to be |
| 134 |
passed in by the caller. This also allows future caller strategies to be implemented, like |
| 135 |
a caller that only cares how many non-fatal errors occurred but doesn't care about the details. |
| 136 |
|
| 137 |
# Serde |
| 138 |
|
| 139 |
(This section only applies if the `serde` feature is enabled) |
| 140 |
|
| 141 |
`ErrorList` implements the `Serialize` trait if the errors it contains do, and |
| 142 |
likewise with the `Deserialize` trait. This means that if every error type in the tree |
| 143 |
implements these traits then the entire tree can be sent over the wire and recreated elsewhere. |
| 144 |
Very useful if the errors are to be examined remotely! |
| 145 |
|