Merge pull request '新增全局异常处理' (#2) from develop into main
Reviewed-on: #2
This commit is contained in:
commit
95ca20b2a0
@ -18,9 +18,31 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.rzdata</groupId>
|
||||
<artifactId>demo-core</artifactId>
|
||||
<version>5.1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -0,0 +1,164 @@
|
||||
package net.rzdata.demo.exception;
|
||||
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.exception.NotPermissionException;
|
||||
import cn.dev33.satoken.exception.NotRoleException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.rzdata.domain.Result;
|
||||
import net.rzdata.exception.ClientException;
|
||||
import net.rzdata.exception.ServerException;
|
||||
import net.rzdata.exception.ThirdPartyException;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 全局异常处理
|
||||
*/
|
||||
@Slf4j
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
protected final transient String errorMessage = "系统错误,请联系系统管理员";
|
||||
|
||||
/**
|
||||
* 权限码异常
|
||||
*/
|
||||
@ExceptionHandler(NotPermissionException.class)
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
public Result<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
|
||||
ClientException ce = new ClientException("没有访问权限,请联系管理员授权", e);
|
||||
return Result.fail(ce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色权限异常
|
||||
*/
|
||||
@ExceptionHandler(NotRoleException.class)
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
public Result<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
|
||||
ClientException ce = new ClientException("没有访问权限,请联系管理员授权", e);
|
||||
return Result.fail(ce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 认证失败
|
||||
*/
|
||||
@ExceptionHandler(NotLoginException.class)
|
||||
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
||||
public Result<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
|
||||
ClientException ce = new ClientException("没有访问权限,请联系管理员授权", e);
|
||||
return Result.fail(ce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求方式不支持
|
||||
*/
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public Result<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
|
||||
ServerException se = new ServerException("请求方式不支持", e);
|
||||
return Result.fail(se, errorMessage);
|
||||
}
|
||||
|
||||
@ExceptionHandler(ClientException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Result<Void> handleClientException(ClientException e) {
|
||||
log.error("客户端异常: {}", e.getMessage(), e);
|
||||
return Result.fail(e);
|
||||
}
|
||||
|
||||
@ExceptionHandler(ServerException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public Result<Void> handleServerException(ServerException e) {
|
||||
log.error("服务端异常: {}", e.getMessage(), e);
|
||||
return Result.fail(e, errorMessage);
|
||||
}
|
||||
|
||||
@ExceptionHandler(ThirdPartyException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public Result<Void> handleThirdPartyException(ThirdPartyException e) {
|
||||
log.error("第三方服务异常: {}", e.getMessage(), e);
|
||||
return Result.fail(e, errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截未知的运行时异常
|
||||
*/
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public Result<String> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("请求地址'{}',发生未知异常.", requestURI, e);
|
||||
ServerException se = new ServerException(errorMessage, e);
|
||||
return Result.fail(se);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统异常
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public Result<String> handleException(Exception e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("请求地址'{}',发生系统异常.", requestURI, e);
|
||||
ServerException se = new ServerException(errorMessage, e);
|
||||
return Result.fail(se);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义验证异常
|
||||
*/
|
||||
@ExceptionHandler(BindException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Result<Void> handleBindException(BindException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String message = e.getAllErrors().parallelStream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.joining(", "));
|
||||
ClientException ce = new ClientException(message, e);
|
||||
return Result.fail(ce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义验证异常
|
||||
*/
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Result<Void> constraintViolationException(ConstraintViolationException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String message = e.getConstraintViolations().parallelStream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.joining(", "));
|
||||
ClientException ce = new ClientException(message, e);
|
||||
return Result.fail(ce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义验证异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Result<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
||||
ClientException ce = new ClientException(message, e);
|
||||
return Result.fail(ce);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
net.rzdata.demo.exception.GlobalExceptionHandler
|
27
core/pom.xml
Normal file
27
core/pom.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.rzdata</groupId>
|
||||
<artifactId>demo</artifactId>
|
||||
<version>5.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>demo-core</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -1,4 +1,4 @@
|
||||
package net.rzdata.demo.domain;
|
||||
package net.rzdata.domain;
|
||||
|
||||
public record Id(String id) {
|
||||
|
42
core/src/main/java/net/rzdata/domain/Result.java
Normal file
42
core/src/main/java/net/rzdata/domain/Result.java
Normal file
@ -0,0 +1,42 @@
|
||||
package net.rzdata.domain;
|
||||
|
||||
import lombok.*;
|
||||
import net.rzdata.exception.BaseException;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class Result<T> {
|
||||
|
||||
public static final String SUCCESS_CODE = "00000";
|
||||
public static final String SUCCESS_MESSAGE = "操作成功";
|
||||
|
||||
private String code;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
public static <T> Result<T> ok() {
|
||||
return new Result<>(SUCCESS_CODE, SUCCESS_MESSAGE, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> ok(T data) {
|
||||
return new Result<>(SUCCESS_CODE, SUCCESS_MESSAGE, data);
|
||||
}
|
||||
|
||||
public static <T> Result<T> fail(BaseException e) {
|
||||
return new Result<>(e.getCode(), e.getMessage(), null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> fail(BaseException e, T data) {
|
||||
return new Result<>(e.getCode(), e.getMessage(), data);
|
||||
}
|
||||
|
||||
public static <T> Result<T> fail(BaseException e, String message) {
|
||||
return new Result<>(e.getCode(), message, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> fail(BaseException e, String message, T data) {
|
||||
return new Result<>(e.getCode(), message, data);
|
||||
}
|
||||
}
|
21
core/src/main/java/net/rzdata/exception/BaseException.java
Normal file
21
core/src/main/java/net/rzdata/exception/BaseException.java
Normal file
@ -0,0 +1,21 @@
|
||||
package net.rzdata.exception;
|
||||
|
||||
public abstract sealed class BaseException extends RuntimeException
|
||||
permits ClientException, ServerException, ThirdPartyException {
|
||||
|
||||
private final String code;
|
||||
|
||||
protected BaseException(String code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
protected BaseException(String code, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
}
|
25
core/src/main/java/net/rzdata/exception/ClientException.java
Normal file
25
core/src/main/java/net/rzdata/exception/ClientException.java
Normal file
@ -0,0 +1,25 @@
|
||||
package net.rzdata.exception;
|
||||
|
||||
/**
|
||||
* 客户端异常
|
||||
*/
|
||||
public non-sealed class ClientException extends BaseException {
|
||||
|
||||
public static final String CLIENT_ERROR_CODE = "A0001";
|
||||
|
||||
public ClientException(String message) {
|
||||
super(CLIENT_ERROR_CODE, message);
|
||||
}
|
||||
|
||||
public ClientException(String message, Throwable cause) {
|
||||
super(CLIENT_ERROR_CODE, message, cause);
|
||||
}
|
||||
|
||||
protected ClientException(String code, String message) {
|
||||
super(code, message);
|
||||
}
|
||||
|
||||
protected ClientException(String code, String message, Throwable cause) {
|
||||
super(code, message, cause);
|
||||
}
|
||||
}
|
25
core/src/main/java/net/rzdata/exception/ServerException.java
Normal file
25
core/src/main/java/net/rzdata/exception/ServerException.java
Normal file
@ -0,0 +1,25 @@
|
||||
package net.rzdata.exception;
|
||||
|
||||
/**
|
||||
* 服务端异常
|
||||
*/
|
||||
public non-sealed class ServerException extends BaseException {
|
||||
|
||||
public static final String SERVER_ERROR_CODE = "B0001";
|
||||
|
||||
public ServerException(String message) {
|
||||
super(SERVER_ERROR_CODE, message);
|
||||
}
|
||||
|
||||
public ServerException(String message, Throwable cause) {
|
||||
super(SERVER_ERROR_CODE, message, cause);
|
||||
}
|
||||
|
||||
protected ServerException(String code, String message) {
|
||||
super(code, message);
|
||||
}
|
||||
|
||||
protected ServerException(String code, String message, Throwable cause) {
|
||||
super(code, message, cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package net.rzdata.exception;
|
||||
|
||||
/**
|
||||
* 第三方服务异常
|
||||
*/
|
||||
public non-sealed class ThirdPartyException extends BaseException {
|
||||
|
||||
public static final String THIRD_PARTY_ERROR_CODE = "C0001";
|
||||
|
||||
public ThirdPartyException(String message) {
|
||||
super(THIRD_PARTY_ERROR_CODE, message);
|
||||
}
|
||||
|
||||
public ThirdPartyException(String message, Throwable cause) {
|
||||
super(THIRD_PARTY_ERROR_CODE, message, cause);
|
||||
}
|
||||
|
||||
protected ThirdPartyException(String code, String message) {
|
||||
super(code, message);
|
||||
}
|
||||
|
||||
protected ThirdPartyException(String code, String message, Throwable cause) {
|
||||
super(code, message, cause);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package net.rzdata.demo.trait;
|
||||
package net.rzdata.trait;
|
||||
|
||||
/**
|
||||
* 实体转换类
|
@ -1,4 +1,4 @@
|
||||
package net.rzdata.demo.trait;
|
||||
package net.rzdata.trait;
|
||||
|
||||
/**
|
||||
* 请求实体类
|
1
pom.xml
1
pom.xml
@ -16,6 +16,7 @@
|
||||
<modules>
|
||||
<module>service</module>
|
||||
<module>common</module>
|
||||
<module>core</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
@ -67,10 +67,6 @@
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
Loading…
x
Reference in New Issue
Block a user