Skip to content
Update rust error authored by umaumax's avatar umaumax
...@@ -173,19 +173,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { ...@@ -173,19 +173,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let custom_error = std::io::Error::new(std::io::ErrorKind::Other, "oh no!"); let custom_error = std::io::Error::new(std::io::ErrorKind::Other, "oh no!");
``` ```
## panicではなくエラーを返す形式のassertで関数の引数チェックなどを行う
[ensure in anyhow - Rust]( https://docs.rs/anyhow/1.0.51/anyhow/macro.ensure.html )を利用すれば良い
``` rust ``` rust
fn user(id: i32) -> Result<(), anyhow::Error> { fn example() -> Result<(), Box<dyn std::error::Error>> {
anyhow::ensure!(id != 0, "id requires non-zero value"); // pattern 1
Err(format!("Something went bad: {}", 1 + 1))?;
// pattern 2
Err("error")?;
Ok(()) Ok(())
} }
``` ```
* [de-vri-es/assert2-rs: assert\!() and check\!() macros for Rust, inspired by Catch2]( https://github.com/de-vri-es/assert2-rs )`check!`を使えば良さそうと思ったがこれはスコープが終了するまでpanicを遅延するものである
* [joelparkerhenderson/assure-rust-crate: Assure rust crate: macros for Rust runtime checks and error handling]( https://github.com/joelparkerhenderson/assure-rust-crate/ ): こちらはあまり利用されていない
## 1つの関数に複数の種類のエラーが入り交じる場合の返り値のResultの型はどうする? ## 1つの関数に複数の種類のエラーが入り交じる場合の返り値のResultの型はどうする?
1. `Result<(),Box<dyn std::error::Error>>` 1. `Result<(),Box<dyn std::error::Error>>`
...@@ -303,7 +300,20 @@ std::fs::File::open(path).with_context(|| format!("Failed to open config file: { ...@@ -303,7 +300,20 @@ std::fs::File::open(path).with_context(|| format!("Failed to open config file: {
`use anyhow::Context`によって、`std::error::Error``.context()`などが生える `use anyhow::Context`によって、`std::error::Error``.context()`などが生える
## `ensure!`の調査 ## panicではなくエラーを返す形式のassertで関数の引数チェックなどを行う
`anyhow::Error`を返り値とする場合には[ensure in anyhow - Rust]( https://docs.rs/anyhow/1.0.51/anyhow/macro.ensure.html )を利用すれば良い
``` rust
fn user(id: i32) -> Result<(), anyhow::Error> {
anyhow::ensure!(id != 0, "id requires non-zero value");
Ok(())
}
```
* [de-vri-es/assert2-rs: assert\!() and check\!() macros for Rust, inspired by Catch2]( https://github.com/de-vri-es/assert2-rs )`check!`を使えば良さそうと思ったがこれはスコープが終了するまでpanicを遅延するものである
* [joelparkerhenderson/assure-rust-crate: Assure rust crate: macros for Rust runtime checks and error handling]( https://github.com/joelparkerhenderson/assure-rust-crate/ ): こちらはあまり利用されていない
### `ensure!`の調査
要約: `failchain::ensure`を利用するか内部で定義するかの2通りがある 要約: `failchain::ensure`を利用するか内部で定義するかの2通りがある
* [failchain::ensure - Rust]( https://docs.rs/failchain/latest/failchain/macro.ensure.html ): `Err($e.into())` * [failchain::ensure - Rust]( https://docs.rs/failchain/latest/failchain/macro.ensure.html ): `Err($e.into())`
...@@ -312,7 +322,7 @@ std::fs::File::open(path).with_context(|| format!("Failed to open config file: { ...@@ -312,7 +322,7 @@ std::fs::File::open(path).with_context(|| format!("Failed to open config file: {
* [snafu::ensure - Rust]( https://docs.rs/snafu/0.6.10/snafu/macro.ensure.html ): `.fail().map_err(::core::convert::Into::into);` * [snafu::ensure - Rust]( https://docs.rs/snafu/0.6.10/snafu/macro.ensure.html ): `.fail().map_err(::core::convert::Into::into);`
* `ensure!``.fail()`を持つ型で利用可能... * `ensure!``.fail()`を持つ型で利用可能...
* [ensure in anyhow - Rust]( https://docs.rs/anyhow/1.0.51/anyhow/macro.ensure.html ): `private::Err(anyhow!())` * [ensure in anyhow - Rust]( https://docs.rs/anyhow/1.0.51/anyhow/macro.ensure.html ): `private::Err(anyhow!())`
* `anyhow::Error`以外では`ensure!`が利用できない... * 関数の返り値が`anyhow::Error`以外では`ensure!`が利用できない...
* 凍結注意 [failure::ensure - Rust]( https://docs.rs/failure/latest/failure/macro.ensure.html ): `failure::Error` * 凍結注意 [failure::ensure - Rust]( https://docs.rs/failure/latest/failure/macro.ensure.html ): `failure::Error`
* 凍結注意 [error_chain::ensure - Rust]( https://docs.rs/error-chain/0.12.4/error_chain/macro.ensure.html ): `Err($e.into())` * 凍結注意 [error_chain::ensure - Rust]( https://docs.rs/error-chain/0.12.4/error_chain/macro.ensure.html ): `Err($e.into())`
* 内部定義例 * 内部定義例
... ...
......