新增全局异常处理

This commit is contained in:
jiangdingxuan
2024-01-08 11:44:13 +08:00
parent 31b4b91bc6
commit 66898b71b2
12 changed files with 345 additions and 3 deletions

View File

@@ -18,9 +18,35 @@
</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>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,8 +0,0 @@
package net.rzdata.demo.domain;
public record Id(String id) {
public static Id of(String id) {
return new Id(id);
}
}

View File

@@ -0,0 +1,150 @@
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.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import java.util.stream.Collectors;
/**
* 全局异常处理
*/
@Slf4j
public class GlobalExceptionHandler {
protected final transient String errorMessage = "系统错误,请联系系统管理员";
/**
* 权限码异常
*/
@ExceptionHandler(NotPermissionException.class)
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)
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)
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)
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)
public Result<Void> handleClientException(ClientException e) {
log.error("客户端异常: {}", e.getMessage(), e);
return Result.fail(e);
}
@ExceptionHandler(ServerException.class)
public Result<Void> handleServerException(ServerException e) {
log.error("服务端异常: {}", e.getMessage(), e);
return Result.fail(e, errorMessage);
}
@ExceptionHandler(ThirdPartyException.class)
public Result<Void> handleThirdPartyException(ThirdPartyException e) {
log.error("第三方服务异常: {}", e.getMessage(), e);
return Result.fail(e, errorMessage);
}
/**
* 拦截未知的运行时异常
*/
@ExceptionHandler(RuntimeException.class)
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)
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)
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)
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)
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);
}
}

View File

@@ -1,11 +0,0 @@
package net.rzdata.demo.trait;
/**
* 实体转换类
* @param <S> 源类型
* @param <T> 目标类型
*/
public interface IConverter<S, T> {
T convert(S source);
}

View File

@@ -1,10 +0,0 @@
package net.rzdata.demo.trait;
/**
* 请求实体类
* @param <T> 核心模型类型
*/
public interface IQuery<T> {
T into();
}