Skip to content
New issue

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

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

Already on GitHub? # to your account

[ISSUE #1482]♻️Refactor create MQClientErr replace with mq_client_err! macro🔥 #1483

Merged
merged 1 commit into from
Dec 1, 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
72 changes: 33 additions & 39 deletions rocketmq-client/src/base/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
use rocketmq_common::common::topic::TopicValidator;
use rocketmq_remoting::code::response_code::ResponseCode;

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

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

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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#L63-L64

Added lines #L63 - L64 were 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(ClientErr::new_with_code(
return mq_client_err!(

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

View check run for this annotation

Codecov / codecov/patch

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

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

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#L73-L74

Added lines #L73 - L74 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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#L81-L82

Added lines #L81 - L82 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L86 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 92 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L91-L92

Added lines #L91 - L92 were 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(ClientErr::new_with_code(
return mq_client_err!(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L100 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 107 in rocketmq-client/src/base/validators.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-client/src/base/validators.rs#L106-L107

Added lines #L106 - L107 were not covered by tests
}
}

Expand All @@ -116,54 +113,51 @@

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

if topic.len() > Self::TOPIC_MAX_LENGTH {
return Err(MQClientErr(ClientErr::new(format!(
return mq_client_err!(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(ClientErr::new(format!(
return mq_client_err!(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(ClientErr::new(format!(
return mq_client_err!(format!(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L139 was not covered by tests
"The topic[{}] is conflict with system topic.",
topic
))));
));

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L142 was 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(ClientErr::new(format!(
"Sending message to topic[{}] is forbidden.",
topic
))));
return mq_client_err!(format!("Sending message to topic[{}] is forbidden.", topic));

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L149 was not covered by tests
}

Ok(())
}

pub fn check_topic_config(topic_config: &TopicConfig) -> Result<()> {
if !PermName::is_valid(topic_config.perm) {
return Err(MQClientErr(ClientErr::new_with_code(
return mq_client_err!(
ResponseCode::NoPermission as i32,
format!("topicPermission value: {} is invalid.", topic_config.perm),
)));
format!("topicPermission value: {} is invalid.", topic_config.perm)
);
}

Ok(())
Expand All @@ -172,10 +166,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(ClientErr::new(format!(
return mq_client_err!(format!(
"brokerPermission value: {} is invalid.",
broker_permission
))));
));
}
}

Expand Down
Loading
Loading