anyhow

Flexible concrete Error type built on std::error::Error

Latest version: 1.0.94 registry icon
Maintenance score
100
Safety score
100
Popularity score
60
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
1.0.94 0 0 0 0 0
1.0.93 0 0 0 0 0
1.0.92 0 0 0 0 0
1.0.91 0 0 0 0 0
1.0.90 0 0 0 0 0
1.0.89 0 0 0 0 0
1.0.88 0 0 0 0 0
1.0.87 0 0 0 0 0
1.0.86 0 0 0 0 0
1.0.85 0 0 0 0 0
1.0.84 0 0 0 0 0
1.0.83 0 0 0 0 0
1.0.82 0 0 0 0 0
1.0.81 0 0 0 0 0
1.0.80 0 0 0 0 0
1.0.79 0 0 0 0 0
1.0.78 0 0 0 0 0
1.0.77 0 0 0 0 0
1.0.76 0 0 0 0 0
1.0.75 0 0 0 0 0
1.0.74 0 0 0 0 0
1.0.73 0 0 0 0 0
1.0.72 0 0 0 0 0
1.0.71 0 0 0 0 0
1.0.70 0 0 0 0 0
1.0.69 0 0 0 0 0
1.0.68 0 0 0 0 0
1.0.67 0 0 0 0 0
1.0.66 0 0 0 0 0
1.0.65 0 0 0 0 0
1.0.64 0 0 0 0 0
1.0.63 0 0 0 0 0
1.0.62 0 0 0 0 0
1.0.61 0 0 0 0 0
1.0.60 0 0 0 0 0
1.0.59 0 0 0 0 0
1.0.58 0 0 0 0 0
1.0.57 0 0 0 0 0
1.0.56 0 0 0 0 0
1.0.55 0 0 0 0 0
1.0.54 0 0 0 0 0
1.0.53 0 0 0 0 0
1.0.52 0 0 0 0 0
1.0.51 0 0 0 0 0
1.0.50 0 0 0 0 0
1.0.49 0 0 0 0 0
1.0.48 0 0 0 0 0
1.0.47 0 0 0 0 0
1.0.46 0 0 0 0 0
1.0.45 0 0 0 0 0
1.0.44 0 0 0 0 0
1.0.43 0 0 0 0 0
1.0.42 0 0 0 0 0
1.0.41 0 0 0 0 0
1.0.40 0 0 0 0 0
1.0.39 0 0 0 0 0
1.0.38 0 0 0 0 0
1.0.37 0 0 0 0 0
1.0.36 0 0 0 0 0
1.0.35 0 0 0 0 0
1.0.34 0 0 0 0 0
1.0.33 0 0 0 0 0
1.0.32 0 0 0 0 0
1.0.31 0 0 0 0 0
1.0.30 0 0 0 0 0
1.0.29 0 0 0 0 0
1.0.28 0 0 0 0 0
1.0.27 0 0 0 0 0
1.0.26 0 0 0 0 0
1.0.25 0 0 0 0 0
1.0.24 0 0 0 0 0
1.0.23 0 0 0 0 0
1.0.22 0 0 0 0 0
1.0.21 0 0 0 0 0
1.0.20 0 0 0 0 0
1.0.19 0 0 0 0 0
1.0.18 0 0 0 0 0
1.0.17 0 0 0 0 0
1.0.16 0 0 0 0 0
1.0.15 0 0 0 0 0
1.0.14 0 0 0 0 0
1.0.13 0 0 0 0 0
1.0.12 0 0 0 0 0
1.0.11 0 0 0 0 0
1.0.10 0 0 0 0 0
1.0.9 0 0 0 0 0
1.0.8 0 0 0 0 0
1.0.7 0 0 0 0 0
1.0.6 0 0 0 0 0
1.0.5 0 0 0 0 0
1.0.4 0 0 0 0 0
1.0.3 0 0 0 0 0
1.0.2 0 0 0 0 0
1.0.1 0 0 0 0 0
1.0.0 0 0 0 0 0
0.0.0 0 0 0 0 0

Stability
Latest release:

