自动注入全局异常处理

This commit is contained in:
jiangdingxuan 2024-01-08 12:04:39 +08:00
parent 66898b71b2
commit 42d738b0b6
4 changed files with 15 additions and 8 deletions

View File

@ -26,22 +26,18 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId> <artifactId>spring-boot-starter-validation</artifactId>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.dev33</groupId> <groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId> <artifactId>sa-token-core</artifactId>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

View File

@ -12,10 +12,12 @@ import net.rzdata.exception.ClientException;
import net.rzdata.exception.ServerException; import net.rzdata.exception.ServerException;
import net.rzdata.exception.ThirdPartyException; import net.rzdata.exception.ThirdPartyException;
import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -31,6 +33,7 @@ public class GlobalExceptionHandler {
* 权限码异常 * 权限码异常
*/ */
@ExceptionHandler(NotPermissionException.class) @ExceptionHandler(NotPermissionException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public Result<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) { public Result<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage()); log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
@ -42,6 +45,7 @@ public class GlobalExceptionHandler {
* 角色权限异常 * 角色权限异常
*/ */
@ExceptionHandler(NotRoleException.class) @ExceptionHandler(NotRoleException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public Result<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) { public Result<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) {
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage()); log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
@ -53,6 +57,7 @@ public class GlobalExceptionHandler {
* 认证失败 * 认证失败
*/ */
@ExceptionHandler(NotLoginException.class) @ExceptionHandler(NotLoginException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public Result<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) { public Result<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage()); log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
@ -64,6 +69,7 @@ public class GlobalExceptionHandler {
* 请求方式不支持 * 请求方式不支持
*/ */
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { public Result<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
@ -72,18 +78,21 @@ public class GlobalExceptionHandler {
} }
@ExceptionHandler(ClientException.class) @ExceptionHandler(ClientException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Result<Void> handleClientException(ClientException e) { public Result<Void> handleClientException(ClientException e) {
log.error("客户端异常: {}", e.getMessage(), e); log.error("客户端异常: {}", e.getMessage(), e);
return Result.fail(e); return Result.fail(e);
} }
@ExceptionHandler(ServerException.class) @ExceptionHandler(ServerException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<Void> handleServerException(ServerException e) { public Result<Void> handleServerException(ServerException e) {
log.error("服务端异常: {}", e.getMessage(), e); log.error("服务端异常: {}", e.getMessage(), e);
return Result.fail(e, errorMessage); return Result.fail(e, errorMessage);
} }
@ExceptionHandler(ThirdPartyException.class) @ExceptionHandler(ThirdPartyException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<Void> handleThirdPartyException(ThirdPartyException e) { public Result<Void> handleThirdPartyException(ThirdPartyException e) {
log.error("第三方服务异常: {}", e.getMessage(), e); log.error("第三方服务异常: {}", e.getMessage(), e);
return Result.fail(e, errorMessage); return Result.fail(e, errorMessage);
@ -93,6 +102,7 @@ public class GlobalExceptionHandler {
* 拦截未知的运行时异常 * 拦截未知的运行时异常
*/ */
@ExceptionHandler(RuntimeException.class) @ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<String> handleRuntimeException(RuntimeException e, HttpServletRequest request) { public Result<String> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生未知异常.", requestURI, e); log.error("请求地址'{}',发生未知异常.", requestURI, e);
@ -104,6 +114,7 @@ public class GlobalExceptionHandler {
* 系统异常 * 系统异常
*/ */
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<String> handleException(Exception e, HttpServletRequest request) { public Result<String> handleException(Exception e, HttpServletRequest request) {
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生系统异常.", requestURI, e); log.error("请求地址'{}',发生系统异常.", requestURI, e);
@ -115,6 +126,7 @@ public class GlobalExceptionHandler {
* 自定义验证异常 * 自定义验证异常
*/ */
@ExceptionHandler(BindException.class) @ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Result<Void> handleBindException(BindException e) { public Result<Void> handleBindException(BindException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
String message = e.getAllErrors().parallelStream() String message = e.getAllErrors().parallelStream()
@ -128,6 +140,7 @@ public class GlobalExceptionHandler {
* 自定义验证异常 * 自定义验证异常
*/ */
@ExceptionHandler(ConstraintViolationException.class) @ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Result<Void> constraintViolationException(ConstraintViolationException e) { public Result<Void> constraintViolationException(ConstraintViolationException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
String message = e.getConstraintViolations().parallelStream() String message = e.getConstraintViolations().parallelStream()
@ -141,6 +154,7 @@ public class GlobalExceptionHandler {
* 自定义验证异常 * 自定义验证异常
*/ */
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Result<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { public Result<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
String message = e.getBindingResult().getFieldError().getDefaultMessage(); String message = e.getBindingResult().getFieldError().getDefaultMessage();

View File

@ -0,0 +1 @@
net.rzdata.demo.exception.GlobalExceptionHandler

View File

@ -67,10 +67,6 @@
<groupId>org.mapstruct</groupId> <groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId> <artifactId>mapstruct</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>