cf-gateway支持ServiceException国际化

This commit is contained in:
gaoqr
2026-05-09 09:34:11 +08:00
parent 5494c2ef42
commit 6bc9298294
6 changed files with 32 additions and 6 deletions
@@ -152,6 +152,7 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
if(commonResult != null) { if(commonResult != null) {
Integer code = commonResult.getCode(); Integer code = commonResult.getCode();
String msg = commonResult.getMsg(); String msg = commonResult.getMsg();
Object[] args = commonResult.getArgs();
if(code != 0) { if(code != 0) {
if (TOKEN_TIME_IS_EXPIRES.getCode().equals(code)) { if (TOKEN_TIME_IS_EXPIRES.getCode().equals(code)) {
throw new ServiceException(TOKEN_TIME_IS_EXPIRES); throw new ServiceException(TOKEN_TIME_IS_EXPIRES);
@@ -160,9 +161,9 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
} else if (ORGAN_NOT_EXISTS.getCode().equals(code)) { } else if (ORGAN_NOT_EXISTS.getCode().equals(code)) {
throw new ServiceException(ORGAN_NOT_EXISTS); throw new ServiceException(ORGAN_NOT_EXISTS);
} else if (ORGAN_DISABLE.getCode().equals(code)) { } else if (ORGAN_DISABLE.getCode().equals(code)) {
throw new ServiceException(new ErrorCode(code, msg)); throw new ServiceException(new ErrorCode(code, msg), args);
} else if (ORGAN_EXPIRE.getCode().equals(code)) { } else if (ORGAN_EXPIRE.getCode().equals(code)) {
throw new ServiceException(new ErrorCode(code, msg)); throw new ServiceException(new ErrorCode(code, msg), args);
} }
} }
} }
@@ -1,10 +1,13 @@
package com.cf.imes.gateway.handler; package com.cf.imes.gateway.handler;
import com.cf.imes.framework.common.constants.ResourceBundleConstants;
import com.cf.imes.framework.common.exception.ServiceException; import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.pojo.CommonResult; import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.i18n.core.util.ResourceBundleUtils;
import com.cf.imes.gateway.util.WebFrameworkUtils; import com.cf.imes.gateway.util.WebFrameworkUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
@@ -72,18 +75,19 @@ public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
log.error("[defaultExceptionHandler][uri({}/{}) 发生异常]", request.getURI(), request.getMethod(), ex); log.error("[defaultExceptionHandler][uri({}/{}) 发生异常]", request.getURI(), request.getMethod(), ex);
// TODO 晨丰:是否要插入异常日志呢? // TODO 晨丰:是否要插入异常日志呢?
// 返回 ERROR CommonResult // 返回 ERROR CommonResult
return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg()); return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), ResourceBundleUtils.getMessage(ResourceBundleConstants.MESSAGE_ERROR_PATH, INTERNAL_SERVER_ERROR.getMsg(), null, LocaleContextHolder.getLocale(), null));
} }
@ExceptionHandler(value = ServiceException.class) @ExceptionHandler(value = ServiceException.class)
public CommonResult serviceExceptionHandler(ServiceException ex) { public CommonResult serviceExceptionHandler(ServiceException ex) {
String errMsg = ResourceBundleUtils.getMessage(ResourceBundleConstants.MESSAGE_ERROR_PATH, ex.getMessage(), null, LocaleContextHolder.getLocale(), null);
// 系统异常输出堆栈 // 系统异常输出堆栈
if(INTERNAL_SERVER_ERROR.getCode().equals(ex.getCode())) { if (INTERNAL_SERVER_ERROR.getCode().equals(ex.getCode())) {
log.info("[serviceExceptionHandler]", ex); log.info("[serviceExceptionHandler]", ex);
} else { } else {
log.info(String.format("==========[serviceExceptionHandler]==========%s%s", ex.getCode(), ex.getMessage())); log.info(String.format("==========[serviceExceptionHandler]==========%s%s", ex.getCode(), errMsg));
} }
return CommonResult.error(ex.getCode(), ex.getMessage()); return CommonResult.error(ex.getCode(), errMsg);
} }
} }
@@ -16,6 +16,9 @@ spring:
- optional:classpath:application-${spring.profiles.active}.yaml # 加载【本地】配置 - optional:classpath:application-${spring.profiles.active}.yaml # 加载【本地】配置
- optional:nacos:${spring.application.name}-${spring.profiles.active}.yaml # 加载【Nacos】的配置 - optional:nacos:${spring.application.name}-${spring.profiles.active}.yaml # 加载【Nacos】的配置
messages:
basename: message_errorcode
cloud: cloud:
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类 # Spring Cloud Gateway 配置项,对应 GatewayProperties 类
gateway: gateway:
@@ -0,0 +1,6 @@
token.time.is_expires=当前token已过期,请重新配置或延长时间
token.config.not_exists=当前应用的token配置不存在
organ.not_exists=组织不存在
organ.disable=[{0}]组织已被禁用
organ.expire=[{0}]组织已过期
global.error.default.error=系统繁忙,请稍后再试
@@ -0,0 +1,6 @@
token.time.is_expires=當前token已過期,請重新配置或延長時間
token.config.not_exists=當前應用的token配置不存在
organ.not_exists=組織不存在
organ.disable=[{0}]組織已被禁用
organ.expire=[{0}]組織已過期
global.error.default.error=系統繁忙,請稍後再試
@@ -0,0 +1,6 @@
token.time.is_expires=The current token has expired. Please reconfigure or extend the validity period
token.config.not_exists=The token configuration for the current application does not exist
organ.not_exists=Organization does not exist
organ.disable=Organization [{0}] has been disabled
organ.expire=Organization [{0}] has expired
global.error.default.error=System is busy, please try again later