Skip to content

Commit

Permalink
refactor: refactor curp error
Browse files Browse the repository at this point in the history
Signed-off-by: iGxnon <[email protected]>
  • Loading branch information
iGxnon committed Dec 13, 2023
1 parent 7c25762 commit b93fc95
Show file tree
Hide file tree
Showing 10 changed files with 579 additions and 926 deletions.
2 changes: 1 addition & 1 deletion curp/proto/common
Submodule common updated 1 files
+84 −46 src/curp-command.proto
302 changes: 100 additions & 202 deletions curp/src/client.rs

Large diffs are not rendered by default.

34 changes: 0 additions & 34 deletions curp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ use std::io;
use curp_external_api::cmd::{Command, PbSerializeError};
use thiserror::Error;

use crate::rpc::ProposeError;

/// Since there are some different statuses with the same code, Xline using "error-label"
/// to tell apart them.
pub const ERROR_LABEL: &str = "error-label";

/// Error type of client builder
#[allow(clippy::module_name_repetitions)] // this-error generate code false-positive
#[derive(Debug, Error)]
Expand Down Expand Up @@ -63,34 +57,6 @@ pub enum ServerError {
RpcError(#[from] tonic::transport::Error),
}

impl std::fmt::Display for ProposeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Self::KeyConflict => write!(f, "ProposeError: Key Conflict"),
Self::Duplicated => write!(f, "ProposeError: Duplicated"),
}
}
}

impl std::error::Error for ProposeError {
fn description(&self) -> &str {
match *self {
Self::KeyConflict => "key conflict error",
Self::Duplicated => "duplicated, the cmd might have already been proposed",
}
}
}

impl From<i32> for ProposeError {
fn from(value: i32) -> Self {
match value {
0 => Self::KeyConflict,
1 => Self::Duplicated,
_ => unreachable!("Unknown ProposeError Type"),
}
}
}

/// The union error which includes propose errors and user-defined errors.
#[derive(Error, Debug)]
#[allow(clippy::module_name_repetitions)] // this-error generate code false-positive
Expand Down
52 changes: 26 additions & 26 deletions curp/src/rpc/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ use crate::{
commandpb::protocol_client::ProtocolClient,
inner_messagepb::inner_protocol_client::InnerProtocolClient,
},
AppendEntriesRequest, AppendEntriesResponse, FetchClusterRequest, FetchClusterResponse,
FetchReadStateRequest, FetchReadStateResponse, InstallSnapshotRequest,
InstallSnapshotResponse, ProposeConfChangeRequest, ProposeConfChangeResponse,
ProposeRequest, ProposeResponse, VoteRequest, VoteResponse, WaitSyncedRequest,
WaitSyncedResponse,
AppendEntriesRequest, AppendEntriesResponse, CurpError, FetchClusterRequest,
FetchClusterResponse, FetchReadStateRequest, FetchReadStateResponse,
InstallSnapshotRequest, InstallSnapshotResponse, ProposeConfChangeRequest,
ProposeConfChangeResponse, ProposeRequest, ProposeResponse, VoteRequest, VoteResponse,
WaitSyncedRequest, WaitSyncedResponse,
},
snapshot::Snapshot,
PublishRequest, PublishResponse, TriggerShutdownRequest,
Expand Down Expand Up @@ -154,49 +154,49 @@ pub(crate) trait ConnectApi: Send + Sync + 'static {
&self,
request: ProposeRequest,
timeout: Duration,
) -> Result<tonic::Response<ProposeResponse>, tonic::Status>;
) -> Result<tonic::Response<ProposeResponse>, CurpError>;

/// Send `ProposeRequest`
async fn propose_conf_change(
&self,
request: ProposeConfChangeRequest,
timeout: Duration,
) -> Result<tonic::Response<ProposeConfChangeResponse>, tonic::Status>;
) -> Result<tonic::Response<ProposeConfChangeResponse>, CurpError>;

/// Send `PublishRequest`
async fn publish(
&self,
request: PublishRequest,
timeout: Duration,
) -> Result<tonic::Response<PublishResponse>, tonic::Status>;
) -> Result<tonic::Response<PublishResponse>, CurpError>;

/// Send `WaitSyncedRequest`
async fn wait_synced(
&self,
request: WaitSyncedRequest,
timeout: Duration,
) -> Result<tonic::Response<WaitSyncedResponse>, tonic::Status>;
) -> Result<tonic::Response<WaitSyncedResponse>, CurpError>;

/// Send `ShutdownRequest`
async fn shutdown(
&self,
request: ShutdownRequest,
timeout: Duration,
) -> Result<tonic::Response<ShutdownResponse>, tonic::Status>;
) -> Result<tonic::Response<ShutdownResponse>, CurpError>;

