Skip to content

What is the most robust way of downcasting an anyhow::Error? #431

@thomaseizinger

Description

@thomaseizinger

I am using anyhow extensively in my application. At the outer layer of an event loop, I am then downcasting the error to check how it needs it be handled and which log-level is appropriate.

So far, I've used root_cause for this but recently, that has failed on my because I was using bail! further down but without applying any additional .context. Hence, the resulting anyhow::Error did in fact not have a source and the check never succeeded, causing the error handling to be buggy.

I then discovered the interactions of context and the is and downcast methods.

My question now is: What is the most robust way of checking whether an anyhow::Error is / contains a particular type? It seems like to cover all bases, I need to both check is and root_cause.

Type-erasure is great for ergonomics but less so if it can easily be misused or broken by a refactoring (like the removal / addition of context).

Should is be extended to also walk all sources of the original error prior to being turned into an anyhow::Error?

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