pub struct Report<E>(/* private fields */);Expand description
Opinionated solution to format an error in a user-friendly
way. Useful as the return type from main and test functions.
Most users will use the snafu::report procedural macro
instead of directly using this type. To use it directly, change
the return type of the function to Report and wrap the body
of your function with Report::capture.
§Nightly Rust
Enabling the unstable-try-trait feature flag will
allow you to use the ? operator directly:
use snafu::{prelude::*, Report};
fn main() -> Report<PlaceholderError> {
let _v = may_fail_with_placeholder_error()?;
Report::ok()
}§Interaction with the Provider API
If you return a Report from your function and enable the
unstable-provider-api feature flag, additional
capabilities will be added:
- If provided, a
Locationwill be appended to each error message. - If provided, a
Backtracewill be included in the output. - If provided, a
ExitCodewill be used as the return value.
§Stability of the output
The exact content and format of a displayed Report are not
stable, but this type strives to print the error and as much
user-relevant information in an easily-consumable manner
Implementations§
Source§impl<E> Report<E>
impl<E> Report<E>
Sourcepub fn from_error(error: E) -> Self
pub fn from_error(error: E) -> Self
Convert an error into a Report.
use snafu::{prelude::*, Report};
#[derive(Debug, Snafu)]
struct PlaceholderError;
fn main() -> Result<(), Report<PlaceholderError>> {
let _v = may_fail_with_placeholder_error().map_err(Report::from_error)?;
Ok(())
}
fn may_fail_with_placeholder_error() -> Result<u8, PlaceholderError> {
Ok(42)
}Sourcepub fn capture(body: impl FnOnce() -> Result<(), E>) -> Self
pub fn capture(body: impl FnOnce() -> Result<(), E>) -> Self
Executes a closure that returns a Result, converting any
error to a Report.
use snafu::{prelude::*, Report};
#[derive(Debug, Snafu)]
struct PlaceholderError;
fn main() -> Report<PlaceholderError> {
Report::capture(|| {
let _v = may_fail_with_placeholder_error()?;
Ok(())
})
}
fn may_fail_with_placeholder_error() -> Result<u8, PlaceholderError> {
Ok(42)
}