Skip to content

Commit

Permalink
[ISSUE #1456]🔥Refactor rocketmq-broker crate error handle🚨 (#1457)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm authored Nov 30, 2024
1 parent 5b151ff commit 38433cc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use thiserror::Error;
#[derive(Debug, Error)]
pub enum BrokerError {
#[error("broker client error: {0}")]
BrokerClientError(#[from] rocketmq_remoting::remoting_error::RemotingError),
BrokerRemotingError(#[from] rocketmq_remoting::remoting_error::RemotingError),

#[error("Common error: {0}")]
BrokerCommonError(#[from] rocketmq_common::error::Error),
Expand All @@ -34,3 +34,34 @@ pub enum BrokerError {
#[error("Client error: {0}")]
ClientError(#[from] rocketmq_client_rust::error::MQClientError),
}

#[cfg(test)]
mod tests {
use rocketmq_remoting::remoting_error::RemotingError;

use super::*;

#[test]
fn broker_remoting_error_displays_correctly() {
let error = BrokerError::BrokerRemotingError(RemotingError::RemoteError(
"remote error".to_string(),
));
assert_eq!(format!("{}", error), "broker client error: remote error");
}

#[test]
fn mq_broker_error_displays_correctly() {
let error =
BrokerError::MQBrokerError(404, "not found".to_string(), "127.0.0.1".to_string());
assert_eq!(
format!("{}", error),
"Client exception occurred: CODE:404, broker address:127.0.0.1, Message:not found"
);
}

#[test]
fn illegal_argument_error_displays_correctly() {
let error = BrokerError::IllegalArgumentError("illegal argument".to_string());
assert_eq!(format!("{}", error), "illegal argument");
}
}
8 changes: 4 additions & 4 deletions rocketmq-broker/src/client/net/broker_to_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use rocketmq_remoting::net::channel::Channel;
use rocketmq_remoting::protocol::header::check_transaction_state_request_header::CheckTransactionStateRequestHeader;
use rocketmq_remoting::protocol::remoting_command::RemotingCommand;

use crate::error::BrokerError::BrokerClientError;
use crate::error::BrokerError::BrokerCommonError;
use crate::broker_error::BrokerError::BrokerCommonError;
use crate::broker_error::BrokerError::BrokerRemotingError;
use crate::Result;

#[derive(Default, Clone)]
Expand All @@ -38,7 +38,7 @@ impl Broker2Client {
) -> Result<RemotingCommand> {
match channel.send_wait_response(request, timeout_millis).await {
Ok(value) => Ok(value),
Err(e) => Err(BrokerClientError(e)),
Err(e) => Err(BrokerRemotingError(e)),
}
}

Expand All @@ -63,7 +63,7 @@ impl Broker2Client {
}
match channel.send_one_way(request, 100).await {
Ok(_) => Ok(()),
Err(e) => Err(BrokerClientError(e)),
Err(e) => Err(BrokerRemotingError(e)),
}
}
}
4 changes: 2 additions & 2 deletions rocketmq-broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
pub use broker_bootstrap::BrokerBootstrap;
pub use broker_bootstrap::Builder;

use crate::error::BrokerError;
use crate::broker_error::BrokerError;

pub mod command;

pub(crate) mod broker;
pub(crate) mod broker_bootstrap;
pub(crate) mod broker_error;
pub(crate) mod broker_path_config_helper;
pub(crate) mod broker_runtime;
pub(crate) mod client;
pub(crate) mod coldctr;
pub(crate) mod controller;
pub(crate) mod error;
pub(crate) mod filter;
pub(crate) mod hook;
pub(crate) mod load_balance;
Expand Down
8 changes: 4 additions & 4 deletions rocketmq-broker/src/out_api/broker_outer_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ use tracing::error;
use tracing::info;
use tracing::warn;

use crate::error::BrokerError;
use crate::error::BrokerError::BrokerClientError;
use crate::broker_error::BrokerError;
use crate::broker_error::BrokerError::BrokerRemotingError;
use crate::Result;

pub struct BrokerOuterAPI {
Expand Down Expand Up @@ -367,7 +367,7 @@ impl BrokerOuterAPI {
))
}
}
Err(e) => Err(BrokerClientError(e)),
Err(e) => Err(BrokerRemotingError(e)),
}
}

Expand Down Expand Up @@ -402,7 +402,7 @@ impl BrokerOuterAPI {
))
}
}
Err(e) => Err(BrokerClientError(e)),
Err(e) => Err(BrokerRemotingError(e)),
}
}

Expand Down
4 changes: 2 additions & 2 deletions rocketmq-broker/src/processor/query_assignment_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use rocketmq_remoting::protocol::body::set_message_request_mode_request_body::Se
use rocketmq_remoting::protocol::heartbeat::message_model::MessageModel;
use rocketmq_remoting::protocol::{RemotingDeserializable, RemotingSerializable};
use crate::client::manager::consumer_manager::ConsumerManager;
use crate::error::BrokerError;
use crate::error::BrokerError::IllegalArgumentError;
use crate::broker_error::BrokerError;
use crate::broker_error::BrokerError::IllegalArgumentError;
use crate::topic::manager::topic_route_info_manager::TopicRouteInfoManager;
use crate::Result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use rocketmq_rust::RocketMQTokioMutex;
use tracing::info;
use tracing::warn;

use crate::error::BrokerError::MQBrokerError;
use crate::broker_error::BrokerError::MQBrokerError;
use crate::out_api::broker_outer_api::BrokerOuterAPI;

const GET_TOPIC_ROUTE_TIMEOUT: u64 = 3000;
Expand Down

0 comments on commit 38433cc

Please # to comment.