| 1 |
#![allow( |
| 2 |
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 |
| 3 |
clippy::nonstandard_macro_braces, |
| 4 |
)] |
| 5 |
|
| 6 |
use anyhow::anyhow; |
| 7 |
use std::error::Error as StdError; |
| 8 |
use std::io; |
| 9 |
use thiserror::Error; |
| 10 |
|
| 11 |
#[derive(Error, Debug)] |
| 12 |
#[error("outer")] |
| 13 |
struct MyError { |
| 14 |
source: io::Error, |
| 15 |
} |
| 16 |
|
| 17 |
#[test] |
| 18 |
fn test_boxed_str() { |
| 19 |
let error = Box::<dyn StdError + Send + Sync>::from("oh no!"); |
| 20 |
let error = anyhow!(error); |
| 21 |
assert_eq!("oh no!", error.to_string()); |
| 22 |
assert_eq!( |
| 23 |
"oh no!", |
| 24 |
error |
| 25 |
.downcast_ref::<Box<dyn StdError + Send + Sync>>() |
| 26 |
.unwrap() |
| 27 |
.to_string() |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn test_boxed_thiserror() { |
| 33 |
let error = MyError { |
| 34 |
source: io::Error::new(io::ErrorKind::Other, "oh no!"), |
| 35 |
}; |
| 36 |
let error = anyhow!(error); |
| 37 |
assert_eq!("oh no!", error.source().unwrap().to_string()); |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn test_boxed_anyhow() { |
| 42 |
let error = anyhow!("oh no!").context("it failed"); |
| 43 |
let error = anyhow!(error); |
| 44 |
assert_eq!("oh no!", error.source().unwrap().to_string()); |
| 45 |
} |