Skip to content

Commit 2f5f0c6

Browse files
authored
[ISSUE #1482]♻️Refactor create MQClientErr replace with mq_client_err! macro🔥 (#1483)
1 parent bd5d50b commit 2f5f0c6

13 files changed

+173
-246
lines changed

rocketmq-client/src/base/validators.rs

+33-39
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ use rocketmq_common::common::message::MessageTrait;
2424
use rocketmq_common::common::topic::TopicValidator;
2525
use rocketmq_remoting::code::response_code::ResponseCode;
2626

27-
use crate::client_error::ClientErr;
28-
use crate::client_error::MQClientError::MQClientErr;
27+
use crate::mq_client_err;
2928
use crate::producer::default_mq_producer::ProducerConfig;
3029
use crate::Result;
3130

@@ -37,21 +36,19 @@ impl Validators {
3736

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

4342
if group.len() > Self::CHARACTER_MAX_LENGTH {
44-
return Err(MQClientErr(ClientErr::new(
45-
"the specified group is longer than group max length 255.",
46-
)));
43+
return mq_client_err!("the specified group is longer than group max length 255.");
4744
}
4845

4946
if TopicValidator::is_topic_or_group_illegal(group) {
50-
return Err(MQClientErr(ClientErr::new(format!(
47+
return mq_client_err!(format!(
5148
"the specified group[{}] contains illegal characters, allowing only \
5249
^[%|a-zA-Z0-9_-]+$",
5350
group
54-
))));
51+
));
5552
}
5653
Ok(())
5754
}
@@ -61,53 +58,53 @@ impl Validators {
6158
M: MessageTrait,
6259
{
6360
if msg.is_none() {
64-
return Err(MQClientErr(ClientErr::new_with_code(
61+
return mq_client_err!(
6562
ResponseCode::MessageIllegal as i32,
66-
"the message is null".to_string(),
67-
)));
63+
"the message is null".to_string()
64+
);
6865
}
6966
let msg = msg.unwrap();
7067
Self::check_topic(msg.get_topic())?;
7168
Self::is_not_allowed_send_topic(msg.get_topic())?;
7269

7370
if msg.get_body().is_none() {
74-
return Err(MQClientErr(ClientErr::new_with_code(
71+
return mq_client_err!(
7572
ResponseCode::MessageIllegal as i32,
76-
"the message body is null".to_string(),
77-
)));
73+
"the message body is null".to_string()
74+
);
7875
}
7976

8077
let length = msg.get_body().unwrap().len();
8178
if length == 0 {
82-
return Err(MQClientErr(ClientErr::new_with_code(
79+
return mq_client_err!(
8380
ResponseCode::MessageIllegal as i32,
84-
"the message body length is zero".to_string(),
85-
)));
81+
"the message body length is zero".to_string()
82+
);
8683
}
8784

8885
if length > producer_config.max_message_size() as usize {
89-
return Err(MQClientErr(ClientErr::new_with_code(
86+
return mq_client_err!(
9087
ResponseCode::MessageIllegal as i32,
9188
format!(
9289
"the message body size over max value, MAX: {}",
9390
producer_config.max_message_size()
94-
),
95-
)));
91+
)
92+
);
9693
}
9794

9895
let lmq_path = msg.get_user_property(&CheetahString::from_static_str(
9996
MessageConst::PROPERTY_INNER_MULTI_DISPATCH,
10097
));
10198
if let Some(value) = lmq_path {
10299
if value.contains(std::path::MAIN_SEPARATOR) {
103-
return Err(MQClientErr(ClientErr::new_with_code(
100+
return mq_client_err!(
104101
ResponseCode::MessageIllegal as i32,
105102
format!(
106103
"INNER_MULTI_DISPATCH {} can not contains {} character",
107104
value,
108105
std::path::MAIN_SEPARATOR
109-
),
110-
)));
106+
)
107+
);
111108
}
112109
}
113110

@@ -116,54 +113,51 @@ impl Validators {
116113

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

122119
if topic.len() > Self::TOPIC_MAX_LENGTH {
123-
return Err(MQClientErr(ClientErr::new(format!(
120+
return mq_client_err!(format!(
124121
"The specified topic is longer than topic max length {}.",
125122
Self::TOPIC_MAX_LENGTH
126-
))));
123+
));
127124
}
128125

129126
if TopicValidator::is_topic_or_group_illegal(topic) {
130-
return Err(MQClientErr(ClientErr::new(format!(
127+
return mq_client_err!(format!(
131128
"The specified topic[{}] contains illegal characters, allowing only \
132129
^[%|a-zA-Z0-9_-]+$",
133130
topic
134-
))));
131+
));
135132
}
136133

137134
Ok(())
138135
}
139136

140137
pub fn is_system_topic(topic: &str) -> Result<()> {
141138
if TopicValidator::is_system_topic(topic) {
142-
return Err(MQClientErr(ClientErr::new(format!(
139+
return mq_client_err!(format!(
143140
"The topic[{}] is conflict with system topic.",
144141
topic
145-
))));
142+
));
146143
}
147144
Ok(())
148145
}
149146

150147
pub fn is_not_allowed_send_topic(topic: &str) -> Result<()> {
151148
if TopicValidator::is_not_allowed_send_topic(topic) {
152-
return Err(MQClientErr(ClientErr::new(format!(
153-
"Sending message to topic[{}] is forbidden.",
154-
topic
155-
))));
149+
return mq_client_err!(format!("Sending message to topic[{}] is forbidden.", topic));
156150
}
157151

158152
Ok(())
159153
}
160154

161155
pub fn check_topic_config(topic_config: &TopicConfig) -> Result<()> {
162156
if !PermName::is_valid(topic_config.perm) {
163-
return Err(MQClientErr(ClientErr::new_with_code(
157+
return mq_client_err!(
164158
ResponseCode::NoPermission as i32,
165-
format!("topicPermission value: {} is invalid.", topic_config.perm),
166-
)));
159+
format!("topicPermission value: {} is invalid.", topic_config.perm)
160+
);
167161
}
168162

169163
Ok(())
@@ -172,10 +166,10 @@ impl Validators {
172166
pub fn check_broker_config(broker_config: &HashMap<String, String>) -> Result<()> {
173167
if let Some(broker_permission) = broker_config.get("brokerPermission") {
174168
if !PermName::is_valid(broker_permission.parse().unwrap()) {
175-
return Err(MQClientErr(ClientErr::new(format!(
169+
return mq_client_err!(format!(
176170
"brokerPermission value: {} is invalid.",
177171
broker_permission
178-
))));
172+
));
179173
}
180174
}
181175

0 commit comments

Comments
 (0)