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

feat(sdk): consensus error 2 #2275

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/rs-dapi-client/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub trait TransportRequest: Clone + Send + Sync + Debug + Mockable {
/// Generic way to create a transport client from provided [Uri].
pub trait TransportClient: Send + Sized {
/// Error type for the specific client.
type Error: CanRetry + Send + Debug + Mockable;
type Error: CanRetry + Send + Debug + Mockable + 'static;

/// Build client using node's url.
fn with_uri(uri: Uri, pool: &ConnectionPool) -> Result<Self, Self::Error>;
Expand Down
22 changes: 21 additions & 1 deletion packages/rs-sdk/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! Definitions of errors

use std::any::Any;
use std::fmt::Debug;
use std::time::Duration;

use dapi_grpc::mock::Mockable;
use dpp::consensus::ConsensusError;
use dpp::serialization::PlatformDeserializable;
use dpp::version::PlatformVersionError;
use dpp::ProtocolError;
use rs_dapi_client::{CanRetry, DapiClientError};
Expand Down Expand Up @@ -73,8 +77,24 @@ pub enum Error {
StaleNode(#[from] StaleNodeError),
}

impl<T: Debug + Mockable> From<DapiClientError<T>> for Error {
impl<T: Debug + Mockable + Any> From<DapiClientError<T>> for Error {
fn from(value: DapiClientError<T>) -> Self {
if let DapiClientError::Transport(ref t, _) = &value {
if let Some(status) = (t as &dyn Any).downcast_ref::<dapi_grpc::tonic::Status>() {
if let Some(consensus_error_value) =
status.metadata().get_bin("drive-error-data-bin")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently cbor is inside. We should do it without cbor

{
return ConsensusError::deserialize_from_bytes(
consensus_error_value.as_encoded_bytes(),
)
.map(|consensus_error| {
Error::Protocol(ProtocolError::ConsensusError(Box::new(consensus_error)))
})
.unwrap_or_else(Error::Protocol);
}
Comment on lines +87 to +94
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider enhancing error handling granularity

The current implementation silently falls back to a generic Protocol error if deserialization fails. Consider providing more specific error information in failure cases.

-    return ConsensusError::deserialize_from_bytes(
-        consensus_error_value.as_encoded_bytes(),
-    )
-    .map(|consensus_error| {
-        Error::Protocol(ProtocolError::ConsensusError(Box::new(consensus_error)))
-    })
-    .unwrap_or_else(Error::Protocol);
+    return match ConsensusError::deserialize_from_bytes(
+        consensus_error_value.as_encoded_bytes(),
+    ) {
+        Ok(consensus_error) => {
+            Error::Protocol(ProtocolError::ConsensusError(Box::new(consensus_error)))
+        }
+        Err(e) => Error::Protocol(ProtocolError::Generic(format!(
+            "Failed to deserialize consensus error: {}",
+            e
+        ))),
+    };

Committable suggestion was skipped due to low confidence.

}
}

Self::DapiClientError(format!("{:?}", value))
}
}
Expand Down
Loading