Skip to content

Commit

Permalink
feat(macros): use sdk::Error instead of Box<dyn Error>
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Jan 24, 2025
1 parent 81ebf16 commit 781ffb1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
5 changes: 5 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ pub enum Error {
/// Error parsing the protocol, from the `PROTOCOL` environment variable.
#[error("Unsupported protocol: {0}")]
UnsupportedProtocol(String),
/// Attempting to load the [`ProtocolSettings`] of a protocol differing from the target
///
/// [`ProtocolSettings`]: crate::ProtocolSettings
#[error("Unexpect protocol, expected {0}")]
UnexpectedProtocol(&'static str),
/// No Sr25519 keypair found in the keystore.
#[error("No Sr25519 keypair found in the keystore")]
NoSr25519Keypair,
Expand Down
12 changes: 6 additions & 6 deletions crates/config/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,26 @@ impl ProtocolSettings {
}

#[cfg(feature = "tangle")]
pub fn tangle(&self) -> Result<&TangleInstanceSettings, &'static str> {
pub fn tangle(&self) -> Result<&TangleInstanceSettings, Error> {
match self {
Self::Tangle(settings) => Ok(settings),
_ => Err("Not a Tangle instance"),
_ => Err(Error::UnexpectedProtocol("Tangle")),
}
}

#[cfg(feature = "eigenlayer")]
pub fn eigenlayer(&self) -> Result<&EigenlayerContractAddresses, &'static str> {
pub fn eigenlayer(&self) -> Result<&EigenlayerContractAddresses, Error> {
match self {
Self::Eigenlayer(settings) => Ok(settings),
_ => Err("Not an Eigenlayer instance"),
_ => Err(Error::UnexpectedProtocol("Eigenlayer")),
}
}

#[cfg(feature = "symbiotic")]
pub fn symbiotic(&self) -> Result<&SymbioticContractAddresses, &'static str> {
pub fn symbiotic(&self) -> Result<&SymbioticContractAddresses, Error> {
match self {
Self::Symbiotic(settings) => Ok(settings),
_ => Err("Not a Symbiotic instance"),
_ => Err(Error::UnexpectedProtocol("Symbiotic")),
}
}

Expand Down
13 changes: 5 additions & 8 deletions crates/macros/blueprint-proc-macro/src/job/tangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ pub(crate) fn generate_tangle_specific_impl(
let struct_name_as_literal = struct_name.to_string();

Ok(quote! {
// TODO: No Box<dyn Error>
impl #struct_name {
/// Create a new
#[doc = "[`"]
Expand All @@ -61,30 +60,28 @@ pub(crate) fn generate_tangle_specific_impl(
/// - The client fails to connect
/// - The signer is not found
/// - The service ID is not found.
pub async fn new(#(#new_function_signature)*) -> Result<Self, Box<dyn core::error::Error>> {
pub async fn new(#(#new_function_signature)*) -> Result<Self, ::blueprint_sdk::error::Error> {
use ::blueprint_sdk::macros::ext::keystore::backends::tangle::TangleBackend as _;
use ::blueprint_sdk::macros::ext::keystore::backends::Backend as _;

let client =
<#env_type as ::blueprint_sdk::macros::ext::contexts::tangle::TangleClientContext>::tangle_client(env)
.await
.map_err(|e| Into::<Box<dyn core::error::Error>>::into(e))?;
.await?;

// TODO: Key IDs
let keystore = <#env_type as ::blueprint_sdk::macros::ext::contexts::keystore::KeystoreContext>::keystore(env);
let public = keystore.first_local::<
::blueprint_sdk::macros::ext::crypto::sp_core::SpSr25519
>().map_err(|_| Into::<Box<dyn core::error::Error>>::into(::blueprint_sdk::macros::ext::config::Error::NoSr25519Keypair))?;
>().map_err(|_| ::blueprint_sdk::macros::ext::config::Error::NoSr25519Keypair)?;
let pair = keystore.get_secret::<
::blueprint_sdk::macros::ext::crypto::sp_core::SpSr25519
>(&public)?;
let signer = ::blueprint_sdk::macros::ext::crypto::tangle_pair_signer::TanglePairSigner::new(pair.0);

let service_id = env.protocol_settings
.tangle()
.map_err(|e| Into::<Box<dyn core::error::Error>>::into(e))?
.tangle()?
.service_id
.ok_or_else(|| Into::<Box<dyn core::error::Error>>::into(::blueprint_sdk::macros::ext::config::Error::MissingServiceId))?;
.ok_or_else(|| ::blueprint_sdk::macros::ext::config::Error::MissingServiceId)?;

Ok(Self {
#(#constructor_args)*
Expand Down
2 changes: 2 additions & 0 deletions crates/sdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub enum Error {
// General Errors
#[error("Config error: {0}")]
Config(#[from] config::Error),
#[error("Client error: {0}")]
Client(#[from] gadget_clients::Error),
#[error("Keystore error: {0}")]
Keystore(#[from] keystore::Error),
#[error("Other Error: {0}")]
Expand Down

0 comments on commit 781ffb1

Please sign in to comment.