From e1e307c868491e81ca70717aae12f2e50a3e2c32 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Thu, 25 Jul 2024 15:48:35 +0200 Subject: [PATCH] chore(iota-sdk): Error cleanup (#1378) --- crates/iota-replay/src/types.rs | 2 +- crates/iota-sdk/examples/json_rpc_errors.rs | 4 ++-- crates/iota-sdk/src/apis.rs | 4 ++-- crates/iota-sdk/src/error.rs | 15 ++++----------- crates/iota-sdk/src/lib.rs | 6 ++---- crates/iota-source-validation/src/lib.rs | 2 +- 6 files changed, 12 insertions(+), 21 deletions(-) diff --git a/crates/iota-replay/src/types.rs b/crates/iota-replay/src/types.rs index 8f5e89c4d5f..d7288783e69 100644 --- a/crates/iota-replay/src/types.rs +++ b/crates/iota-replay/src/types.rs @@ -230,7 +230,7 @@ impl From for ReplayEngineError { impl From for ReplayEngineError { fn from(err: IotaRpcError) -> Self { match err { - IotaRpcError::RpcError(JsonRpseeError::RequestTimeout) => { + IotaRpcError::Rpc(JsonRpseeError::RequestTimeout) => { ReplayEngineError::IotaRpcRequestTimeout } _ => ReplayEngineError::IotaRpcError { diff --git a/crates/iota-sdk/examples/json_rpc_errors.rs b/crates/iota-sdk/examples/json_rpc_errors.rs index 314db0362bd..722debb1d72 100644 --- a/crates/iota-sdk/examples/json_rpc_errors.rs +++ b/crates/iota-sdk/examples/json_rpc_errors.rs @@ -16,13 +16,13 @@ async fn main() -> Result<(), anyhow::Error> { .get_coins(active_address, coin_type.clone(), None, Some(5)) .await; let error = coins.unwrap_err(); - if let Error::RpcError(rpc_error) = error { + if let Error::Rpc(rpc_error) = error { let converted: JsonRpcError = rpc_error.into(); println!(" *** RpcError ***"); println!("{converted}"); println!("{}", converted.is_client_error()); } else { - bail!("Expected Error::RpcError, got {:?}", error); + bail!("Expected Error::Rpc, got {:?}", error); } Ok(()) } diff --git a/crates/iota-sdk/src/apis.rs b/crates/iota-sdk/src/apis.rs index 0eb21059537..1341e666033 100644 --- a/crates/iota-sdk/src/apis.rs +++ b/crates/iota-sdk/src/apis.rs @@ -448,11 +448,11 @@ impl ReadApi { .await? .into_object() .map_err(|e| { - Error::DataError(format!("Can't get bcs of object {:?}: {:?}", object_id, e)) + Error::Data(format!("Can't get bcs of object {:?}: {:?}", object_id, e)) })?; // unwrap: requested bcs data let move_object = resp.bcs.unwrap(); - let raw_move_obj = move_object.try_into_move().ok_or(Error::DataError(format!( + let raw_move_obj = move_object.try_into_move().ok_or(Error::Data(format!( "Object {:?} is not a MoveObject", object_id )))?; diff --git a/crates/iota-sdk/src/error.rs b/crates/iota-sdk/src/error.rs index a2f2ca3ed47..f2aabf86550 100644 --- a/crates/iota-sdk/src/error.rs +++ b/crates/iota-sdk/src/error.rs @@ -2,10 +2,7 @@ // Modifications Copyright (c) 2024 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -use iota_types::{ - base_types::{IotaAddress, TransactionDigest}, - error::UserInputError, -}; +use iota_types::base_types::{IotaAddress, TransactionDigest}; use thiserror::Error; pub use crate::json_rpc_error::Error as JsonRpcError; @@ -15,19 +12,15 @@ pub type IotaRpcResult = Result; #[derive(Error, Debug)] pub enum Error { #[error(transparent)] - RpcError(#[from] jsonrpsee::core::Error), + Rpc(#[from] jsonrpsee::core::Error), #[error(transparent)] - JsonRpcError(JsonRpcError), - #[error(transparent)] - BcsSerialisationError(#[from] bcs::Error), - #[error(transparent)] - UserInputError(#[from] UserInputError), + BcsSerialization(#[from] bcs::Error), #[error("Subscription error : {0}")] Subscription(String), #[error("Failed to confirm tx status for {0:?} within {1} seconds.")] FailToConfirmTransactionStatus(TransactionDigest, u64), #[error("Data error: {0}")] - DataError(String), + Data(String), #[error( "Client/Server api version mismatch, client api version : {client_version}, server api version : {server_version}" )] diff --git a/crates/iota-sdk/src/lib.rs b/crates/iota-sdk/src/lib.rs index 61e7570fa13..202581ff8eb 100644 --- a/crates/iota-sdk/src/lib.rs +++ b/crates/iota-sdk/src/lib.rs @@ -340,7 +340,7 @@ impl IotaClientBuilder { .pointer("/info/version") .and_then(|v| v.as_str()) .ok_or_else(|| { - Error::DataError("Fail parsing server version from rpc.discover endpoint.".into()) + Error::Data("Fail parsing server version from rpc.discover endpoint.".into()) })?; let rpc_methods = Self::parse_methods(&rpc_spec)?; @@ -362,9 +362,7 @@ impl IotaClientBuilder { .pointer("/methods") .and_then(|methods| methods.as_array()) .ok_or_else(|| { - Error::DataError( - "Fail parsing server information from rpc.discover endpoint.".into(), - ) + Error::Data("Fail parsing server information from rpc.discover endpoint.".into()) })?; Ok(methods diff --git a/crates/iota-source-validation/src/lib.rs b/crates/iota-source-validation/src/lib.rs index f94e8f8c8f1..3786be4b00e 100644 --- a/crates/iota-source-validation/src/lib.rs +++ b/crates/iota-source-validation/src/lib.rs @@ -304,7 +304,7 @@ impl<'a> BytecodeSourceVerifier<'a> { .map_err(SourceVerificationError::IotaObjectRefFailure)? .bcs .ok_or_else(|| { - SourceVerificationError::DependencyObjectReadFailure(Error::DataError( + SourceVerificationError::DependencyObjectReadFailure(Error::Data( "Bcs field is not found".to_string(), )) })?;