-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert between anyhow::Error and eyre::Result
- Loading branch information
1 parent
df42dc4
commit 5d5e18c
Showing
8 changed files
with
101 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#![feature(error_generic_member_access)] | ||
|
||
use eyre::Report; | ||
use std::fmt::Display; | ||
|
||
#[derive(Debug)] | ||
struct RootError; | ||
|
||
impl Display for RootError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "RootError") | ||
} | ||
} | ||
|
||
impl std::error::Error for RootError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
None | ||
} | ||
} | ||
|
||
fn this_function_fails() -> anyhow::Result<()> { | ||
use anyhow::Context; | ||
|
||
Err(RootError).context("Ouch!").context("Anyhow context A") | ||
} | ||
|
||
fn test_failure() -> eyre::Result<()> { | ||
use anyhow::Context; | ||
this_function_fails().context("Anyhow context B")?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn anyhow_conversion() { | ||
use eyre::WrapErr; | ||
let error: Report = test_failure().wrap_err("Eyre context").unwrap_err(); | ||
|
||
eprintln!("Error: {:?}", error); | ||
|
||
let chain = error.chain().map(ToString::to_string).collect::<Vec<_>>(); | ||
assert_eq!( | ||
chain, | ||
[ | ||
"Eyre context", | ||
// Anyhow context | ||
"Anyhow context B", | ||
"Anyhow context A", | ||
// Anyhow error | ||
"Ouch!", | ||
// Original concrete error, shows up in chain too | ||
"RootError" | ||
] | ||
); | ||
|
||
let backtrace = std::error::request_ref::<std::backtrace::Backtrace>(&*error).unwrap(); | ||
dbg!(backtrace); | ||
} |