Skip to content

Commit

Permalink
refactor: using a constant to replace string literal
Browse files Browse the repository at this point in the history
Signed-off-by: Phoeniix Zhao <[email protected]>
  • Loading branch information
Phoenix500526 committed Oct 23, 2023
1 parent 4808d96 commit 0f3f494
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions curp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use utils::config::ClientConfig;

use crate::{
cmd::{Command, ProposeId},
error::{ClientBuildError, ClientError},
error::{ClientBuildError, ClientError, ERROR_LABEL},
members::ServerId,
rpc::{
self, connect::ConnectApi, protocol_client::ProtocolClient, ConfChangeError,
Expand Down Expand Up @@ -239,7 +239,7 @@ enum UnpackStatus {
/// unpack `tonic::Status` and convert it to `UnpackStatus`
fn unpack_status(status: &tonic::Status) -> UnpackStatus {
let meta = status.metadata();
if let Some(label) = meta.get("error-label") {
if let Some(label) = meta.get(ERROR_LABEL) {
match label.to_str().unwrap_or_else(|err| {
unreachable!("error-label should be always able to convert to str: {err:?}")
}) {
Expand Down
4 changes: 4 additions & 0 deletions curp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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
37 changes: 26 additions & 11 deletions curp/src/server/curp_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use super::{
};
use crate::{
cmd::{Command, CommandExecutor},
error::ERROR_LABEL,
log_entry::LogEntry,
members::{ClusterInfo, ServerId},
role_change::RoleChange,
Expand Down Expand Up @@ -92,7 +93,7 @@ impl From<PbSerializeError> for CurpError {
fn gen_metadata(label: &str) -> MetadataMap {
let mut meta = MetadataMap::new();
_ = meta.insert(
"error-label",
ERROR_LABEL,
label.parse().unwrap_or_else(|e| {
unreachable!("convert a empty string to MetadataValue should always success: {e}")
}),
Expand Down Expand Up @@ -170,11 +171,18 @@ impl From<Status> for SendAEError {
fn from(status: Status) -> Self {
#[allow(clippy::wildcard_enum_match_arm)]
// it's ok to do so since only three status can covert to `SendAEError`
match status.code() {
tonic::Code::Cancelled => Self::EncodeDecode(status.message().to_owned()),
tonic::Code::FailedPrecondition => Self::NotLeader,
tonic::Code::Unavailable => Self::Transport(status.message().to_owned()),
_ => Self::RpcError(status.message().to_owned()),
let metadata = status.metadata();
if let Some(label) = metadata.get(ERROR_LABEL) {
match label.to_str().unwrap_or_else(|err| {
unreachable!("error-label should be always able to convert to str: {err:?}")
}) {
"transport" => Self::Transport(status.message().to_owned()),
"redirect" => Self::NotLeader,
"encode-decode" => Self::EncodeDecode(status.message().to_owned()),
_ => Self::RpcError(status.message().to_owned()),
}
} else {
Self::Transport(status.message().to_owned())
}
}
}
Expand Down Expand Up @@ -203,10 +211,17 @@ impl From<Status> for SendSnapshotError {
fn from(status: Status) -> Self {
#[allow(clippy::wildcard_enum_match_arm)]
// it's ok to do so since `SendSnapshotError` only has two variants.
match status.code() {
tonic::Code::FailedPrecondition => Self::NotLeader,
tonic::Code::Unavailable => Self::Transport(status.message().to_owned()),
_ => Self::RpcError(status.message().to_owned()),
let metadata = status.metadata();
if let Some(label) = metadata.get(ERROR_LABEL) {
match label.to_str().unwrap_or_else(|err| {
unreachable!("error-label should be always able to convert to str: {err:?}")
}) {
"transport" => Self::Transport(status.message().to_owned()),
"redirect" => Self::NotLeader,
_ => Self::RpcError(status.message().to_owned()),
}
} else {
Self::Transport(status.message().to_owned())
}
}
}
Expand Down Expand Up @@ -969,7 +984,7 @@ mod tests {
fn get_error_label(status: &Status) -> &str {
let metadata = status.metadata();
metadata
.get("error-label")
.get(ERROR_LABEL)
.expect("error-label should not be None in CurpError")
.to_str()
.expect("error-label must be construct by ascii char")
Expand Down

0 comments on commit 0f3f494

Please sign in to comment.