TASK: 全局异常处理,代码优化,测试数据库连接信息变更;
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
package com.ai.da.common.config.exception;
|
||||
|
||||
import com.ai.da.common.context.UserContext;
|
||||
import com.ai.da.common.response.ResultEnum;
|
||||
import com.ai.da.model.vo.AuthPrincipalVo;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.PropertyResourceBundle;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* @author: dangweijian
|
||||
* @description: 业务异常
|
||||
@@ -15,14 +25,44 @@ public class BusinessException extends RuntimeException {
|
||||
private String msg;
|
||||
|
||||
public BusinessException(ResultEnum resultEnum) {
|
||||
super(resultEnum.getMsg());
|
||||
this.code = resultEnum.getCode();
|
||||
this.msg = resultEnum.getMsg();
|
||||
this.msg = getMessageFromResource(resultEnum.getMsg(), getUserLocale());
|
||||
}
|
||||
|
||||
public BusinessException(String msg) {
|
||||
super(msg);
|
||||
this.code = ResultEnum.FAIL.getCode();
|
||||
this.msg = msg;
|
||||
this.msg = getMessageFromResource(msg, getUserLocale());
|
||||
}
|
||||
}
|
||||
|
||||
public BusinessException(String msg, Integer code) {
|
||||
this.code = code;
|
||||
this.msg = getMessageFromResource(msg, getUserLocale());
|
||||
}
|
||||
|
||||
public BusinessException(Throwable cause) {
|
||||
this.code = ResultEnum.FAIL.getCode();
|
||||
this.msg = getMessageFromResource(cause.getMessage(), getUserLocale());
|
||||
}
|
||||
|
||||
private static Locale getUserLocale() {
|
||||
AuthPrincipalVo userInfo = UserContext.getUserHolder();
|
||||
if (Objects.isNull(userInfo)) {
|
||||
return new Locale("en");
|
||||
}
|
||||
return new Locale(userInfo.getLanguage());
|
||||
}
|
||||
|
||||
private static String getMessageFromResource(String msg, Locale userLocale) {
|
||||
try (InputStream inputStream = BusinessException.class.getClassLoader().getResourceAsStream("messages_" + userLocale.getLanguage() + ".properties")) {
|
||||
if (inputStream != null) {
|
||||
ResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
|
||||
if (bundle.containsKey(msg)) {
|
||||
return bundle.getString(msg);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return msg; // 如果找不到对应的资源文件,返回原始的消息
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,8 @@ public class ExceptionCatch {
|
||||
@ExceptionHandler(BindException.class)
|
||||
public Response<String> bindExceptionHandler(BindException e) {
|
||||
log.error("参数错误bind:{}", e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
|
||||
return Response.fail(ResultEnum.FAIL.getCode(), e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
|
||||
BusinessException businessException = new BusinessException(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
|
||||
return Response.error(businessException.getCode(), businessException.getMsg());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,9 +76,10 @@ public class ExceptionCatch {
|
||||
*/
|
||||
@ResponseBody
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public Response<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
|
||||
public Response<String> handleValidationException(MethodArgumentNotValidException e) {
|
||||
log.error("参数错误bind:{}", e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
|
||||
return Response.fail(ResultEnum.FAIL.getCode(), e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
|
||||
BusinessException businessException = new BusinessException(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
|
||||
return Response.error(businessException.getCode(), businessException.getMsg());
|
||||
}
|
||||
|
||||
//初始化,不可预知异常自定义错误编码
|
||||
|
||||
Reference in New Issue
Block a user