-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add compatibility layer for eyre
and anyhow
#763
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e365eae
Add compatibility to `anyhow`
TimDiekmann 1ef5004
Add compatibility to `eyre`
TimDiekmann 82b6e69
Add optional dependencies to test suite
TimDiekmann 59c40af
Make `Compat` only available with `eyre` or `anyhow` features
TimDiekmann b9a089f
Fix clippy
TimDiekmann f4ce051
Allow `unused_mut` in `no-std`
TimDiekmann 5690234
Add tests for conversions
TimDiekmann 4c40a66
Ignore `miri` for running `eyre` tests
TimDiekmann 82b92ec
Merge branch 'main' into td/error-stack-compat
TimDiekmann e388213
Rename `Compat` into `IntoReportCompat`
TimDiekmann 00996b9
Support `eyre` and `anyhow` backtraces
TimDiekmann e29eba5
Add a reason on `IntoReportCompat` why the trait is needed
TimDiekmann b137334
Clarify docs
TimDiekmann 3f97cad
Merge branch 'main' into td/error-stack-compat
TimDiekmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
extend = { path = "../../../.github/scripts/rust/Makefile.toml" } | ||
|
||
[env] | ||
CARGO_CLIPPY_HACK_FLAGS = "--workspace --feature-powerset --exclude-features default" | ||
CARGO_TEST_HACK_FLAGS = "--workspace --feature-powerset --exclude-features default" | ||
CARGO_MIRI_HACK_FLAGS = "--workspace --feature-powerset --exclude-features default" | ||
CARGO_CLIPPY_HACK_FLAGS = "--workspace --feature-powerset --exclude-features default --optional-deps eyre,anyhow" | ||
CARGO_TEST_HACK_FLAGS = "--workspace --feature-powerset --exclude-features default --optional-deps eyre,anyhow" | ||
CARGO_MIRI_HACK_FLAGS = "--workspace --feature-powerset --exclude-features default --optional-deps eyre,anyhow" |
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,101 @@ | ||
#[cfg(nightly)] | ||
use core::any::Demand; | ||
use core::{fmt, panic::Location}; | ||
#[cfg(all(nightly, feature = "std"))] | ||
use std::{backtrace::Backtrace, error::Error}; | ||
|
||
use anyhow::Error as AnyhowError; | ||
|
||
use crate::{Context, Frame, IntoReportCompat, Report, Result}; | ||
|
||
#[repr(transparent)] | ||
struct AnyhowContext(AnyhowError); | ||
|
||
impl fmt::Debug for AnyhowContext { | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Debug::fmt(&self.0, fmt) | ||
} | ||
} | ||
|
||
impl fmt::Display for AnyhowContext { | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Display::fmt(&self.0, fmt) | ||
} | ||
} | ||
|
||
impl Context for AnyhowContext { | ||
#[cfg(nightly)] | ||
fn provide<'a>(&'a self, demand: &mut Demand<'a>) { | ||
demand.provide_ref(&self.0); | ||
#[cfg(feature = "std")] | ||
if let Some(backtrace) = self.0.chain().find_map(Error::backtrace) { | ||
demand.provide_ref(backtrace); | ||
} | ||
} | ||
} | ||
|
||
impl<T> IntoReportCompat for core::result::Result<T, AnyhowError> { | ||
type Err = AnyhowError; | ||
type Ok = T; | ||
|
||
#[track_caller] | ||
fn into_report(self) -> Result<T, AnyhowError> { | ||
match self { | ||
Ok(t) => Ok(t), | ||
Err(anyhow) => { | ||
#[cfg(feature = "std")] | ||
let sources = anyhow | ||
.chain() | ||
.skip(1) | ||
.map(ToString::to_string) | ||
.collect::<Vec<_>>(); | ||
|
||
#[cfg(all(nightly, feature = "std"))] | ||
let backtrace = anyhow | ||
.chain() | ||
.all(|error| error.backtrace().is_none()) | ||
.then(Backtrace::capture); | ||
|
||
#[cfg_attr(not(feature = "std"), allow(unused_mut))] | ||
let mut report = Report::from_frame( | ||
Frame::from_compat::<AnyhowError, AnyhowContext>( | ||
AnyhowContext(anyhow), | ||
Location::caller(), | ||
), | ||
#[cfg(all(nightly, feature = "std"))] | ||
backtrace, | ||
#[cfg(feature = "spantrace")] | ||
Some(tracing_error::SpanTrace::capture()), | ||
); | ||
|
||
#[cfg(feature = "std")] | ||
for source in sources { | ||
report = report.attach_printable(source); | ||
} | ||
|
||
Err(report) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use anyhow::anyhow; | ||
|
||
use crate::{test_helper::messages, IntoReportCompat}; | ||
|
||
#[test] | ||
fn conversion() { | ||
let anyhow: Result<(), _> = Err(anyhow!("A").context("B").context("C")); | ||
|
||
let report = anyhow.into_report().unwrap_err(); | ||
#[cfg(feature = "std")] | ||
let expected_output = ["A", "B", "C"]; | ||
#[cfg(not(feature = "std"))] | ||
let expected_output = ["C"]; | ||
for (anyhow, expected) in messages(&report).into_iter().zip(expected_output) { | ||
assert_eq!(anyhow, expected); | ||
} | ||
} | ||
} |
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,103 @@ | ||
#[cfg(nightly)] | ||
use core::any::Demand; | ||
use core::{fmt, panic::Location}; | ||
#[cfg(all(nightly, feature = "std"))] | ||
use std::{backtrace::Backtrace, error::Error}; | ||
|
||
use eyre::Report as EyreReport; | ||
|
||
use crate::{Context, Frame, IntoReportCompat, Report, Result}; | ||
|
||
#[repr(transparent)] | ||
struct EyreContext(EyreReport); | ||
|
||
impl fmt::Debug for EyreContext { | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Debug::fmt(&self.0, fmt) | ||
} | ||
} | ||
|
||
impl fmt::Display for EyreContext { | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Display::fmt(&self.0, fmt) | ||
} | ||
} | ||
|
||
impl Context for EyreContext { | ||
#[cfg(nightly)] | ||
fn provide<'a>(&'a self, demand: &mut Demand<'a>) { | ||
demand.provide_ref(&self.0); | ||
#[cfg(feature = "std")] | ||
if let Some(backtrace) = self.0.chain().find_map(Error::backtrace) { | ||
demand.provide_ref(backtrace); | ||
} | ||
} | ||
} | ||
|
||
impl<T> IntoReportCompat for core::result::Result<T, EyreReport> { | ||
type Err = EyreReport; | ||
type Ok = T; | ||
|
||
#[track_caller] | ||
fn into_report(self) -> Result<T, EyreReport> { | ||
match self { | ||
Ok(t) => Ok(t), | ||
Err(eyre) => { | ||
let sources = eyre | ||
.chain() | ||
.skip(1) | ||
.map(alloc::string::ToString::to_string) | ||
.collect::<alloc::vec::Vec<_>>(); | ||
|
||
#[cfg(all(nightly, feature = "std"))] | ||
let backtrace = eyre | ||
.chain() | ||
.all(|error| error.backtrace().is_none()) | ||
.then(Backtrace::capture); | ||
|
||
let mut report = Report::from_frame( | ||
Frame::from_compat::<EyreReport, EyreContext>( | ||
EyreContext(eyre), | ||
Location::caller(), | ||
), | ||
#[cfg(all(nightly, feature = "std"))] | ||
backtrace, | ||
#[cfg(feature = "spantrace")] | ||
Some(tracing_error::SpanTrace::capture()), | ||
); | ||
|
||
for source in sources { | ||
report = report.attach_printable(source); | ||
} | ||
|
||
Err(report) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use alloc::boxed::Box; | ||
|
||
use eyre::eyre; | ||
|
||
use crate::{test_helper::messages, IntoReportCompat}; | ||
|
||
#[test] | ||
#[cfg_attr( | ||
miri, | ||
ignore = "bug: miri is failing for `eyre`, this is unrelated to our implementation" | ||
)] | ||
fn conversion() { | ||
eyre::set_hook(Box::new(eyre::DefaultHandler::default_with)).expect("Could not set hook"); | ||
|
||
let eyre: Result<(), _> = Err(eyre!("A").wrap_err("B").wrap_err("C")); | ||
|
||
let report = eyre.into_report().unwrap_err(); | ||
let expected_output = ["A", "B", "C"]; | ||
for (eyre, expected) in messages(&report).into_iter().zip(expected_output) { | ||
assert_eq!(eyre, expected); | ||
} | ||
} | ||
} |
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,31 @@ | ||
#[cfg(feature = "anyhow")] | ||
mod anyhow; | ||
#[cfg(feature = "eyre")] | ||
mod eyre; | ||
|
||
/// Compatibility trait to convert from external libraries to [`Report`]. | ||
TimDiekmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// *Note*: It's not possible to implement [`IntoReport`] or [`Context`] on other error libraries' | ||
/// types as both traits have blanket implementation relying on [`Error`]. Thus, implementing either | ||
/// trait would violate the orphan rule; the upstream crate could implement [`Error`] and this would | ||
/// imply an implementation for [`IntoReport`]/[`Context`]. | ||
/// | ||
/// [`Report`]: crate::Report | ||
/// [`IntoReport`]: crate::IntoReport | ||
/// [`Context`]: crate::Context | ||
/// [`Error`]: std::error::Error | ||
#[cfg(any(feature = "anyhow", feature = "eyre"))] | ||
pub trait IntoReportCompat: Sized { | ||
/// Type of the [`Ok`] value in the [`Result`] | ||
type Ok; | ||
|
||
/// Type of the resulting [`Err`] variant wrapped inside a [`Report<E>`]. | ||
/// | ||
/// [`Report<E>`]: crate::Report | ||
type Err; | ||
|
||
/// Converts the [`Err`] variant of the [`Result`] to a [`Report`] | ||
/// | ||
/// [`Report`]: crate::Report | ||
fn into_report(self) -> crate::Result<Self::Ok, Self::Err>; | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed as part of #747