Skip to content

Creating anyhow! from Arc<Error> miss error source #405

@mickaelmagniez

Description

@mickaelmagniez

I'm trying to convert a Arc<Error> (returned by moka crate) to an Error

So what i basically do is

moka().map_err(|e| anyhow::anyhow!(e))?

But doing this, i lose information, before the map_err, printing with {:#} give me all the chained errors, but after the map_err, i get only the last one.

I've reproduced issue with the following code

let error1 = anyhow::Error::msg("error1");
let error2 = anyhow::Error::from(error1).context("error2");
println!("error 2 '{:#}'", error2);
println!("error 2 src '{:?}'", error2.source());

is printing

error 2 'error2: error1'
error 2 src 'Some("error1")'

Recreating an error from error2 still works

let error_from_error = anyhow::anyhow!(error2);

But if i try to do this this an Arc

let arc_error = std::sync::Arc::new(error2);
let error_from_arc = anyhow::anyhow!(arc_error);
println!("src '{:?}'",  error_from_arc.source());
println!("error_from_arc '{:#}'", error_from_arc);

is printing

src 'None'
error_from_arc 'error2'

But

println!("error_from_arc '{:#?}'", error_from_arc);

is printing

error_from_arc 'Error {
    context: "error2",
    source: "error1",
}'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions