< 返回我的博客

黑豆腐 发表于 2020-04-17 08:57

Tags:rust

前言

之前我们借助讲解failure库,详细说明的Rust错误处理的哲学,这有助于我们理解今天的主题,anyhowthiserror

Anyhow提供了一个anyhow::Error trait(有点类似failure::Error)。而得益于std::error::Error所做的修改,它anyhow::Error与std::error::Error兼容的。也就是说,对于自定义的错误类型,只需要实现std::error::Error即可。这对于程序的兼容性是一大利好,也因此failure库被日渐废弃。而thiserror正是方便大家为自定义的错误使用宏实现std::error::Error而设计的。

依赖

[dependencies]
anyhow = "1.0"
thiserror = "1.0"

Anyhow用法

在需要返回Result的地方,使用Result<T, anyhow::Error>或者等价的anyhow::Result<T>,就可以利用抛出任何类型实现了std::error::Error的错误

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)
}

还可以给错误加上上下文(context)

use anyhow::{Context, Result};

fn main() -> Result<()> {
    ...

    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

这样就会在发送错误时看到上下文

Error: Failed to read instrs from ./path/to/instrs.json

Caused by:
    No such file or directory (os error 2)

anyhow宏可以基于错误字符串产生一个anyhow::Error(类似于failure中的format_err宏)

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

thiserror用法

use thiserror::Error;

#[derive(Error, Debug)]
pub enum DataStoreError {
    #[error("data store disconnected")]
    Disconnect(#[from] io::Error),
    #[error("the data for key `{0}` is not available")]
    Redaction(String),
    #[error("invalid header (expected {expected:?}, found {found:?})")]
    InvalidHeader {
        expected: String,
        found: String,
    },
    #[error("unknown data store error")]
    Unknown,
}

实际上,只要为struct或者每个成员提供#[error("...")],那么就会实现Display,具体语法如下

  • #[error("{var}")] ⟶ write!("{}", self.var)
  • #[error("{0}")] ⟶ write!("{}", self.0)
  • #[error("{var:?}")] ⟶ write!("{:?}", self.var)
  • #[error("{0:?}")] ⟶ write!("{:?}", self.0)

通过sourcebacktrace字段(或者#[source]#[backtrace])可以指定source()和backtrace()的返回。还可以通过#[from]实现为source实现From

#[derive(Error, Debug)]
pub enum MyError {
    Io {
        #[from]
        source: io::Error,
        backtrace: Backtrace,
    },
}

小结

通过anyhow和thiserror,错误处理是不是变得轻松多了呢?直接支持std::error:Error,真香!

评论区

写评论
lcoder 2023-12-30 08:38

thiserror用法看的不太明白,就是为了方便实现Display吗

itfanr 2022-12-27 14:48

--
👇
overheat: "之前我们借助讲解failure库,详细说明的Rust错误处理的哲学" 这篇文章在哪里啊?

https://rustcc.cn/article?id=c7ff2472-b990-4b63-93ab-6ea16009ebe0

overheat 2021-10-19 19:17

"之前我们借助讲解failure库,详细说明的Rust错误处理的哲学" 这篇文章在哪里啊?

1 共 3 条评论, 1 页