This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
enderbyendera-realtime-prot…/src/main/java/com/realtime/protection/configuration/exception/GlobalExceptionHandler.java

101 lines
4.0 KiB
Java
Raw Normal View History

2024-01-02 10:16:15 +08:00
package com.realtime.protection.configuration.exception;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.SaTokenException;
2024-01-02 10:16:15 +08:00
import com.realtime.protection.configuration.response.ResponseResult;
import com.realtime.protection.configuration.utils.enums.StateEnum;
import com.realtime.protection.server.task.status.StateChangeService;
2024-01-12 14:31:34 +08:00
import lombok.extern.slf4j.Slf4j;
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;
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;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
2024-01-02 10:16:15 +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 {
private final StateChangeService stateChangeService;
public GlobalExceptionHandler(StateChangeService stateChangeService) {
this.stateChangeService = stateChangeService;
}
@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-01-13 10:23:48 +08:00
log.error("遭遇全局异常:" + e.getMessage());
return ResponseResult.error().setMessage(e.getMessage());
}
2024-01-02 10:16:15 +08:00
@Order(2)
2024-01-13 10:23:48 +08:00
@ExceptionHandler(value = {PersistenceException.class, DuplicateKeyException.class})
public ResponseResult handleSQLException(Exception e) {
log.info("遭遇数据库异常:" + e.getMessage());
return ResponseResult.invalid().setMessage(
2024-01-13 10:23:48 +08:00
"请检查json字段的完整性确保json字段按照文档中要求填写。");
2024-01-12 19:24:19 +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());
return ResponseResult.invalid().setMessage(
e.getBindingResult().getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining())
);
2024-01-02 10:16:15 +08:00
}
@Order(2)
@ExceptionHandler(value = {
HandlerMethodValidationException.class,
IllegalArgumentException.class,
IllegalStateException.class
})
public ResponseResult handleHandlerMethodValidationException(Exception e) {
2024-01-13 10:23:48 +08:00
log.debug("遭遇非法参数异常:" + e.getMessage());
return ResponseResult.invalid().setMessage(e.getMessage());
}
@Order(2)
@ExceptionHandler(value = NotLoginException.class)
public ResponseResult handleNotLoginException(NotLoginException e) {
2024-01-13 10:23:48 +08:00
log.debug("遭遇Sa-Token登录异常登录类型为" + e.getLoginType());
return new ResponseResult(
401,
e.getMessage()
);
}
@Order(2)
@ExceptionHandler(value = SaTokenException.class)
public ResponseResult handleSaTokenException(SaTokenException e) {
2024-01-13 10:23:48 +08:00
log.debug("Sa-token模块遭遇异常" + e.getMessage());
return ResponseResult.unAuthorized().setMessage(e.getMessage());
}
@Order(2)
@ExceptionHandler(value = DorisStartException.class)
public ResponseResult handleDorisStartException(DorisStartException e) {
2024-01-13 10:23:48 +08:00
log.warn("Doris数据库遭遇异常" + e.getMessage());
ResponseResult responseResult = ResponseResult.error()
2024-01-13 10:23:48 +08:00
.setMessage("Doris数据库指令生成遭遇异常" + e.getMessage());
try {
stateChangeService.changeState(StateEnum.FAILED.getStateNum(), e.taskId);
} catch (Exception another) {
responseResult.setAnother(ResponseResult.error().setMessage(e.getMessage()));
}
2024-01-12 14:31:34 +08:00
log.error(responseResult.getMessage());
return responseResult;
}
2024-01-02 10:16:15 +08:00
}