Skip to content

Commit

Permalink
feat(sdk): consensus error 2
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Oct 24, 2024
1 parent e0e152f commit 00f36c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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")
{
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);
}
}
}

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

0 comments on commit 00f36c7

Please sign in to comment.