macro_rules! bail {
($err:expr) => { ... };
[$($err:expr),+ $(,)?] => { ... };
}
Expand description
Creates a Report
and returns it as Result
.
Shorthand for return Err(report!(..))
.
§unstable
The match arm: [$($err:expr),+ $(,)?]
is considered unstable and can be used to construct a
Report<[C]>
.
§Examples
use std::fs;
use error_stack::bail;
match fs::read_to_string("/path/to/file") {
Ok(content) => println!("file contents: {content}"),
Err(err) => bail!(err),
}
use core::error::Error;
use core::fmt;
use error_stack::bail;
#[derive(Debug)]
struct PermissionDenied(User, Resource);
impl fmt::Display for PermissionDenied {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
...
}
}
impl Error for PermissionDenied {}
if !has_permission(&user, &resource) {
bail!(PermissionDenied(user, resource));
}
Create a Report<[C]>
from multiple errors (unstable only):
use error_stack::bail;
#[derive(Debug)]
struct PermissionDenied(User, Resource);
impl core::fmt::Display for PermissionDenied {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
...
}
}
impl core::error::Error for PermissionDenied {}
// You might want to look into `ReportSink` for a more incremental approach.
if !has_permission(&user, &create_user) && !has_permission(&user, &create_resource) {
bail![
PermissionDenied(user, create_user),
PermissionDenied(user, create_resource)
];
}