From 03f5384d18e0d352ae01d9a82fb4c1e2d98c850e Mon Sep 17 00:00:00 2001 From: wuxianao <100472806+wuxianao@users.noreply.github.com> Date: Mon, 19 Jun 2023 17:59:27 +0800 Subject: [PATCH] Add with_context in anyhow --- src/advance/errors.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/advance/errors.md b/src/advance/errors.md index c593f520..ba029197 100644 --- a/src/advance/errors.md +++ b/src/advance/errors.md @@ -684,6 +684,24 @@ fn render() -> Result { Ok(source) } ``` +`anyhow`还可以利用``context``和`with_context`来给错误加上上下文(context), 比如说 +```rust + let file = std::env::var("MARKDOWN") + .with_context(|| "environment variable not found", )?; +``` +这样报错时样式如下 +``` +Error: environment variable not found + +Caused by: + environment variable not found +``` +借助于`with_context`, 也可以将报错与实际的环境结合起来. +```rust + let varname: &str = "MARKDOWN"; + let file = std::env::var(varname) + .with_context(|| format!("environment variable {} not found", varname) )?; +``` 关于如何选用 `thiserror` 和 `anyhow` 只需要遵循一个原则即可:**是否关注自定义错误消息**,关注则使用 `thiserror`(常见业务代码),否则使用 `anyhow`(编写第三方库代码)。