Skip to content

Commit

Permalink
chore: add a represantation of unxepected enum in client error (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
uriel-starkware authored Jul 29, 2024
1 parent 254c436 commit cbe5fb5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
4 changes: 2 additions & 2 deletions crates/mempool_infra/src/component_client/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub enum ClientError {
ResponseParsingFailure(HyperError),
#[error("Got status code: {0}, with server error: {1}")]
ResponseError(StatusCode, ServerError),
#[error("Got an unexpected response type.")]
UnexpectedResponse,
#[error("Got an unexpected response type: {0}")]
UnexpectedResponse(String),
}

pub type ClientResult<T> = Result<T, ClientError>;
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ impl ComponentBClientTrait for RemoteComponentClient<ComponentBRequest, Componen
async fn b_get_value(&self) -> ResultB {
match self.send(ComponentBRequest::BGetValue).await? {
ComponentBResponse::BGetValue(value) => Ok(value),
_ => Err(ClientError::UnexpectedResponse),
unexpected_response => {
Err(ClientError::UnexpectedResponse(format!("{unexpected_response:?}")))
}
}
}

async fn b_set_value(&self, value: ValueB) -> ClientResult<()> {
match self.send(ComponentBRequest::BSetValue(value)).await? {
ComponentBResponse::BSetValue => Ok(()),
_ => Err(ClientError::UnexpectedResponse),
unexpected_response => {
Err(ClientError::UnexpectedResponse(format!("{unexpected_response:?}")))
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/mempool_infra/tests/component_server_client_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ impl ComponentBClientTrait for LocalComponentClient<ComponentBRequest, Component
let res = self.send(ComponentBRequest::BGetValue).await;
match res {
ComponentBResponse::BGetValue(value) => Ok(value),
_ => Err(ClientError::UnexpectedResponse),
unexpected_response => {
Err(ClientError::UnexpectedResponse(format!("{unexpected_response:?}")))
}
}
}

async fn b_set_value(&self, value: ValueB) -> ClientResult<()> {
match self.send(ComponentBRequest::BSetValue(value)).await {
ComponentBResponse::BSetValue => Ok(()),
_ => Err(ClientError::UnexpectedResponse),
unexpected_response => {
Err(ClientError::UnexpectedResponse(format!("{unexpected_response:?}")))
}
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions crates/mempool_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ impl MempoolClient for MempoolClientImpl {
MempoolResponse::AddTransaction(Err(response)) => {
Err(MempoolClientError::MempoolError(response))
}
_ => Err(MempoolClientError::ClientError(ClientError::UnexpectedResponse)),
unexpected_response => Err(MempoolClientError::ClientError(
ClientError::UnexpectedResponse(format!("{unexpected_response:?}")),
)),
}
}

Expand All @@ -74,7 +76,9 @@ impl MempoolClient for MempoolClientImpl {
MempoolResponse::GetTransactions(Err(response)) => {
Err(MempoolClientError::MempoolError(response))
}
_ => Err(MempoolClientError::ClientError(ClientError::UnexpectedResponse)),
unexpected_response => Err(MempoolClientError::ClientError(
ClientError::UnexpectedResponse(format!("{unexpected_response:?}")),
)),
}
}
}
Expand All @@ -89,7 +93,9 @@ impl MempoolClient for RemoteMempoolClientImpl {
MempoolResponse::AddTransaction(Err(response)) => {
Err(MempoolClientError::MempoolError(response))
}
_ => Err(MempoolClientError::ClientError(ClientError::UnexpectedResponse)),
unexpected_response => Err(MempoolClientError::ClientError(
ClientError::UnexpectedResponse(format!("{unexpected_response:?}")),
)),
}
}

Expand All @@ -101,7 +107,9 @@ impl MempoolClient for RemoteMempoolClientImpl {
MempoolResponse::GetTransactions(Err(response)) => {
Err(MempoolClientError::MempoolError(response))
}
_ => Err(MempoolClientError::ClientError(ClientError::UnexpectedResponse)),
unexpected_response => Err(MempoolClientError::ClientError(
ClientError::UnexpectedResponse(format!("{unexpected_response:?}")),
)),
}
}
}

0 comments on commit cbe5fb5

Please sign in to comment.