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

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