ez_err/
lib.rs

1//! # ez-err
2//! The goal of this crate is to add simple and easy-to-use error handling. The
3//! amount of boilerplate code required to get the full set of features should
4//! be minimal. Ez-err includes the stack trace information directly in the error
5//! type in case of [`Err`] (almost no overhead if [`Ok`]). The approach is especially
6//! useful for any case where the error propagation might be deferred (code storing
7//! [`Result`]s in a [`Vec`] and only later checking whether they are [`Ok`] or not).
8//!
9//! It is also worth taking a look at [`eget`] and [`eget_mut`] for more advanced error
10//! messages which include more information about the error.
11//!
12//! # Use cases
13//! This crate can be useful for general purpose error handling. However, it should
14//! be helpful in scenarios where no source code information should be contained in
15//! the resulting product for any reason. Only relying on ez-err for error handling
16//! will provide full power over the error output. Disabling stack trace collection
17//! should also remove any source code information from the binary that would have
18//! been generated with ez-err.
19//!
20//! # How to use / Example
21//! To use ez-err, you need to add `use ez_err::prelude::*` to your source file.
22//! Once that is done, you can use the custom [`Result<T>`] type in your functions
23//! and then handle all errors by using `xxx.loc(flc!())?`. It is possible to use this
24//! same pattern when converting from any error type to [`EzError`].
25//! ```ignore
26//! use ez_err::prelude::*;
27//! use std::io::Write;
28//!
29//! fn save_log_output(log: String) -> Result<()> {
30//!     // Try to open the file or return an error with stack trace.
31//!     let mut file = std::fs::File::open("...").loc(flc!())?;
32//!     // Try to write to the file or return an error with stack trace.
33//!     write!(&mut file, "{}", log).loc(flc!())?;
34//!
35//!     // Everything went well without an error.
36//!     Ok(())
37//! }
38//!
39//! fn quit(log: String) {
40//!     // Print the error to the console if there is any.
41//!     let _optional_return: Option<()> = save_log_output(log).handle();
42//! }
43//! ```
44//!
45//! # How does it work?
46//! `xxx.loc(flc!())?` is made up of 3 parts: the [`loc`] function, the [`flc!`] macro,
47//! and the standard `?` operator. The [`flc!`] macro will first expand to a [`ConstLocation`]
48//! containing information about the source code location where the macro was invoked.
49//! This information is passed into the `loc` function, which will store the location
50//! ONLY if it is currently an [`Err`] to minimize the overhead. The standard `?` operator
51//! will then perform the already existing logic for error propagation. This approach
52//! requires no special backtrace configuration and can produce clean stack traces. It
53//! should be very fast by compiling down to just an additional if statement in the [`Ok`]-case.
54//!
55//! # Why should I use ez-err?
56//! The advantage of this crate over others is simplicity. Other error handling
57//! crates require manually adding error reasons, which can be helpful but can
58//! often be overkill.
59//!
60//! Here is an example to show the difference between this crate and a popular
61//! one ([error-chain](https://crates.io/crates/error-chain)):
62//! ```ignore
63//! fn error_producer() -> Result<i32> { /* ... */ }
64//!
65//! // Propagate an error using error-chain
66//! fn use_error_chain() -> Result<i32> {
67//!     /* ... */
68//!     let value: i32 = error_producer().chain_err(|| "error when getting value")?;
69//!     Ok(value + 1)
70//! }
71//!
72//! // Propagate an error using ez-err
73//! fn use_ez_err() -> ez_err::Result<i32> {
74//!     /* ... */
75//!     let value: i32 = error_producer().loc(flc!())?;
76//!     Ok(value + 1)
77//! }
78//! ```
79//!
80//! # Features
81//! * `log` - enable compatibility with the [log](https://crates.io/crates/log) crate. The code will by default output to `error!(...)`.
82//! * `no_stacktrace` - disable any stacktrace collection. This might be useful in a scenario where leaking source information is problematic.
83//!
84//! # License
85//! This project is licensed under the [MIT license](https://github.com/MariusSoft-LLC/ez-err/blob/main/LICENSE).
86//!
87//! # Contribution
88//! Any contribution intentionally submitted for inclusion in the work by you, shall be licensed as MIT, without any additional terms or conditions.
89//!
90//! [`loc`]: prelude::LocData::loc
91//! [`Result`]: prelude::Result
92//! [`Result<T>`]: prelude::Result
93//! [`EzError`]: prelude::EzError
94//! [`ConstLocation`]: prelude::ConstLocation
95//! [`eget`]: prelude::SliceExt::eget
96//! [`eget_mut`]: prelude::SliceExtMut::eget_mut
97
98#![warn(missing_docs)]
99#![deny(warnings)]
100
101pub mod core;
102pub mod prelude;
103pub mod slice_ext;