Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #1458]🔥Refactor rocketmq-client crate error handle🚨 #1459

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rocketmq-broker/src/broker_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum BrokerError {
IllegalArgumentError(String),

#[error("Client error: {0}")]
ClientError(#[from] rocketmq_client_rust::error::MQClientError),
ClientError(#[from] rocketmq_client_rust::client_error::MQClientError),
}

#[cfg(test)]
Expand Down
99 changes: 45 additions & 54 deletions rocketmq-client/src/base/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
use rocketmq_common::common::topic::TopicValidator;
use rocketmq_remoting::code::response_code::ResponseCode;

use crate::error::MQClientError::MQClientErr;
use crate::client_error::ClientErr;
use crate::client_error::MQClientError::MQClientErr;
use crate::producer::default_mq_producer::ProducerConfig;
use crate::Result;

Expand All @@ -36,25 +37,21 @@

pub fn check_group(group: &str) -> Result<()> {
if group.trim().is_empty() {
return Err(MQClientErr(-1, "the specified group is blank".to_string()));
return Err(MQClientErr(ClientErr::new("the specified group is blank")));
}

if group.len() > Self::CHARACTER_MAX_LENGTH {
return Err(MQClientErr(
-1,
"the specified group is longer than group max length 255.".to_string(),
));
return Err(MQClientErr(ClientErr::new(
"the specified group is longer than group max length 255.",
)));
}

if TopicValidator::is_topic_or_group_illegal(group) {
return Err(MQClientErr(
-1,
format!(
"the specified group[{}] contains illegal characters, allowing only \
^[%|a-zA-Z0-9_-]+$",
group
),
));
return Err(MQClientErr(ClientErr::new(format!(
"the specified group[{}] contains illegal characters, allowing only \
^[%|a-zA-Z0-9_-]+$",
group
))));
}
Ok(())
}
Expand All @@ -64,53 +61,53 @@
M: MessageTrait,
{
if msg.is_none() {
return Err(MQClientErr(
return Err(MQClientErr(ClientErr::new_with_code(

Check warning on line 64 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L64

Added line #L64 was not covered by tests
ResponseCode::MessageIllegal as i32,
"the message is null".to_string(),
));
)));

Check warning on line 67 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L67

Added line #L67 was not covered by tests
}
let msg = msg.unwrap();
Self::check_topic(msg.get_topic())?;
Self::is_not_allowed_send_topic(msg.get_topic())?;

if msg.get_body().is_none() {
return Err(MQClientErr(
return Err(MQClientErr(ClientErr::new_with_code(

Check warning on line 74 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L74

Added line #L74 was not covered by tests
ResponseCode::MessageIllegal as i32,
"the message body is null".to_string(),
));
)));

Check warning on line 77 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L77

Added line #L77 was not covered by tests
}

let length = msg.get_body().unwrap().len();
if length == 0 {
return Err(MQClientErr(
return Err(MQClientErr(ClientErr::new_with_code(

Check warning on line 82 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L82

Added line #L82 was not covered by tests
ResponseCode::MessageIllegal as i32,
"the message body length is zero".to_string(),
));
)));

Check warning on line 85 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L85

Added line #L85 was not covered by tests
}

if length > producer_config.max_message_size() as usize {
return Err(MQClientErr(
return Err(MQClientErr(ClientErr::new_with_code(

Check warning on line 89 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L89

Added line #L89 was not covered by tests
ResponseCode::MessageIllegal as i32,
format!(
"the message body size over max value, MAX: {}",
producer_config.max_message_size()
),
));
)));

Check warning on line 95 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L95

Added line #L95 was not covered by tests
}

let lmq_path = msg.get_user_property(&CheetahString::from_static_str(
MessageConst::PROPERTY_INNER_MULTI_DISPATCH,
));
if let Some(value) = lmq_path {
if value.contains(std::path::MAIN_SEPARATOR) {
return Err(MQClientErr(
return Err(MQClientErr(ClientErr::new_with_code(

Check warning on line 103 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L103

Added line #L103 was not covered by tests
ResponseCode::MessageIllegal as i32,
format!(
"INNER_MULTI_DISPATCH {} can not contains {} character",
value,
std::path::MAIN_SEPARATOR
),
));
)));

Check warning on line 110 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L110

Added line #L110 was not covered by tests
}
}

Expand All @@ -119,60 +116,54 @@

pub fn check_topic(topic: &str) -> Result<()> {
if topic.trim().is_empty() {
return Err(MQClientErr(-1, "The specified topic is blank".to_string()));
return Err(MQClientErr(ClientErr::new("The specified topic is blank")));
}

if topic.len() > Self::TOPIC_MAX_LENGTH {
return Err(MQClientErr(
-1,
format!(
"The specified topic is longer than topic max length {}.",
Self::TOPIC_MAX_LENGTH
),
));
return Err(MQClientErr(ClientErr::new(format!(
"The specified topic is longer than topic max length {}.",
Self::TOPIC_MAX_LENGTH
))));
}

if TopicValidator::is_topic_or_group_illegal(topic) {
return Err(MQClientErr(
-1,
format!(
"The specified topic[{}] contains illegal characters, allowing only \
^[%|a-zA-Z0-9_-]+$",
topic
),
));
return Err(MQClientErr(ClientErr::new(format!(
"The specified topic[{}] contains illegal characters, allowing only \
^[%|a-zA-Z0-9_-]+$",
topic
))));
}

Ok(())
}

pub fn is_system_topic(topic: &str) -> Result<()> {
if TopicValidator::is_system_topic(topic) {
return Err(MQClientErr(
-1,
format!("The topic[{}] is conflict with system topic.", topic),
));
return Err(MQClientErr(ClientErr::new(format!(
"The topic[{}] is conflict with system topic.",
topic
))));

Check warning on line 145 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L142-L145

Added lines #L142 - L145 were not covered by tests
}
Ok(())
}

pub fn is_not_allowed_send_topic(topic: &str) -> Result<()> {
if TopicValidator::is_not_allowed_send_topic(topic) {
return Err(MQClientErr(
-1,
format!("Sending message to topic[{}] is forbidden.", topic),
));
return Err(MQClientErr(ClientErr::new(format!(
"Sending message to topic[{}] is forbidden.",
topic
))));

Check warning on line 155 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L152-L155

Added lines #L152 - L155 were not covered by tests
}

Ok(())
}

pub fn check_topic_config(topic_config: &TopicConfig) -> Result<()> {
if !PermName::is_valid(topic_config.perm) {
return Err(MQClientErr(
return Err(MQClientErr(ClientErr::new_with_code(
ResponseCode::NoPermission as i32,
format!("topicPermission value: {} is invalid.", topic_config.perm),
));
)));
Comment on lines +163 to +166
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Inconsistent error creation pattern in permission checks

check_topic_config uses new_with_code while check_broker_config uses new for permission validation errors. Consider standardizing the approach.

  pub fn check_broker_config(broker_config: &HashMap<String, String>) -> Result<()> {
      if let Some(broker_permission) = broker_config.get("brokerPermission") {
          if !PermName::is_valid(broker_permission.parse().unwrap()) {
-             return Err(MQClientErr(ClientErr::new(format!(
+             return Err(MQClientErr(ClientErr::new_with_code(
+                 ResponseCode::NoPermission as i32,
                  "brokerPermission value: {} is invalid.",
                  broker_permission
              ))));
          }
      }

Also applies to: 175-178

}

Ok(())
Expand All @@ -181,10 +172,10 @@
pub fn check_broker_config(broker_config: &HashMap<String, String>) -> Result<()> {
if let Some(broker_permission) = broker_config.get("brokerPermission") {
if !PermName::is_valid(broker_permission.parse().unwrap()) {
return Err(MQClientErr(
-1,
format!("brokerPermission value: {} is invalid.", broker_permission),
));
return Err(MQClientErr(ClientErr::new(format!(
"brokerPermission value: {} is invalid.",
broker_permission
))));
}
}

Expand Down
Loading
Loading