1、统一抛出ServiceException带占位符的填充方式,不再使用ServiceExceptionUtils;2、新增System服务ErrorCode的国际化;3、Webcad异步导入移除分布式锁,导入解析事务完善;

This commit is contained in:
gaoqr
2025-11-20 11:12:21 +08:00
parent f051e6466b
commit ab4ec08078
139 changed files with 1005 additions and 1193 deletions
@@ -22,15 +22,21 @@ public final class ServiceException extends RuntimeException {
*/
private String message;
/**
* 占位参数
*/
private transient Object[] args;
/**
* 空构造方法,避免反序列化问题
*/
public ServiceException() {
}
public ServiceException(ErrorCode errorCode) {
public ServiceException(ErrorCode errorCode, Object... args) {
this.code = errorCode.getCode();
this.message = errorCode.getMsg();
this.args = args;
}
public ServiceException(Integer code, String message) {
@@ -22,7 +22,7 @@ public class GlobalErrorCodeConstants {
public static final ErrorCode REQUEST_ORGAN_ID_NOT_EXIST = new ErrorCode(400, "global.error.request.organId.notExist");
public static final ErrorCode NO_PERMISSION_TO_VISIT_ORG = new ErrorCode(400, "global.error.no.permission.to.visit.org");
public static final ErrorCode REQUEST_PARAM_TYPE_ERROR = new ErrorCode(400, "global.error.request.param.type.error");
public static final ErrorCode UNAUTHORIZED = new ErrorCode(401, "账号未登录");
public static final ErrorCode UNAUTHORIZED = new ErrorCode(401, "global.not.login");
public static final ErrorCode FORBIDDEN = new ErrorCode(403, "global.error.no.permission");
public static final ErrorCode REQUEST_PARAM_MISSING = new ErrorCode(403, "global.error.request.param.missing");
public static final ErrorCode NOT_FOUND = new ErrorCode(404, "global.error.request.not.found");
@@ -3,13 +3,13 @@ package com.cf.imes.framework.common.util.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.exception.ErrorCode;
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import java.util.List;
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -31,81 +31,81 @@ public class AssertUtils {
* */
public static void notEmpty(List<?> array, ErrorCode errorCode) {
if (ObjectUtils.isEmpty(array)) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
public static void empty(List<?> array, ErrorCode errorCode) {
if (!ObjectUtils.isEmpty(array)) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
public static void empty(List<?> array, ErrorCode errorCode, Object... params) {
if (!ObjectUtils.isEmpty(array)) {
throw ServiceExceptionUtil.exception(errorCode, params);
throw new ServiceException(errorCode, params);
}
}
public static void empty(Object object, ErrorCode errorCode) {
if (object != null) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
public static void empty(Object object, ErrorCode errorCode, Object... params) {
if (object != null) {
throw ServiceExceptionUtil.exception(errorCode, params);
throw new ServiceException(errorCode, params);
}
}
public static void notEmpty(Object object, ErrorCode errorCode) {
if (object == null) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
public static void notEmpty(List<?> array ,Object object, ErrorCode errorCode) {
if (object == null || ObjectUtils.isEmpty(array)) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
public static void notEmpty(List<?> array, ErrorCode errorCode,Object object) {
if (ObjectUtils.isEmpty(array)) {
throw exception(errorCode,object);
throw new ServiceException(errorCode,object);
}
}
public static void notEmpty(String str, ErrorCode errorCode) {
if (StringUtils.isEmpty(str)) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
public static void notEquals(Object obj1, Object obj2, ErrorCode errorCode) {
if (ObjectUtil.notEqual(obj1, obj2)) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
public static void equals(Object obj1, Object obj2, ErrorCode errorCode) {
if (ObjectUtil.equal(obj1, obj2)) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
public static void isFalse(boolean expression, ErrorCode errorCode) {
if (!expression) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
public static void isTrue(boolean expression, ErrorCode errorCode) {
if (expression) {
throw exception(errorCode);
throw new ServiceException(errorCode);
}
}
}
@@ -27,6 +27,6 @@ public class I18nUtils {
* @return
*/
public String getMessage(String code, Object... args) {
return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
return messageSource.getMessage(code, args, code, LocaleContextHolder.getLocale());
}
}
@@ -8,7 +8,6 @@ import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.cf.imes.framework.common.exception.ErrorCode;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
import com.cf.imes.framework.ip.core.enums.ErrorCodeConstants;
import com.cf.imes.framework.ip.core.property.IPQueryProperties;
import com.cf.imes.framework.ip.core.service.IPQueryService;
@@ -46,11 +45,11 @@ public class IPQueryServiceImpl implements IPQueryService {
String appCode = ipQueryProperties.getAppCode();
if (CharSequenceUtil.isEmpty(apiUrl)) {
log.error("[IPQueryService][querySource]request apiUrl为空");
throw ServiceExceptionUtil.exception(queryError);
throw new ServiceException(queryError);
}
if (CharSequenceUtil.isEmpty(appCode)) {
log.error("[IPQueryService][querySource]request appCode为空");
throw ServiceExceptionUtil.exception(queryError);
throw new ServiceException(queryError);
}
log.info("[IPQueryService][querySource]request url{}", apiUrl);
log.info("[IPQueryService][querySource]request param, appCode: {}, ip: {}", appCode, ip);
@@ -63,7 +62,7 @@ public class IPQueryServiceImpl implements IPQueryService {
data.setIp(ip);
return data;
} else {
ServiceException serviceException = ServiceExceptionUtil.exception(queryError);
ServiceException serviceException = new ServiceException(queryError);
log.error(serviceException.getMessage() + ",状态【{}】,异常:{}", ipQueryRespDTO.getRet(), respBody);
return null;
}
@@ -83,11 +83,10 @@ public class ChenfengOrganAutoConfiguration {
public FilterRegistrationBean<OrganSecurityWebFilter> organSecurityWebFilter(OrganProperties tenantProperties,
WebProperties webProperties,
GlobalExceptionHandler globalExceptionHandler,
OrganFrameworkService organFrameworkService,
I18nUtils i18nUtils) {
OrganFrameworkService organFrameworkService) {
FilterRegistrationBean<OrganSecurityWebFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new OrganSecurityWebFilter(tenantProperties, webProperties,
globalExceptionHandler, organFrameworkService, i18nUtils));
globalExceptionHandler, organFrameworkService));
registrationBean.setOrder(WebFilterOrderEnum.TENANT_SECURITY_FILTER);
return registrationBean;
}
@@ -6,8 +6,8 @@ import com.cf.imes.framework.common.enums.UserTypeEnum;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.servlet.ServletUtils;
import com.cf.imes.framework.common.util.i18n.core.util.I18nUtils;
import com.cf.imes.framework.common.util.servlet.ServletUtils;
import com.cf.imes.framework.security.core.LoginUser;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.framework.organ.config.OrganProperties;
@@ -18,6 +18,7 @@ import com.cf.imes.framework.web.core.filter.ApiRequestFilter;
import com.cf.imes.framework.web.core.handler.GlobalExceptionHandler;
import com.cf.imes.framework.web.core.util.WebFrameworkUtils;
import com.cf.imes.module.system.enums.ErrorCodeConstants;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
@@ -52,19 +53,18 @@ public class OrganSecurityWebFilter extends ApiRequestFilter {
private final GlobalExceptionHandler globalExceptionHandler;
private final OrganFrameworkService organFrameworkService;
private final I18nUtils i18nUtils;
@Resource
private I18nUtils i18nUtils;
public OrganSecurityWebFilter(OrganProperties organProperties,
WebProperties webProperties,
GlobalExceptionHandler globalExceptionHandler,
OrganFrameworkService organFrameworkService,
I18nUtils i18nUtils) {
OrganFrameworkService organFrameworkService) {
super(webProperties);
this.organProperties = organProperties;
this.pathMatcher = new AntPathMatcher();
this.globalExceptionHandler = globalExceptionHandler;
this.organFrameworkService = organFrameworkService;
this.i18nUtils = i18nUtils;
}
@Override
@@ -4,7 +4,6 @@ import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.RandomUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
import com.cf.imes.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
import com.cf.imes.framework.pay.core.client.dto.refund.PayRefundRespDTO;
import com.cf.imes.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO;
@@ -171,7 +170,7 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
public void testUnifiedRefund_throwServiceException() throws AlipayApiException {
// mock 方法
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> true)))
.thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
.thenThrow(new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
// 准备请求参数
String notifyUrl = randomURL();
PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> o.setNotifyUrl(notifyUrl));
@@ -2,7 +2,6 @@ package com.cf.imes.framework.pay.core.client.impl.alipay;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
import com.cf.imes.framework.pay.core.client.dto.order.PayOrderRespDTO;
import com.cf.imes.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
import com.cf.imes.framework.common.exception.PayException;
@@ -135,7 +134,7 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest {
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> {
assertEquals(notifyUrl, request.getNotifyUrl());
return true;
}))).thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
}))).thenThrow(new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
// 准备请求参数
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price);
@@ -1,5 +1,6 @@
package com.cf.imes.framework.security.core.aop;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.security.core.annotations.PreAuthenticated;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import lombok.extern.slf4j.Slf4j;
@@ -8,7 +9,6 @@ import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import static com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants.UNAUTHORIZED;
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
@Aspect
@Slf4j
@@ -17,7 +17,7 @@ public class PreAuthenticatedAspect {
@Around("@annotation(preAuthenticated)")
public Object around(ProceedingJoinPoint joinPoint, PreAuthenticated preAuthenticated) throws Throwable {
if (SecurityFrameworkUtils.getLoginUser() == null) {
throw exception(UNAUTHORIZED);
throw new ServiceException(UNAUTHORIZED);
}
return joinPoint.proceed();
}
@@ -2,7 +2,9 @@ package com.cf.imes.framework.security.core.handler;
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.i18n.core.util.I18nUtils;
import com.cf.imes.framework.common.util.servlet.ServletUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
@@ -25,11 +27,13 @@ import static com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConsta
@SuppressWarnings("JavadocReference") // 忽略文档引用报错
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
@Resource
private I18nUtils i18nUtils;
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
log.debug("[commence][访问 URL({}) 时,没有登录]", request.getRequestURI(), e);
// 返回 401
ServletUtils.writeJSON(response, CommonResult.error(UNAUTHORIZED));
ServletUtils.writeJSON(response, CommonResult.error(UNAUTHORIZED.getCode(), i18nUtils.getMessage(UNAUTHORIZED.getMsg())));
}
}
@@ -66,7 +66,7 @@ public class ChenfengWebAutoConfiguration implements WebMvcConfigurer {
}
@Bean
public GlobalExceptionHandler globalExceptionHandler(ApiErrorLogApi apiErrorLogApi, I18nUtils i18nUtils) {
public GlobalExceptionHandler globalExceptionHandler(ApiErrorLogApi apiErrorLogApi) {
return new GlobalExceptionHandler(applicationName, apiErrorLogApi);
}
@@ -235,7 +235,7 @@ public class GlobalExceptionHandler {
} else {
log.error(String.format("==========[serviceExceptionHandler]==========%s%s", ex.getCode(), ex.getMessage()));
}
return CommonResult.error(ex.getCode(), ex.getMessage());
return CommonResult.error(ex.getCode(), ex.getMessage(), ex.getArgs());
}
/**