Skip to content

Commit 8580a8d

Browse files
committed
feat: 添加 common 错误枚举
1 parent e33ff8b commit 8580a8d

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package top.cadecode.uniboot.common.core.enums;
2+
3+
import lombok.Getter;
4+
import top.cadecode.uniboot.common.core.exception.UniErrorCode;
5+
6+
/**
7+
* Common 模块错误码枚举
8+
*
9+
* @author Cade Li
10+
* @date 2022/5/8
11+
*/
12+
@Getter
13+
public enum CommonErrorEnum implements UniErrorCode {
14+
15+
// json
16+
CAST_BEAN_TO_JSON_FAIL(0, "cast bean to json fail"),
17+
CAST_JSON_TO_BEAN_FAIL(0, "cast json to bean fail"),
18+
;
19+
20+
private final String code;
21+
22+
private final String message;
23+
24+
CommonErrorEnum(int code, String message) {
25+
this.code = "COMMON_" + code;
26+
this.message = message;
27+
}
28+
}

common/core/src/main/java/top/cadecode/uniboot/common/core/util/JacksonUtil.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.springframework.beans.factory.InitializingBean;
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.stereotype.Component;
14+
import top.cadecode.uniboot.common.core.enums.CommonErrorEnum;
15+
import top.cadecode.uniboot.common.core.exception.UniException;
1416

1517
import java.io.IOException;
1618
import java.text.SimpleDateFormat;
@@ -68,7 +70,7 @@ public static String toJson(Object bean, boolean isPretty) {
6870
}
6971
return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
7072
} catch (JsonProcessingException e) {
71-
throw new RuntimeException("cast bean to json fail", e);
73+
throw UniException.of(CommonErrorEnum.CAST_BEAN_TO_JSON_FAIL, e);
7274
}
7375
}
7476

@@ -90,7 +92,7 @@ public static <T> T toBean(String json, Class<T> clazz) {
9092
try {
9193
return OBJECT_MAPPER.readValue(json, clazz);
9294
} catch (Exception e) {
93-
throw new RuntimeException("cast json to bean fail", e);
95+
throw UniException.of(CommonErrorEnum.CAST_JSON_TO_BEAN_FAIL, e);
9496
}
9597
}
9698

@@ -112,7 +114,7 @@ public static <T> T toBean(String json, TypeReference<T> typeReference) {
112114
try {
113115
return OBJECT_MAPPER.readValue(json, typeReference);
114116
} catch (IOException e) {
115-
throw new RuntimeException("cast json to bean fail", e);
117+
throw UniException.of(CommonErrorEnum.CAST_JSON_TO_BEAN_FAIL, e);
116118
}
117119
}
118120

framework/src/main/java/top/cadecode/uniboot/framework/enums/FrameErrorEnum.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
@Getter
1414
public enum FrameErrorEnum implements UniErrorCode {
1515

16+
// web mvc
1617
VALIDATED_ERROR(1, "参数校验不通过") {
1718
@Override
1819
public int getStatus() {
@@ -49,6 +50,8 @@ public int getStatus() {
4950
return ApiStatus.TOO_MANY_REQUESTS;
5051
}
5152
},
53+
54+
// file
5255
EXTENSION_NOT_ALLOWED(6, "上传或下载文件的类型不被允许"),
5356
UPLOAD_FILE_FAIL(7, "上传文件失败"),
5457
FILE_NOT_FOUND(8, "文件未找到"),

0 commit comments

Comments
 (0)