/// Send `FetchClusterRequest`
async fn fetch_cluster(
&self,
request: FetchClusterRequest,
timeout: Duration,
) -> Result<tonic::Response<FetchClusterResponse>, tonic::Status>;
) -> Result<tonic::Response<FetchClusterResponse>, CurpError>;

/// Send `FetchReadStateRequest`
async fn fetch_read_state(
&self,
request: FetchReadStateRequest,
timeout: Duration,
) -> Result<tonic::Response<FetchReadStateResponse>, tonic::Status>;
) -> Result<tonic::Response<FetchReadStateResponse>, CurpError>;
}

/// Inner Connect interface among different servers
Expand Down Expand Up @@ -324,12 +324,12 @@ impl ConnectApi for Connect<ProtocolClient<Channel>> {
&self,
request: ProposeRequest,
timeout: Duration,
) -> Result<tonic::Response<ProposeResponse>, tonic::Status> {
) -> Result<tonic::Response<ProposeResponse>, CurpError> {
let mut client = self.rpc_connect.clone();
let mut req = tonic::Request::new(request);
req.set_timeout(timeout);
req.metadata_mut().inject_current();
client.propose(req).await
client.propose(req).await.map_err(Into::into)
}

/// Send `ShutdownRequest`
Expand All @@ -338,12 +338,12 @@ impl ConnectApi for Connect<ProtocolClient<Channel>> {
&self,
request: ShutdownRequest,
timeout: Duration,
) -> Result<tonic::Response<ShutdownResponse>, tonic::Status> {
) -> Result<tonic::Response<ShutdownResponse>, CurpError> {
let mut client = self.rpc_connect.clone();
let mut req = tonic::Request::new(request);
req.set_timeout(timeout);
req.metadata_mut().inject_current();
client.shutdown(req).await
client.shutdown(req).await.map_err(Into::into)
}

/// Send `ProposeRequest`
Expand All @@ -352,12 +352,12 @@ impl ConnectApi for Connect<ProtocolClient<Channel>> {
&self,
request: ProposeConfChangeRequest,
timeout: Duration,
) -> Result<tonic::Response<ProposeConfChangeResponse>, tonic::Status> {
) -> Result<tonic::Response<ProposeConfChangeResponse>, CurpError> {
let mut client = self.rpc_connect.clone();
let mut req = tonic::Request::new(request);
req.set_timeout(timeout);
req.metadata_mut().inject_current();
client.propose_conf_change(req).await
client.propose_conf_change(req).await.map_err(Into::into)
}

/// Send `PublishRequest`
Expand All @@ -366,12 +366,12 @@ impl ConnectApi for Connect<ProtocolClient<Channel>> {
&self,
request: PublishRequest,
timeout: Duration,
) -> Result<tonic::Response<PublishResponse>, tonic::Status> {
) -> Result<tonic::Response<PublishResponse>, CurpError> {
let mut client = self.rpc_connect.clone();
let mut req = tonic::Request::new(request);
req.set_timeout(timeout);
req.metadata_mut().inject_current();
client.publish(req).await
client.publish(req).await.map_err(Into::into)
}

/// Send `WaitSyncedRequest`
Expand All @@ -380,36 +380,36 @@ impl ConnectApi for Connect<ProtocolClient<Channel>> {
&self,
request: WaitSyncedRequest,
timeout: Duration,
) -> Result<tonic::Response<WaitSyncedResponse>, tonic::Status> {
) -> Result<tonic::Response<WaitSyncedResponse>, CurpError> {
let mut client = self.rpc_connect.clone();
let mut req = tonic::Request::new(request);
req.set_timeout(timeout);
req.metadata_mut().inject_current();
client.wait_synced(req).await
client.wait_synced(req).await.map_err(Into::into)
}

/// Send `FetchClusterRequest`
async fn fetch_cluster(
&self,
request: FetchClusterRequest,
timeout: Duration,
) -> Result<tonic::Response<FetchClusterResponse>, tonic::Status> {
) -> Result<tonic::Response<FetchClusterResponse>, CurpError> {
let mut client = self.rpc_connect.clone();
let mut req = tonic::Request::new(request);
req.set_timeout(timeout);
client.fetch_cluster(req).await
client.fetch_cluster(req).await.map_err(Into::into)
}

/// Send `FetchReadStateRequest`
async fn fetch_read_state(
&self,
request: FetchReadStateRequest,
timeout: Duration,
) -> Result<tonic::Response<FetchReadStateResponse>, tonic::Status> {
) -> Result<tonic::Response<FetchReadStateResponse>, CurpError> {
let mut client = self.rpc_connect.clone();
let mut req = tonic::Request::new(request);
req.set_timeout(timeout);
client.fetch_read_state(req).await
client.fetch_read_state(req).await.map_err(Into::into)
}
}

Expand Down
Loading

0 comments on commit b93fc95

Please sign in to comment.