2024-01-02 10:16:15 +08:00
|
|
|
|
package com.realtime.protection.configuration.exception;
|
|
|
|
|
|
|
2024-01-03 09:13:22 +08:00
|
|
|
|
import cn.dev33.satoken.exception.NotLoginException;
|
2024-01-03 22:53:02 +08:00
|
|
|
|
import cn.dev33.satoken.exception.SaTokenException;
|
2024-01-02 10:16:15 +08:00
|
|
|
|
import com.realtime.protection.configuration.response.ResponseResult;
|
2024-01-11 19:49:07 +08:00
|
|
|
|
import com.realtime.protection.configuration.utils.enums.StateEnum;
|
|
|
|
|
|
import com.realtime.protection.server.task.status.StateChangeService;
|
2024-05-11 16:47:42 +08:00
|
|
|
|
import io.lettuce.core.RedisConnectionException;
|
2024-01-12 14:31:34 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
import org.apache.ibatis.exceptions.PersistenceException;
|
|
|
|
|
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
|
|
|
|
import org.springframework.core.annotation.Order;
|
2024-01-12 19:24:19 +08:00
|
|
|
|
import org.springframework.dao.DuplicateKeyException;
|
2024-05-11 16:47:42 +08:00
|
|
|
|
import org.springframework.data.redis.RedisConnectionFailureException;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
2024-01-02 10:16:15 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
2024-01-05 09:32:19 +08:00
|
|
|
|
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
2024-01-02 10:16:15 +08:00
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
import java.sql.SQLIntegrityConstraintViolationException;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
2024-01-02 10:16:15 +08:00
|
|
|
|
@RestControllerAdvice
|
2024-01-12 14:31:34 +08:00
|
|
|
|
@Slf4j
|
2024-01-02 10:16:15 +08:00
|
|
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
|
2024-01-11 19:49:07 +08:00
|
|
|
|
private final StateChangeService stateChangeService;
|
|
|
|
|
|
|
|
|
|
|
|
public GlobalExceptionHandler(StateChangeService stateChangeService) {
|
|
|
|
|
|
this.stateChangeService = stateChangeService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-03 09:13:22 +08:00
|
|
|
|
@Order(3)
|
2024-01-12 19:24:19 +08:00
|
|
|
|
@ExceptionHandler(value = {Exception.class})
|
2024-01-02 10:16:15 +08:00
|
|
|
|
public ResponseResult handleGlobalException(Exception e) {
|
2024-06-08 21:03:05 +08:00
|
|
|
|
e.printStackTrace();
|
2024-04-17 14:01:46 +08:00
|
|
|
|
log.error("遭遇全局异常:{}", e.getMessage());
|
2024-01-03 09:13:22 +08:00
|
|
|
|
return ResponseResult.error().setMessage(e.getMessage());
|
|
|
|
|
|
}
|
2024-01-02 10:16:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-01-03 09:13:22 +08:00
|
|
|
|
@Order(2)
|
2024-01-15 20:40:55 +08:00
|
|
|
|
@ExceptionHandler(value = {
|
|
|
|
|
|
PersistenceException.class,
|
|
|
|
|
|
SQLException.class,
|
|
|
|
|
|
SQLIntegrityConstraintViolationException.class
|
|
|
|
|
|
})
|
2024-01-13 10:23:48 +08:00
|
|
|
|
public ResponseResult handleSQLException(Exception e) {
|
2024-04-17 14:01:46 +08:00
|
|
|
|
log.info("遭遇数据库异常:{}", e.getMessage());
|
2024-01-03 22:53:02 +08:00
|
|
|
|
return ResponseResult.invalid().setMessage(
|
2024-01-13 10:23:48 +08:00
|
|
|
|
"请检查json字段的完整性,确保json字段按照文档中要求填写。");
|
2024-01-12 19:24:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-22 15:40:03 +08:00
|
|
|
|
@Order(2)
|
|
|
|
|
|
@ExceptionHandler(value = DuplicateKeyException.class)
|
|
|
|
|
|
public ResponseResult handleDuplicateKeyException(DuplicateKeyException e) {
|
|
|
|
|
|
return ResponseResult.invalid().setMessage(
|
2024-06-08 21:03:05 +08:00
|
|
|
|
"插入/更新失败,请检查当前插入字段和数据库中是否存在相同数据 "+e.getMessage()
|
2024-01-22 15:40:03 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-03 09:13:22 +08:00
|
|
|
|
@Order(2)
|
|
|
|
|
|
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
|
|
|
|
|
public ResponseResult handleBindException(MethodArgumentNotValidException e) {
|
2024-01-13 10:23:48 +08:00
|
|
|
|
log.debug("遭遇数据绑定异常:" + e.getMessage());
|
2024-01-03 22:53:02 +08:00
|
|
|
|
return ResponseResult.invalid().setMessage(
|
2024-01-03 09:13:22 +08:00
|
|
|
|
e.getBindingResult().getAllErrors().stream()
|
|
|
|
|
|
.map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining())
|
|
|
|
|
|
);
|
2024-01-02 10:16:15 +08:00
|
|
|
|
}
|
2024-01-03 22:53:02 +08:00
|
|
|
|
|
2024-01-05 09:32:19 +08:00
|
|
|
|
@Order(2)
|
2024-01-11 19:49:07 +08:00
|
|
|
|
@ExceptionHandler(value = {
|
|
|
|
|
|
HandlerMethodValidationException.class,
|
|
|
|
|
|
IllegalArgumentException.class,
|
|
|
|
|
|
IllegalStateException.class
|
|
|
|
|
|
})
|
|
|
|
|
|
public ResponseResult handleHandlerMethodValidationException(Exception e) {
|
2024-04-17 14:01:46 +08:00
|
|
|
|
log.debug("遭遇非法参数异常:{}", e.getMessage());
|
2024-01-05 09:32:19 +08:00
|
|
|
|
return ResponseResult.invalid().setMessage(e.getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-11 19:49:07 +08:00
|
|
|
|
@Order(2)
|
|
|
|
|
|
@ExceptionHandler(value = NotLoginException.class)
|
|
|
|
|
|
public ResponseResult handleNotLoginException(NotLoginException e) {
|
2024-04-17 14:01:46 +08:00
|
|
|
|
log.debug("遭遇Sa-Token登录异常,登录类型为:{}", e.getLoginType());
|
2024-01-11 19:49:07 +08:00
|
|
|
|
return new ResponseResult(
|
|
|
|
|
|
401,
|
|
|
|
|
|
e.getMessage()
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-03 22:53:02 +08:00
|
|
|
|
@Order(2)
|
|
|
|
|
|
@ExceptionHandler(value = SaTokenException.class)
|
|
|
|
|
|
public ResponseResult handleSaTokenException(SaTokenException e) {
|
2024-04-17 14:01:46 +08:00
|
|
|
|
log.debug("Sa-token模块遭遇异常:{}", e.getMessage());
|
2024-01-03 22:53:02 +08:00
|
|
|
|
return ResponseResult.unAuthorized().setMessage(e.getMessage());
|
|
|
|
|
|
}
|
2024-01-11 19:49:07 +08:00
|
|
|
|
|
|
|
|
|
|
@Order(2)
|
|
|
|
|
|
@ExceptionHandler(value = DorisStartException.class)
|
|
|
|
|
|
public ResponseResult handleDorisStartException(DorisStartException e) {
|
2024-04-17 14:01:46 +08:00
|
|
|
|
log.warn("Doris数据库遭遇异常:{}", e.getMessage());
|
2024-01-11 19:49:07 +08:00
|
|
|
|
ResponseResult responseResult = ResponseResult.error()
|
2024-01-13 10:23:48 +08:00
|
|
|
|
.setMessage("Doris数据库指令生成遭遇异常:" + e.getMessage());
|
2024-01-11 19:49:07 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
2024-01-15 20:40:55 +08:00
|
|
|
|
// 内部修改状态,可以跳过一切状态检查
|
|
|
|
|
|
stateChangeService.changeState(StateEnum.FAILED.getStateNum(), e.taskId, true);
|
2024-01-11 19:49:07 +08:00
|
|
|
|
} catch (Exception another) {
|
|
|
|
|
|
responseResult.setAnother(ResponseResult.error().setMessage(e.getMessage()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 14:31:34 +08:00
|
|
|
|
log.error(responseResult.getMessage());
|
2024-01-11 19:49:07 +08:00
|
|
|
|
return responseResult;
|
|
|
|
|
|
}
|
2024-05-11 16:47:42 +08:00
|
|
|
|
|
|
|
|
|
|
@Order(2)
|
|
|
|
|
|
@ExceptionHandler(value = RedisConnectionFailureException.class)
|
|
|
|
|
|
public ResponseResult handleRedisConnectionException(RedisConnectionFailureException e) {
|
|
|
|
|
|
log.warn("Redis连接失败,具体原因:{}", e.getCause().getMessage());
|
|
|
|
|
|
return ResponseResult.error().setMessage("Redis连接失败");
|
|
|
|
|
|
}
|
2024-01-02 10:16:15 +08:00
|
|
|
|
}
|