-
Notifications
You must be signed in to change notification settings - Fork 128
refactor: fix typos and improve error messages #229
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -237,7 +237,7 @@ pub async fn add_instance( | |
if !namespace_privilege.check_permission(&instance.namespace_id) { | ||
user_no_namespace_permission!(&instance.namespace_id); | ||
} | ||
if !instance.check_vaild() { | ||
if !instance.check_valid() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 错误响应使用 HTTP 200 状态码不恰当
📋 问题详情当实例检查无效时,返回 HTTP 200 状态码(Ok)但携带错误消息,这与语义不符。建议使用 400 等错误状态码以准确反映错误场景。 💡 解决方案调整 HTTP 状态码为 400: @@ -241,7 +241,7 @@
- HttpResponse::Ok().json(ApiResult::<()>::error(
+ HttpResponse::BadRequest().json(ApiResult::<()>::error(
|
||
HttpResponse::Ok().json(ApiResult::<()>::error( | ||
ERROR_CODE_SYSTEM_ERROR.to_string(), | ||
Some("instance check is invalid".to_string()), | ||
|
@@ -274,7 +274,7 @@ pub async fn remove_instance( | |
if !namespace_privilege.check_permission(&instance.namespace_id) { | ||
user_no_namespace_permission!(&instance.namespace_id); | ||
} | ||
if !instance.check_vaild() { | ||
if !instance.check_valid() { | ||
HttpResponse::Ok().json(ApiResult::<()>::error( | ||
ERROR_CODE_SYSTEM_ERROR.to_string(), | ||
Some("instance check is invalid".to_string()), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,7 @@ create index if not exists tb_service_key_idx on tb_service(namespace_id,service | |
Ok(()) | ||
} | ||
else{ | ||
Err(anyhow::anyhow!("update_service:unvaild service info")) | ||
Err(anyhow::anyhow!("update_service:invalid service info")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 错误消息描述不够具体,建议增强可读性
📋 问题详情错误消息 'update_service:invalid service info' 过于模糊,建议增加具体上下文信息(如缺少必要字段)以帮助调试。 💡 解决方案增强错误消息的描述性: @@ -63,7 +63,7 @@
- Err(anyhow::anyhow!("update_service:invalid service info"))
+ Err(anyhow::anyhow!("update_service: 无效的服务信息,缺少必要字段或格式错误"))
|
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ impl ServiceDO { | |
s | ||
} | ||
|
||
pub fn check_vaild(&self) -> bool { | ||
pub fn check_valid(&self) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 方法 'check_valid' 缺少返回值,导致编译错误
📋 问题详情在方法 'check_valid' 中,当条件不满足时未返回布尔值,导致函数无法正确返回,引发编译错误。必须在 else 分支中返回 false 以确保所有执行路径都有返回值。 💡 解决方案方法 'check_valid' 缺少返回值,导致编译错误。需要添加 else 分支返回 false: @@ -40,6 +40,7 @@
(self.namespace_id.as_ref(), self.service_name.as_ref(), self.group_name.as_ref()) {
true
} else {
+ false
}
|
||
if let (Some(namespace_id),Some(service_name),Some(group_name)) = | ||
(self.namespace_id.as_ref(),self.service_name.as_ref(),self.group_name.as_ref()) { | ||
true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ impl NamingUtils { | |
format!("{}@@{}", group_name, service_name) | ||
} | ||
|
||
pub fn split_group_and_serivce_name(grouped_name: &str) -> Option<(String, String)> { | ||
pub fn split_group_and_service_name(grouped_name: &str) -> Option<(String, String)> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使用 'split_once()' 替代 'split().collect()' 提升代码简洁性和效率
📋 问题详情当前代码使用 'split("@@").collect::<Vec<_>>()' 来分割字符串,这可能产生不必要的中间 Vec。改用 'split_once()' 可以更高效地处理恰好两次分割的情况,同时代码更简洁。 💡 解决方案使用 'split_once()' 简化代码: @@ -37,8 +37,3 @@
- let split = grouped_name.split("&&").collect::<Vec<_>>();
- if split.is_empty() {
- return None;
- }
- if split.len() != 2 {
- return None;
- }
- Some((split[0].to_string(), split[1].to_string()))
+ grouped_name.split_once("&&").map(|(group, service)| (group.to_string(), service.to_string()))
|
||
let split = grouped_name.split("@@").collect::<Vec<_>>(); | ||
if split.is_empty() { | ||
return None; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,12 +77,12 @@ impl InstanceWebParams { | |
|
||
let grouped_name = self.service_name.unwrap_or_default(); | ||
if let Some((group_name, service_name)) = | ||
NamingUtils::split_group_and_serivce_name(&grouped_name) | ||
NamingUtils::split_group_and_service_name(&grouped_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 硬编码的错误消息应使用常量管理
📋 问题详情在多个地方使用硬编码的错误消息 'serviceName is invalid!',这可能导致不一致和维护困难。建议将这些消息定义为常量以提高可维护性。 💡 解决方案将硬编码的错误消息定义为常量: const INVALID_SERVICE_NAME_MSG: &str = "serviceName is invalid!"; 然后替换现有代码: @@ -80,7 +80,7 @@
- return Err("serviceName is invalid!".to_owned());
+ return Err(INVALID_SERVICE_NAME_MSG.to_owned());
|
||
{ | ||
instance.service_name = Arc::new(service_name); | ||
instance.group_name = Arc::new(group_name); | ||
} else { | ||
return Err("serivceName is unvaild!".to_owned()); | ||
return Err("serviceName is invalid!".to_owned()); | ||
} | ||
if let Some(group_name) = self.group_name { | ||
if !group_name.is_empty() { | ||
|
@@ -121,12 +121,12 @@ impl InstanceWebQueryListParams { | |
let mut group_name = "".to_owned(); | ||
let grouped_name = self.service_name.clone().unwrap_or_default(); | ||
if let Some((_group_name, _service_name)) = | ||
NamingUtils::split_group_and_serivce_name(&grouped_name) | ||
NamingUtils::split_group_and_service_name(&grouped_name) | ||
{ | ||
service_name = _service_name; | ||
group_name = _group_name; | ||
} else { | ||
return Err("serivceName is unvaild!".to_owned()); | ||
return Err("serviceName is invalid!".to_owned()); | ||
} | ||
if let Some(_group_name) = self.group_name.as_ref() { | ||
if !_group_name.is_empty() { | ||
|
@@ -219,7 +219,7 @@ impl BeatRequest { | |
if service_name_option.is_none() { | ||
let grouped_name = self.service_name.unwrap(); | ||
if let Some((group_name, service_name)) = | ||
NamingUtils::split_group_and_serivce_name(&grouped_name) | ||
NamingUtils::split_group_and_service_name(&grouped_name) | ||
{ | ||
instance.service_name = Arc::new(service_name); | ||
instance.group_name = Arc::new(group_name); | ||
|
@@ -263,7 +263,7 @@ impl BeatRequest { | |
if service_name_option.is_none() { | ||
if let Some(grouped_name) = self.service_name { | ||
if let Some((group_name, service_name)) = | ||
NamingUtils::split_group_and_serivce_name(&grouped_name) | ||
NamingUtils::split_group_and_service_name(&grouped_name) | ||
{ | ||
instance.service_name = Arc::new(service_name); | ||
instance.group_name = Arc::new(group_name); | ||
|
@@ -323,7 +323,7 @@ impl BeatInfo { | |
}; | ||
if let Some(grouped_name) = self.service_name.as_ref() { | ||
if let Some((group_name, service_name)) = | ||
NamingUtils::split_group_and_serivce_name(grouped_name) | ||
NamingUtils::split_group_and_service_name(grouped_name) | ||
{ | ||
instance.service_name = Arc::new(service_name); | ||
instance.group_name = Arc::new(group_name); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
重复的错误消息 'limiter is invalid' 可以通过辅助函数简化
📋 问题详情
在三个不同的变量解析处(a、b、c)重复使用相同的错误消息 'limiter is invalid',这会导致代码冗余。建议提取一个辅助函数来统一处理此类错误,提高代码可维护性。
💡 解决方案
提取辅助函数来减少重复代码:
然后替换现有代码:
有用意见👍 | 无用意见👎 | 错误意见❌