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-03 22:53:02 +08:00
|
|
|
import io.swagger.v3.oas.annotations.ExternalDocumentation;
|
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;
|
|
|
|
|
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-03 09:13:22 +08:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
2024-01-02 10:16:15 +08:00
|
|
|
@RestControllerAdvice
|
|
|
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
2024-01-03 09:13:22 +08:00
|
|
|
@Order(3)
|
|
|
|
|
@ExceptionHandler(value = Exception.class)
|
2024-01-02 10:16:15 +08:00
|
|
|
public ResponseResult handleGlobalException(Exception e) {
|
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)
|
|
|
|
|
@ExceptionHandler(value = NotLoginException.class)
|
|
|
|
|
public ResponseResult handleNotLoginException(NotLoginException e) {
|
|
|
|
|
return new ResponseResult(
|
2024-01-03 22:53:02 +08:00
|
|
|
401,
|
2024-01-03 09:13:22 +08:00
|
|
|
e.getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-01-02 10:16:15 +08:00
|
|
|
|
2024-01-03 09:13:22 +08:00
|
|
|
@Order(2)
|
|
|
|
|
@ExceptionHandler(value = PersistenceException.class)
|
2024-01-03 22:53:02 +08:00
|
|
|
public ResponseResult handleSQLException(PersistenceException e) {
|
|
|
|
|
return ResponseResult.invalid().setMessage(
|
2024-01-03 09:13:22 +08:00
|
|
|
"please check the integrity of the data. check if the json data exists in the database");
|
|
|
|
|
}
|
2024-01-02 10:16:15 +08:00
|
|
|
|
2024-01-03 09:13:22 +08:00
|
|
|
@Order(2)
|
|
|
|
|
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
|
|
|
|
public ResponseResult handleBindException(MethodArgumentNotValidException e) {
|
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
|
|
|
|
|
|
|
|
@Order(2)
|
|
|
|
|
@ExceptionHandler(value = SaTokenException.class)
|
|
|
|
|
public ResponseResult handleSaTokenException(SaTokenException e) {
|
|
|
|
|
return ResponseResult.unAuthorized().setMessage(e.getMessage());
|
|
|
|
|
}
|
2024-01-02 10:16:15 +08:00
|
|
|
}
|