error_graph/
lib.rs

1//! Allows non-fatal errors in a tree of subfunctions to easily be collected by a caller
2//!
3//! Provides the [`error_graph::ErrorList<E>`][ErrorList] type to hold a list of non-fatal errors
4//! that occurred while a function was running.
5//!
6//! It has a [`subwriter()`][WriteErrorList::subwriter] method that can be passed as a parameter to
7//! a subfunction and allows that subfunction to record all the non-fatal errors it encounters.
8//! When the subfunction is done running, its error list will be mapped to the caller's error type
9//! and added to the caller's [ErrorList] automatically.
10//!
11//! Since subfunctions may in-turn also use the [`subwriter()`][WriteErrorList::subwriter]
12//! function on the writter given to them by their caller, this creates a tree of non-fatal errors
13//! that occurred during the execution of an entire call graph.
14//!
15//! # Usage
16//!
17//! ```
18//! # use error_graph::{ErrorList, WriteErrorList, strategy::{DontCare, ErrorOccurred}};
19//! enum UpperError {
20//!     Upper,
21//!     Middle(ErrorList<MiddleError>),
22//! }
23//! enum MiddleError {
24//!     Middle,
25//!     Lower(ErrorList<LowerError>),
26//! }
27//! enum LowerError {
28//!     Lower,
29//! }
30//! fn upper() {
31//!     let mut errors = ErrorList::default();
32//!     errors.push(UpperError::Upper);
33//!     // Map the ErrorList<MiddleError> to our UpperError::Middle variant
34//!     middle(errors.subwriter(UpperError::Middle));
35//!     errors.push(UpperError::Upper);
36//!
37//!     // Some callers just don't want to know if things went wrong or not
38//!     middle(DontCare);
39//!
40//!     // Some callers are only interested in whether an error occurred or not
41//!     let mut error_occurred = ErrorOccurred::default();
42//!     middle(&mut error_occurred);
43//!     if error_occurred.as_bool() {
44//!         errors.push(UpperError::Upper);
45//!     }
46//! }
47//! fn middle(mut errors: impl WriteErrorList<MiddleError>) {
48//!     // We can pass a sublist by mutable reference if we need to manipulate it before and after
49//!     let mut sublist = errors.sublist(MiddleError::Lower);
50//!     lower(&mut sublist);
51//!     let num_errors = sublist.len();
52//!     sublist.finish();
53//!     if num_errors > 10 {
54//!         errors.push(MiddleError::Middle);
55//!     }
56//!     // We can pass a reference directly to our error list for peer functions
57//!     middle_2(&mut errors);
58//! }
59//! fn middle_2(mut errors: impl WriteErrorList<MiddleError>) {
60//!     errors.push(MiddleError::Middle);
61//! }
62//! fn lower(mut errors: impl WriteErrorList<LowerError>) {
63//!     errors.push(LowerError::Lower);
64//! }
65//! ```
66//!
67//! # Motivation
68//!
69//! In most call graphs, a function that encounters an error will early-return and pass an
70//! error type to its caller. The caller will often respond by passing that error further up the
71//! call stack up to its own caller (possibly after wrapping it in its own error type). That
72//! continues so-on-and-so-forth until some caller finally handles the error, returns from `main`,
73//! or panics. Ultimately, the result is that some interested caller will receive a linear chain of
74//! errors that led to the failure.
75//!
76//! But, not all errors are fatal -- Sometimes, a function might be able to continue working after
77//! it encounters an error and still be able to at-least-partially achieve its goals. Calling it
78//! again - or calling other functions in the same API - is still permissible and may also result
79//! in full or partial functionality.
80//!
81//! In that case, the function may still choose to return `Result::Ok`; however, that leaves the
82//! function with a dilemma -- How can it report the non-fatal errors to the caller?
83//!
84//! 1.  **Return a tuple in its `Result::Ok` type**: that wouldn't capture the non-fatal errors in
85//!     the case that a fatal error occurs, so it would also have to be added to the `Result::Err`
86//!     type as well.
87//!
88//!     That adds a bunch of boilerplate, as the function needs to allocate the list and map it
89//!     into the return type for every error return and good return. It also makes the function
90//!     signature much more noisy.
91//!
92//! 2.  **Take a list as a mutable reference?**: Better, but now the caller has to allocate the
93//!     list, and there's no way for it to opt out if it doesn't care about the non-fatal errors.
94//!
95//! 3.  **Maybe add an `Option` to it?** Okay, so a parameter like `errors: Option<&mut Vec<E>>`?
96//!     Getting warmer, but now the child has to do a bunch of
97//!     `if let Some(v) = errors { v.push(error); }` all over the place.
98//!
99//! And what about the caller side of it? For a simple caller, the last point isn't too bad: The
100//! caller just has to allocate the list, pass `Some(&mut errors)` to the child, and check it upon
101//! return.
102//!
103//! But often, the caller itself is keeping its own list of non-fatal errors and may also be a
104//! subfunction to some other caller, and so-on-and-so-forth. In this case, we no longer have
105//! a simple chain of errors, but instead we have a tree of errors -- Each level in the tree
106//! contains all the non-fatal errors that occurred during execution of a function and all
107//! subfunctions in its call graph.
108//!
109//! # Solution
110//!
111//! The main behavior we want is captured by the [WriteErrorList] trait in this crate. It can be
112//! passed as a parameter to any function that wants to be able to report non-fatal errors to its
113//! caller, and it gives the caller flexibility to decide what it wants to do with that
114//! information.
115//!
116//! The main concrete type in this crate is [ErrorList], which stores a list of a single type of
117//! error. Any time a list of errors needs to be stored in memory, this is the type to use. It will
118//! usually be created by the top-level caller using [ErrorList::default], and any subfunction will
119//! give an [ErrorList] of its own error type to the `map_fn` that was passed in by its caller upon
120//! return.
121//!
122//! However, [ErrorList] should rarely be passed as a parameter to a function, as that wouldn't
123//! provide the caller with the flexiblity to decide what strategy it actually wants
124//! to use when collecting its subfunction's non-fatal errors. The caller may want to pass direct
125//! reference to its own error list, it may want to pass a [Sublist] type that automatically
126//! pushes the subfunction's error list to its own error list after mapping, or it may want to
127//! pass the [DontCare] type if it doesn't want to know anything about the
128//! subfunction's non-fatal errors.
129//!
130//! Instead, subfunctions should take `impl WriteErrorList<E>` as a parameter.
131//! This allows any of those types above, as well as mutable references to those types, to be
132//! passed in by the caller. This also allows future caller strategies to be implemented, like
133//! a caller that only cares how many non-fatal errors occurred but doesn't care about the details.
134//!
135//! # Serde
136//!
137//! (This section only applies if the `serde` feature is enabled)
138//!
139//! [ErrorList] implements the `Serialize` trait if the errors it contains do, and
140//! likewise with the `Deserialize` trait. This means that if every error type in the tree
141//! implements these traits then the entire tree can be sent over the wire and recreated elsewhere.
142//! Very useful if the errors are to be examined remotely!
143
144use {
145    std::{
146        error::Error,
147        fmt::{self, Debug, Display, Formatter},
148    },
149    strategy::*,
150};
151
152#[cfg(feature = "serde")]
153use serde::{Deserialize, Serialize};
154
155pub mod strategy;
156
157/// Types that are capable of having errors and sublists of errors pushed onto them
158///
159/// This is the main trait that allows a function to record a list of non-fatal errors it
160/// encounters during its execution. Generally, the [WriteErrorList::push] method will be used when
161/// such an error occurs to record the error and any relevant information.
162///
163/// Often, a function will want to call a subfunction and add any non-fatal errors encountered
164/// to its own list of errors. There are 2 strategies it could use:
165///
166/// 1.  **Let the subfunction directly push onto its error list** For functions that are at the same
167///     level of abstraction and use the same error type, it might make the most sense for them
168///     to just share an error list. In this case, simply pass a mutable reference to the error
169///     list. For any type that implements this trait, a mutable reference to it implements the
170///     trait too. This allows a single function to be a composition of a bunch of functions that
171///     each share a single flat error list.
172///
173/// 2.  **Map the subfunction's error list to the caller** For a subfunction that is at a different
174///     level of abstraction than the caller and uses its own error type, this makes the most sense;
175///     consume the subfunction's entire error list and store it as a single error of the
176///     caller's higher-level error type. Of course, those subfunctions may implement this same
177///     strategy for subfunctions they call, creating a hierarchy of errors.
178///
179///     In this case, call the [WriteErrorList::subwriter()] function as a parameter to
180///     the subfunction. If you need to manipulate the list after the subfunction has returned,
181///     instead call [WriteErrorList::sublist] and pass a mutable reference as a parameter.
182///
183/// Function parameters should always prefer to take an object by this trait, and should rarely
184/// take parameters as concrete types like [ErrorList] or [Sublist].
185/// Doing so would prevent callers from being able to decide what strategy they want to use
186/// to merge the subfunction's errors with its own, and would also prevent them from using the
187/// [DontCare] call if they want to opt out of receiving non-fatal error information.
188///
189/// Passing by this trait may also help prevent logic errors: Directly passing a [Sublist] allows
190/// the subfunction to query the contents of the list it's passed. Functions may incorrectly rely on
191/// the fact that they are always passed an empty list, and will suddenly break if that assumption
192/// doesn't hold.
193pub trait WriteErrorList<E>: Sized + private::Sealed<E> {
194    /// Add an error to the list of errors
195    fn push(&mut self, error: E);
196    /// Create a new mapping error writer with this as its parent
197    ///
198    /// Creates a error writer for use by a subfunction. When the subfunction is finished,
199    /// either by explicitly calling [WriteErrorList::finish] or by letting it drop, the list
200    /// of errors it has written using [WriteErrorList::push] will be passed as an
201    /// [`ErrorList<SubErr>`][ErrorList] to the given `map_fn`, which is expected to map it to
202    /// our error type, `E`.
203    ///
204    /// Use of this function should always be preferred to [WriteErrorList::sublist] when the
205    /// caller does not need to inspect or manipulate the list returned by the subfunction and
206    /// simply wants to pass it upward to its own caller, as this function will pass forward
207    /// alternate strategies for collecting the errors, like [DontCare] (which turns
208    /// [WriteErrorList::push] into a no-op). In constrast, [WriteErrorList::sublist] actually
209    /// materializes a list that will collect all the errors of all the lists below it, even
210    /// if the caller above it passed in a [DontCare].
211    fn subwriter<'sub, SubMapFn, SubErr: 'sub>(
212        &'sub mut self,
213        map_fn: SubMapFn,
214    ) -> impl WriteErrorList<SubErr> + 'sub
215    where
216        SubMapFn: FnOnce(ErrorList<SubErr>) -> E + 'sub;
217    /// Start a new error list with this error list as its parent
218    ///
219    /// This works in a very similar manner to [WriteErrorList::subwriter], but it materializes
220    /// an actual concrete [Sublist] type. This function
221    /// should only be used if the function needs to be able to inspect or manipulate the errors
222    /// returned by the subfunction, as it always collects all errors written by the subfunction's
223    /// call graph. Otherwise, [WriteErrorList::subwriter] should be used.
224    fn sublist<SubMapFn, SubErr>(
225        &mut self,
226        map_fn: SubMapFn,
227    ) -> Sublist<'_, SubErr, SubMapFn, Self, E>
228    where
229        SubMapFn: FnOnce(ErrorList<SubErr>) -> E,
230    {
231        Sublist::new(map_fn, self)
232    }
233    /// Finish this error list
234    ///
235    /// This doesn't normally need to be called, as the [Drop] implementation will take care of
236    /// all the details of cleaning up and ensuring that sublists are mapped up to their parent.
237    ///
238    /// This is mostly useful when a caller maintains a binding to a subfunction's error list
239    /// and passes it by mutable reference instead of by value. Before the caller can continue
240    /// to use its own error list, the sublist must release its exclusive reference.
241    ///
242    /// This function simply calls [drop()], but it's just a bit more clear about the intent.
243    fn finish(self) {
244        drop(self)
245    }
246}
247
248impl<E, T: WriteErrorList<E>> private::Sealed<E> for &mut T {}
249
250impl<E, T: WriteErrorList<E>> WriteErrorList<E> for &mut T {
251    fn push(&mut self, error: E) {
252        WriteErrorList::push(*self, error)
253    }
254    fn subwriter<'sub, SubMapFn, SubErr: 'sub>(
255        &'sub mut self,
256        map_fn: SubMapFn,
257    ) -> impl WriteErrorList<SubErr> + 'sub
258    where
259        SubMapFn: FnOnce(ErrorList<SubErr>) -> E + 'sub,
260    {
261        WriteErrorList::subwriter(*self, map_fn)
262    }
263}
264
265/// The main type that holds a list of errors.
266///
267/// See the module-level docs and the docs for [WriteErrorList].
268#[derive(Debug, Eq, Hash, PartialEq)]
269pub struct ErrorList<E> {
270    errors: Vec<E>,
271}
272
273#[cfg(feature = "serde")]
274impl<E: Serialize> Serialize for ErrorList<E> {
275    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
276    where
277        S: serde::Serializer,
278    {
279        Serialize::serialize(&self.errors, serializer)
280    }
281}
282
283#[cfg(feature = "serde")]
284impl<'de, E: Deserialize<'de>> Deserialize<'de> for ErrorList<E> {
285    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
286    where
287        D: serde::Deserializer<'de>,
288    {
289        Ok(ErrorList {
290            errors: Deserialize::deserialize(deserializer)?,
291        })
292    }
293}
294
295impl<E> ErrorList<E> {
296    /// Returns whether the error list is empty
297    pub fn is_empty(&self) -> bool {
298        self.errors.is_empty()
299    }
300    /// Return the length of the error list
301    pub fn len(&self) -> usize {
302        self.errors.len()
303    }
304    /// Iterate the error list, returning immutable references
305    pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a E>
306    where
307        E: 'a,
308    {
309        self.errors.iter()
310    }
311    /// Iterate the error list, returning mutable references
312    pub fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut E>
313    where
314        E: 'a,
315    {
316        self.errors.iter_mut()
317    }
318}
319
320impl<E> private::Sealed<E> for ErrorList<E> {}
321
322impl<E> WriteErrorList<E> for ErrorList<E> {
323    fn push(&mut self, error: E) {
324        self.errors.push(error);
325    }
326    fn subwriter<'sub, SubMapFn, SubErr: 'sub>(
327        &'sub mut self,
328        map_fn: SubMapFn,
329    ) -> impl WriteErrorList<SubErr> + 'sub
330    where
331        SubMapFn: FnOnce(ErrorList<SubErr>) -> E + 'sub,
332    {
333        self.sublist(map_fn)
334    }
335}
336
337impl<E> Default for ErrorList<E> {
338    fn default() -> Self {
339        Self { errors: Vec::new() }
340    }
341}
342
343impl<E: Error> Display for ErrorList<E> {
344    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
345        writeln!(f, "one or more errors occurred:")?;
346        writeln!(f)?;
347        for (i, e) in self.errors.iter().enumerate() {
348            writeln!(f, "  {i}:")?;
349
350            for line in e.to_string().lines() {
351                writeln!(f, "    {line}")?;
352            }
353
354            writeln!(f)?;
355
356            let mut source = e.source();
357            while let Some(e) = source {
358                writeln!(f, "    caused by:")?;
359
360                for line in e.to_string().lines() {
361                    writeln!(f, "      {line}")?;
362                }
363
364                writeln!(f)?;
365
366                source = e.source();
367            }
368        }
369        Ok(())
370    }
371}
372
373impl<E: Error> Error for ErrorList<E> {}
374
375impl<E> IntoIterator for ErrorList<E> {
376    type Item = <Vec<E> as IntoIterator>::Item;
377    type IntoIter = <Vec<E> as IntoIterator>::IntoIter;
378    fn into_iter(self) -> Self::IntoIter {
379        self.errors.into_iter()
380    }
381}
382
383mod private {
384    /// Prevent users of this crate from implementing traits for their own types
385    pub trait Sealed<E> {}
386}