diff --git a/Cargo.lock b/Cargo.lock index 4e871ffa4..bf6503eb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2659,6 +2659,7 @@ dependencies = [ "serde", "tangle-subxt", "tempfile", + "thiserror 2.0.11", "tokio", ] diff --git a/crates/sdk/Cargo.toml b/crates/sdk/Cargo.toml index 8a95c85ce..de7ddb274 100644 --- a/crates/sdk/Cargo.toml +++ b/crates/sdk/Cargo.toml @@ -25,10 +25,13 @@ gadget-stores = { workspace = true, optional = true } tangle-subxt = { workspace = true, optional = true } # EigenLayer and EVM -alloy = { workspace = true, optional = true } +alloy = { workspace = true, optional = true, features = ["full"] } alloy-json-abi = { workspace = true, optional = true, features = ["serde_json"] } eigensdk = { workspace = true, optional = true, features = ["full"] } +# Error Handling +thiserror = { workspace = true } + # Serialization serde = { workspace = true, features = ["derive"] } @@ -89,6 +92,7 @@ tangle = [ "gadget-macros?/tangle", "gadget-testing-utils?/tangle", "gadget-utils/tangle", + "gadget-event-listeners/tangle" ] evm = [ @@ -97,6 +101,7 @@ evm = [ "alloy", "alloy-json-abi", "gadget-macros?/evm", + "gadget-event-listeners/evm" ] eigenlayer = [ @@ -107,6 +112,7 @@ eigenlayer = [ "alloy", "alloy-json-abi", "eigensdk", + "gadget-event-listeners/evm" ] testing = [ diff --git a/crates/sdk/src/error.rs b/crates/sdk/src/error.rs new file mode 100644 index 000000000..7be092c97 --- /dev/null +++ b/crates/sdk/src/error.rs @@ -0,0 +1,75 @@ +use super::{config, keystore}; + +#[cfg(any(feature = "evm", feature = "eigenlayer", feature = "tangle"))] +use super::event_listeners; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + // General Errors + #[error("Config error: {0}")] + Config(#[from] config::Error), + #[error("Keystore error: {0}")] + Keystore(#[from] keystore::Error), + #[error("Other Error: {0}")] + Other(String), + + // Specific to Tangle + #[cfg(feature = "tangle")] + #[error("Event listener error: {0}")] + TangleEvent( + #[from] + event_listeners::core::Error, + ), + #[cfg(feature = "tangle")] + #[error("Tangle Subxt error: {0}")] + TangleSubxt(#[from] tangle_subxt::subxt::Error), + + // EVM and EigenLayer + #[cfg(any(feature = "evm", feature = "eigenlayer"))] + #[error("Event listener error: {0}")] + EvmEvent(#[from] event_listeners::core::Error), + #[cfg(any(feature = "evm", feature = "eigenlayer"))] + #[error("EVM error: {0}")] + Alloy(#[from] AlloyError), + #[cfg(feature = "eigenlayer")] + #[error("Eigenlayer error: {0}")] + Eigenlayer(#[from] eigensdk::types::avs::SignatureVerificationError), + + // Specific to Networking + #[cfg(feature = "networking")] + #[error("Networking error: {0}")] + Networking(#[from] gadget_networking::Error), +} + +#[cfg(any(feature = "evm", feature = "eigenlayer"))] +#[derive(thiserror::Error, Debug)] +pub enum AlloyError { + #[error("Alloy signer error: {0}")] + Signer(#[from] alloy::signers::Error), + #[error("Alloy contract error: {0}")] + Contract(#[from] alloy::contract::Error), + #[error("Alloy transaction error: {0}")] + Conversion(#[from] alloy::rpc::types::transaction::ConversionError), + #[error("Alloy local signer error: {0}")] + LocalSigner(#[from] alloy::signers::local::LocalSignerError), +} + +#[cfg(any(feature = "evm", feature = "eigenlayer"))] +macro_rules! implement_from_alloy_error { + ($($path:ident)::+, $variant:ident) => { + impl From for Error { + fn from(value: alloy::$($path)::+) -> Self { + Error::Alloy(AlloyError::$variant(value)) + } + } + }; +} + +#[cfg(any(feature = "evm", feature = "eigenlayer"))] +implement_from_alloy_error!(signers::Error, Signer); +#[cfg(any(feature = "evm", feature = "eigenlayer"))] +implement_from_alloy_error!(contract::Error, Contract); +#[cfg(any(feature = "evm", feature = "eigenlayer"))] +implement_from_alloy_error!(rpc::types::transaction::ConversionError, Conversion); +#[cfg(any(feature = "evm", feature = "eigenlayer"))] +implement_from_alloy_error!(signers::local::LocalSignerError, LocalSigner); diff --git a/crates/sdk/src/lib.rs b/crates/sdk/src/lib.rs index 6c5ae24e0..914f95186 100644 --- a/crates/sdk/src/lib.rs +++ b/crates/sdk/src/lib.rs @@ -66,5 +66,9 @@ pub use gadget_std as std; pub use serde; pub use tokio; +/// Error +pub mod error; +pub use error::Error; + #[cfg(feature = "local-store")] pub use gadget_stores as stores;