1.0.94 - This version is safe to use because it has no known security vulnerabilities at this time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

Apache-2.0   -   Apache License 2.0

Not a wildcard

Not proprietary

OSI Compliant


MIT   -   MIT License

Not a wildcard

Not proprietary

OSI Compliant



Anyhow ¯\_(°ペ)_/¯

github crates.io docs.rs build status

This library provides anyhow::Error, a trait object based error type for easy idiomatic error handling in Rust applications.

[dependencies]
anyhow = "1.0"

Compiler support: requires rustc 1.39+


Details

  • Use Result<T, anyhow::Error>, or equivalently anyhow::Result<T>, as the return type of any fallible function.

    Within the function, use ? to easily propagate any error that implements the std::error::Error trait.

    use anyhow::Result;
    
    fn get_cluster_info() -> Result<ClusterMap> {
        let config = std::fs::read_to_string("cluster.json")?;
        let map: ClusterMap = serde_json::from_str(&config)?;
        Ok(map)
    }
  • Attach context to help the person troubleshooting the error understand where things went wrong. A low-level error like "No such file or directory" can be annoying to debug without more context about what higher level step the application was in the middle of.

    use anyhow::{Context, Result};
    
    fn main() -> Result<()> {
        ...
        it.detach().context("Failed to detach the important thing")?;
    
        let content = std::fs::read(path)
            .with_context(|| format!("Failed to read instrs from {}", path))?;
        ...
    }
    Error: Failed to read instrs from ./path/to/instrs.json
    
    Caused by:
        No such file or directory (os error 2)
  • Downcasting is supported and can be by value, by shared reference, or by mutable reference as needed.

    // If the error was caused by redaction, then return a
    // tombstone instead of the content.
    match root_cause.downcast_ref::<DataStoreError>() {
        Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),
        None => Err(error),
    }
  • If using Rust ≥ 1.65, a backtrace is captured and printed with the error if the underlying error type does not already provide its own. In order to see backtraces, they must be enabled through the environment variables described in std::backtrace:

    • If you want panics and errors to both have backtraces, set RUST_BACKTRACE=1;
    • If you want only errors to have backtraces, set RUST_LIB_BACKTRACE=1;
    • If you want only panics to have backtraces, set RUST_BACKTRACE=1 and RUST_LIB_BACKTRACE=0.
  • Anyhow works with any error type that has an impl of std::error::Error, including ones defined in your crate. We do not bundle a derive(Error) macro but you can write the impls yourself or use a standalone macro like thiserror.

    use thiserror::Error;
    
    #[derive(Error, Debug)]
    pub enum FormatError {
        #[error("Invalid header (expected {expected:?}, got {found:?})")]
        InvalidHeader {
            expected: String,
            found: String,
        },
        #[error("Missing attribute: {0}")]
        MissingAttribute(String),
    }
  • One-off error messages can be constructed using the anyhow! macro, which supports string interpolation and produces an anyhow::Error.

    return Err(anyhow!("Missing attribute: {}", missing));

    A bail! macro is provided as a shorthand for the same early return.

    bail!("Missing attribute: {}", missing);

No-std support

In no_std mode, almost all of the same API is available and works the same way. To depend on Anyhow in no_std mode, disable our default enabled "std" feature in Cargo.toml. A global allocator is required.

[dependencies]
anyhow = { version = "1.0", default-features = false }

With versions of Rust older than 1.81, no_std mode may require an additional .map_err(Error::msg) when working with a non-Anyhow error type inside a function that returns Anyhow's error type, as the trait that ?-based error conversions are defined by is only available in std in those old versions.


Comparison to failure

The anyhow::Error type works something like failure::Error, but unlike failure ours is built around the standard library's std::error::Error trait rather than a separate trait failure::Fail. The standard library has adopted the necessary improvements for this to be possible as part of RFC 2504.


Comparison to thiserror

Use Anyhow if you don't care what error type your functions return, you just want it to be easy. This is common in application code. Use thiserror if you are a library that wants to design your own dedicated error type(s) so that on failures the caller gets exactly the information that you choose.


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.