Skip to content

error-stack: How to implicitly wrap errors? #3919

Answered by TimDiekmann
SUPERCILEX asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, the best option here is using .map_err(CliError::from) as the CliError does contain the full error.

If you're willing to change your enum CliError it's possible to let error-stack handle this properly, e.g.:

#[derive(Error, Debug)]
pub enum CliError {
    #[error("The CLI encountered an error ... more context info ... ")]
    ClientHistory { /* more context on your error */ },
    ...
}

fn client_history() -> Result<_, client_history_core::Error> { ... }

fn my_function() -> Result<_, Report<CliError>> {
    client_history().change_context(CliError::ClientHistory { /* ... */ })?;
    // or
    client_history().change_context_lazy(|| CliError::ClientHistory { /* ... */ })?;
	...
}

Th…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@SUPERCILEX
Comment options

@TimDiekmann
Comment options

Answer selected by TimDiekmann
@SUPERCILEX
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
area/libs > error-stack Affects the `error-stack` crate (library) area/libs Relates to first-party libraries/crates/packages (area)
2 participants