新增全局异常处理

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

27
core/pom.xml Normal file
View 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>

View File

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

View 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);
}
}

View 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;
}
}

View 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);
}
}

View 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);
}
}

View File

@@ -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);
}
}

View File

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

View File

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