Macro error_stack::report

source ·
macro_rules! report {
    ($err:expr $(,)?) => { ... };
}
Expand description

Creates a Report from the given parameters.

The parameters may either be Context or a Report. The returned Report will use the the provided type as context.

§Examples

Create a Report from Error:

use std::fs;

use error_stack::report;

match fs::read_to_string("/path/to/file") {
    Ok(content) => println!("file contents: {content}"),
    Err(err) => return Err(report!(err)),
}

Create a Report from Context:

use core::fmt;

use error_stack::{report, Context};

#[derive(Debug)]
struct PermissionDenied(User, Resource);

impl fmt::Display for PermissionDenied {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        ...
}

impl Context for PermissionDenied {}

if !has_permission(&user, &resource) {
    return Err(report!(PermissionDenied(user, resource)));
}