feat: 新增国际化及全局异常码配置

This commit is contained in:
jasmine
2024-06-24 13:44:03 +00:00
committed by Charles7c
parent 07d9fdf3e2
commit ce08f28a61
8 changed files with 287 additions and 0 deletions

View File

@@ -19,6 +19,8 @@ package top.continew.starter.web.autoconfigure.exception;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
@@ -36,7 +38,11 @@ import org.springframework.web.multipart.MultipartException;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.exception.BadRequestException;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.core.exception.GlobalException;
import top.continew.starter.core.exception.ResultInfoInterface;
import top.continew.starter.web.autoconfigure.i18n.I18nProperties;
import top.continew.starter.web.model.R;
import top.continew.starter.web.util.MessageSourceUtils;
/**
* 全局异常处理器
@@ -49,6 +55,8 @@ public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
private static final String PARAM_FAILED = "请求地址 [{}],参数验证失败。";
@Resource
private I18nProperties i18nProperties;
/**
* 拦截自定义验证异常-错误请求
@@ -153,6 +161,23 @@ public class GlobalExceptionHandler {
return R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
}
/**
* 拦截全局应用异常
*/
@ExceptionHandler(GlobalException.class)
public R<Void> handleGlobalException(GlobalException e, HttpServletRequest request) {
log.error("请求地址 [{}],发生业务异常。", request.getRequestURI(), e);
ResultInfoInterface resultInfo = e.getResultInfo();
// 未开启,直接返回
if (!i18nProperties.getEnabled()) {
return R.fail(resultInfo.getCode(), resultInfo.getDefaultMessage());
}
// 以用户自定的messageKey优先否则枚举当messageKey
String messageKey = StrUtil.blankToDefault(resultInfo.getMessageKey(), resultInfo.toString());
String message = MessageSourceUtils.getMessage(messageKey, resultInfo.getDefaultMessage());
return R.fail(resultInfo.getCode(), message);
}
/**
* 拦截未知的运行时异常
*/

View File

@@ -26,10 +26,12 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory;
import top.continew.starter.web.autoconfigure.i18n.I18nProperties;
/**
* 全局异常处理器自动配置
@@ -40,6 +42,7 @@ import org.springframework.validation.beanvalidation.SpringConstraintValidatorFa
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(BasicErrorController.class)
@Import({GlobalExceptionHandler.class, GlobalErrorHandler.class})
@EnableConfigurationProperties(I18nProperties.class)
public class GlobalExceptionHandlerAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerAutoConfiguration.class);

View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.starter.web.autoconfigure.i18n;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.continew.starter.core.constant.PropertiesConstants;
/**
* 国际化 配置属性
*
* @author Jasmine
* @since 2.2.0
*/
@ConfigurationProperties(prefix = PropertiesConstants.I18N)
public class I18nProperties {
/**
* 国际化开启 true-开启, false-关闭
*/
private Boolean enabled;
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.starter.web.util;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
/**
* @author Jasmine
* @since 2.2.0
*/
public class MessageSourceUtils {
private static final MessageSource messageSource = SpringUtil.getBean(MessageSource.class);
private static final Object[] emptyArray = new Object[] {};
public static String getMessage(String key) {
return getMessage(key, emptyArray);
}
public static String getMessage(String key, String defaultMessage) {
return getMessage(key, defaultMessage, emptyArray);
}
public static String getMessage(String msgKey, Object... args) {
return getMessage(msgKey, msgKey, args);
}
public static String getMessage(String msgKey, String defaultMessage, Object... args) {
try {
return messageSource.getMessage(msgKey, args, LocaleContextHolder.getLocale());
} catch (Exception e) {
return defaultMessage;
}
}
}