TASK: 全局异常处理,代码优化,测试数据库连接信息变更;

This commit is contained in:
shahaibo
2023-10-27 10:09:19 +08:00
parent 9fa605f83e
commit bedc640e13
27 changed files with 377 additions and 242 deletions

View File

@@ -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; // 如果找不到对应的资源文件,返回原始的消息
}
}

View File

@@ -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());
}
//初始化,不可预知异常自定义错误编码

View File

@@ -1,31 +1,31 @@
package com.ai.da.common.enums;
import java.util.stream.Stream;
/**
* @author: yanglei
* @description: 操作类型 登入 忘记密码
* @create: 2022-8-10 17:33
**/
public enum OperationTypeEnum {
/**
* 登入
*/
LOGIN,
/**
* 异常ip
*/
EXCEPTION_IP,
/**
* 绑定邮箱
*/
BIND_MAILBOX,
/**
* 忘记密码
*/
FORGET_PWD;
public static OperationTypeEnum of(String name) {
return Stream.of(OperationTypeEnum.values()).filter(v -> v.name().equals(name)).findFirst().orElse(null);
}
}
package com.ai.da.common.enums;
import java.util.stream.Stream;
/**
* @author: yanglei
* @description: 操作类型 登入 忘记密码
* @create: 2022-8-10 17:33
**/
public enum AuthenticationOperationTypeEnum {
/**
* 登入
*/
LOGIN,
/**
* 异常ip
*/
EXCEPTION_IP,
/**
* 绑定邮箱
*/
BIND_MAILBOX,
/**
* 忘记密码
*/
FORGET_PWD;
public static AuthenticationOperationTypeEnum of(String name) {
return Stream.of(AuthenticationOperationTypeEnum.values()).filter(v -> v.name().equals(name)).findFirst().orElse(null);
}
}

View File

@@ -28,6 +28,8 @@ public enum SwitchCategoryEnum {
* 裤子
*/
TROUSERS("Trousers"),
TOPS("Tops"),
BOTTOMS("Bottoms"),
;
private String realName;

View File

@@ -11,6 +11,7 @@ import com.tencentcloudapi.ses.v20201002.models.SendEmailRequest;
import com.tencentcloudapi.ses.v20201002.models.SendEmailResponse;
import com.tencentcloudapi.ses.v20201002.models.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import java.util.Date;
@@ -96,7 +97,7 @@ public class SendEmailUtil {
return Boolean.TRUE;
} catch (TencentCloudSDKException e) {
log.info("邮件发送失败###{}", e.toString());
throw new BusinessException(e.getMessage());
throw new BusinessException("failed.to.send.mail");
}
}