Skip to content
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

chore(sequencer): migrate from anyhow::Result to eyre::Result #1387

Merged
merged 19 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions crates/astria-eyre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ edition = "2021"
[dependencies]
eyre = "0.6"
itoa = "1.0.10"
anyhow = { version = "1", optional = true }
anyhow = { version = "1.0.0", optional = true }

[dev-dependencies]
tracing = { workspace = true }
tokio-test = { workspace = true }
tracing-subscriber = "0.3.18"

[features]
anyhow_support = ["anyhow"]
72 changes: 38 additions & 34 deletions crates/astria-eyre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use std::{
fmt::Write as _,
};

#[cfg(feature = "anyhow_support")]
pub use anyhow;
#[cfg(feature = "anyhow")]
pub use anyhow_conversion::{
anyhow_to_eyre,
eyre_to_anyhow,
};
pub use eyre;
#[doc(hidden)]
pub use eyre::Result;
Expand Down Expand Up @@ -86,43 +89,44 @@ fn write_value(err: &dyn Error, f: &mut core::fmt::Formatter<'_>) -> core::fmt::
Ok(())
}

#[cfg(feature = "anyhow_support")]
pub fn anyhow_to_eyre(anyhow_error: anyhow::Error) -> eyre::Report {
let boxed: Box<dyn std::error::Error + Send + Sync> = anyhow_error.into();
eyre::eyre!(boxed)
}
#[cfg(feature = "anyhow")]
pub mod anyhow_conversion {
pub use anyhow;

#[must_use]
#[cfg(feature = "anyhow_support")]
pub fn eyre_to_anyhow(eyre_error: eyre::Report) -> anyhow::Error {
let boxed: Box<dyn std::error::Error + Send + Sync> = eyre_error.into();
anyhow::anyhow!(boxed)
}
pub fn anyhow_to_eyre(anyhow_error: anyhow::Error) -> eyre::Report {
let boxed: Box<dyn std::error::Error + Send + Sync> = anyhow_error.into();
eyre::eyre!(boxed)
}

mod test {
#[test]
#[cfg(feature = "anyhow_support")]
fn anyhow_to_eyre_preserves_source_chain() {
let mut errs = ["foo", "bar", "baz", "qux"];
let anyhow_error = anyhow::anyhow!(errs[0]).context(errs[1]).context(errs[2]);
let eyre_from_anyhow = crate::anyhow_to_eyre(anyhow_error).wrap_err(errs[3]);

errs.reverse();
for (i, err) in eyre_from_anyhow.chain().enumerate() {
assert_eq!(errs[i], &err.to_string());
}
#[must_use]
pub fn eyre_to_anyhow(eyre_error: eyre::Report) -> anyhow::Error {
let boxed: Box<dyn std::error::Error + Send + Sync> = eyre_error.into();
anyhow::anyhow!(boxed)
}

#[test]
#[cfg(feature = "anyhow_support")]
fn eyre_to_anyhow_preserves_source_chain() {
let mut errs = ["foo", "bar", "baz", "qux"];
let eyre_error = eyre::eyre!(errs[0]).wrap_err(errs[1]).wrap_err(errs[2]);
let anyhow_from_eyre = crate::eyre_to_anyhow(eyre_error).context(errs[3]);
mod test {
#[test]
fn anyhow_to_eyre_preserves_source_chain() {
let mut errs = ["foo", "bar", "baz", "qux"];
let anyhow_error = anyhow::anyhow!(errs[0]).context(errs[1]).context(errs[2]);
let eyre_from_anyhow = super::anyhow_to_eyre(anyhow_error).wrap_err(errs[3]);

errs.reverse();
for (i, err) in eyre_from_anyhow.chain().enumerate() {
assert_eq!(errs[i], &err.to_string());
}
}

#[test]
fn eyre_to_anyhow_preserves_source_chain() {
let mut errs = ["foo", "bar", "baz", "qux"];
let eyre_error = eyre::eyre!(errs[0]).wrap_err(errs[1]).wrap_err(errs[2]);
let anyhow_from_eyre = super::eyre_to_anyhow(eyre_error).context(errs[3]);

errs.reverse();
for (i, err) in anyhow_from_eyre.chain().enumerate() {
assert_eq!(errs[i], &err.to_string());
errs.reverse();
for (i, err) in anyhow_from_eyre.chain().enumerate() {
assert_eq!(errs[i], &err.to_string());
}
}
}
}
6 changes: 5 additions & 1 deletion crates/astria-sequencer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ benchmark = ["divan"]
[dependencies]
astria-core = { path = "../astria-core", features = ["server", "serde"] }
astria-build-info = { path = "../astria-build-info", features = ["runtime"] }
astria-eyre = { path = "../astria-eyre", features = ["anyhow_support"] }

# The "anyhow" feature is only included because it is necessary for the implementation of
# `penumbra_ibc::component::HostInterface` in `crates/astria-sequencer/src/ibc/host_interface.rs`.
# Avoid using "anyhow" results anywhere else.
astria-eyre = { path = "../astria-eyre", features = ["anyhow"] }

config = { package = "astria-config", path = "../astria-config" }
merkle = { package = "astria-merkle", path = "../astria-merkle" }
Expand Down
2 changes: 1 addition & 1 deletion crates/astria-sequencer/src/accounts/state_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pin_project! {

impl<St> Stream for AccountAssetBalancesStream<St>
where
St: Stream<Item = astria_eyre::anyhow::Result<(String, Vec<u8>)>>,
St: Stream<Item = astria_eyre::anyhow_conversion::anyhow::Result<(String, Vec<u8>)>>,
{
type Item = Result<AssetBalance>;

Expand Down
2 changes: 1 addition & 1 deletion crates/astria-sequencer/src/ibc/host_interface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use astria_eyre::{
anyhow,
anyhow_conversion::anyhow,
eyre_to_anyhow,
};
use cnidarium::StateRead;
Expand Down
2 changes: 1 addition & 1 deletion crates/astria-sequencer/src/ibc/ics20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use astria_core::{
sequencerblock::v1alpha1::block::Deposit,
};
use astria_eyre::{
anyhow::{
anyhow_conversion::anyhow::{
self,
Context as _,
},
Expand Down
Loading