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, but you can if you do not
wish to use the macro.
§Rust 1.61 and up
Change the return type of the function to Report and wrap
the body of your function with Report::capture.
§Rust before 1.61
Use Report as the error type inside of Result and then
call either Report::capture_into_result or
Report::from_error.
§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
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.
Recommended if you support versions of Rust before 1.61.
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_into_result<T>(
body: impl FnOnce() -> Result<T, E>,
) -> Result<T, Self>
pub fn capture_into_result<T>( body: impl FnOnce() -> Result<T, E>, ) -> Result<T, Self>
Executes a closure that returns a Result, converting the
error variant into a Report.
Recommended if you support versions of Rust before 1.61.
use snafu::{prelude::*, Report};
#[derive(Debug, Snafu)]
struct PlaceholderError;
fn main() -> Result<(), Report<PlaceholderError>> {
Report::capture_into_result(|| {
let _v = may_fail_with_placeholder_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.
Recommended if you only support Rust version 1.61 or above.
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)
}