Skip to content

Commit 00f36c7

Browse files
committed
feat(sdk): consensus error 2
1 parent e0e152f commit 00f36c7

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/rs-dapi-client/src/transport.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub trait TransportRequest: Clone + Send + Sync + Debug + Mockable {
4848
/// Generic way to create a transport client from provided [Uri].
4949
pub trait TransportClient: Send + Sized {
5050
/// Error type for the specific client.
51-
type Error: CanRetry + Send + Debug + Mockable;
51+
type Error: CanRetry + Send + Debug + Mockable + 'static;
5252

5353
/// Build client using node's url.
5454
fn with_uri(uri: Uri, pool: &ConnectionPool) -> Result<Self, Self::Error>;

packages/rs-sdk/src/error.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
//! Definitions of errors
2+
3+
use std::any::Any;
24
use std::fmt::Debug;
35
use std::time::Duration;
46

57
use dapi_grpc::mock::Mockable;
8+
use dpp::consensus::ConsensusError;
9+
use dpp::serialization::PlatformDeserializable;
610
use dpp::version::PlatformVersionError;
711
use dpp::ProtocolError;
812
use rs_dapi_client::{CanRetry, DapiClientError};
@@ -73,8 +77,24 @@ pub enum Error {
7377
StaleNode(#[from] StaleNodeError),
7478
}
7579

76-
impl<T: Debug + Mockable> From<DapiClientError<T>> for Error {
80+
impl<T: Debug + Mockable + Any> From<DapiClientError<T>> for Error {
7781
fn from(value: DapiClientError<T>) -> Self {
82+
if let DapiClientError::Transport(ref t, _) = &value {
83+
if let Some(status) = (t as &dyn Any).downcast_ref::<dapi_grpc::tonic::Status>() {
84+
if let Some(consensus_error_value) =
85+
status.metadata().get_bin("drive-error-data-bin")
86+
{
87+
return ConsensusError::deserialize_from_bytes(
88+
consensus_error_value.as_encoded_bytes(),
89+
)
90+
.map(|consensus_error| {
91+
Error::Protocol(ProtocolError::ConsensusError(Box::new(consensus_error)))
92+
})
93+
.unwrap_or_else(Error::Protocol);
94+
}
95+
}
96+
}
97+
7898
Self::DapiClientError(format!("{:?}", value))
7999
}
80100
}

0 commit comments

Comments
 (0)