update 新增支持占位符格式的 ServiceException 构造方法

- 新增 ServiceException(String message, Object... args) 构造器,内部使用 Hutool StrFormatter.format 格式化消息
- 解决日志打印和异常抛出信息格式不统一的问题,统一使用 {} 占位符
- 优化异常消息书写,简化拼接,提升代码可读性和维护性
This commit is contained in:
AprilWind
2025-08-06 11:03:01 +08:00
parent e015970f79
commit 543be7a809
9 changed files with 15 additions and 16 deletions

View File

@@ -1,5 +1,6 @@
package org.dromara.common.core.exception;
import cn.hutool.core.text.StrFormatter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
@@ -8,7 +9,7 @@ import lombok.NoArgsConstructor;
import java.io.Serial;
/**
* 业务异常
* 业务异常(支持占位符 {}
*
* @author ruoyi
*/
@@ -45,8 +46,8 @@ public final class ServiceException extends RuntimeException {
this.code = code;
}
public String getDetailMessage() {
return detailMessage;
public ServiceException(String message, Object... args) {
this.message = StrFormatter.format(message, args);
}
@Override
@@ -54,10 +55,6 @@ public final class ServiceException extends RuntimeException {
return message;
}
public Integer getCode() {
return code;
}
public ServiceException setMessage(String message) {
this.message = message;
return this;

View File

@@ -293,7 +293,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
// 校验时间跨度不超过最大限制
if (diff > maxValue) {
throw new ServiceException("最大时间跨度为 " + maxValue + " " + unit.toString().toLowerCase());
throw new ServiceException("最大时间跨度为 {} {}", maxValue, unit.toString().toLowerCase());
}
}

View File

@@ -104,7 +104,7 @@ public class ExcelDownHandler implements SheetWriteHandler {
if (StringUtils.isNotBlank(dictType)) {
// 如果传递了字典名,则依据字典建立下拉
Collection<String> values = Optional.ofNullable(dictService.getAllDictByDictType(dictType))
.orElseThrow(() -> new ServiceException(String.format("字典 %s 不存在", dictType)))
.orElseThrow(() -> new ServiceException("字典 {} 不存在", dictType))
.values();
options = new ArrayList<>(values);
} else if (StringUtils.isNotBlank(converterExp)) {

View File

@@ -119,7 +119,7 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService {
*/
private void validEntityBeforeSave(SysOssConfig entity) {
if (StringUtils.isNotEmpty(entity.getConfigKey()) && !checkConfigKeyUnique(entity)) {
throw new ServiceException("操作配置'" + entity.getConfigKey() + "'失败, 配置key已存在!");
throw new ServiceException("操作配置'{}'失败, 配置key已存在!", entity.getConfigKey());
}
}

View File

@@ -170,7 +170,7 @@ public class SysConfigServiceImpl implements ISysConfigService {
List<SysConfig> list = baseMapper.selectByIds(configIds);
list.forEach(config -> {
if (StringUtils.equals(SystemConstants.YES, config.getConfigType())) {
throw new ServiceException(String.format("内置参数【%s】不能删除", config.getConfigKey()));
throw new ServiceException("内置参数【{}】不能删除", config.getConfigKey());
}
CacheUtils.evict(CacheNames.SYS_CONFIG, config.getConfigKey());
});

View File

@@ -138,7 +138,7 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService {
boolean assigned = dictDataMapper.exists(new LambdaQueryWrapper<SysDictData>()
.eq(SysDictData::getDictType, x.getDictType()));
if (assigned) {
throw new ServiceException(String.format("%1$s已分配,不能删除", x.getDictName()));
throw new ServiceException("{}已分配,不能删除", x.getDictName());
}
});
baseMapper.deleteByIds(dictIds);

View File

@@ -223,7 +223,7 @@ public class SysPostServiceImpl implements ISysPostService {
List<SysPost> list = baseMapper.selectByIds(postIds);
for (SysPost post : list) {
if (this.countUserPostById(post.getPostId()) > 0) {
throw new ServiceException(String.format("%1$s已分配,不能删除!", post.getPostName()));
throw new ServiceException("{}已分配,不能删除!", post.getPostName());
}
}
return baseMapper.deleteByIds(postIds);

View File

@@ -19,7 +19,9 @@ import org.dromara.common.core.utils.*;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.*;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.SysUserPost;
import org.dromara.system.domain.SysUserRole;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.domain.vo.SysPostVo;
import org.dromara.system.domain.vo.SysRoleVo;
@@ -351,7 +353,7 @@ public class SysUserServiceImpl implements ISysUserService {
// 防止错误更新后导致的数据误删除
int flag = baseMapper.updateById(sysUser);
if (flag < 1) {
throw new ServiceException("修改用户" + user.getUserName() + "信息失败");
throw new ServiceException("修改用户{}信息失败", user.getUserName());
}
return flag;
}

View File

@@ -130,7 +130,7 @@ public class FlwDefinitionServiceImpl implements IFlwDefinitionService {
}
}
if (CollUtil.isNotEmpty(errorMsg)) {
throw new ServiceException("节点【" + StringUtils.join(errorMsg, ",") + "】未配置办理人!");
throw new ServiceException("节点【{}】未配置办理人!", StringUtils.join(errorMsg, ","));
}
}
return defService.publish(id);