This commit is contained in:
六如
2024-12-30 20:49:17 +08:00
parent 40865ab049
commit 3e900cf0fe
27 changed files with 2232 additions and 117 deletions

View File

@@ -4,39 +4,52 @@ import com.gitee.sop.support.message.I18nMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Objects;
/**
* @author 六如
*/
@Getter
@AllArgsConstructor
public enum CodeEnum implements I18nMessage {
SUCCESS("0"),
SUCCESS("0", "成功"),
/**
* 认证异常
*/
AUTH("20001"),
AUTH("20001", "认证异常"),
/**
* 缺少参数
*/
MISSING("40001"),
MISSING("40001", "缺少参数"),
/**
* 错误参数
*/
INVALID("40002"),
INVALID("40002", "错误参数"),
/**
* 业务异常
*/
BIZ("50003"),
BIZ("50003", "业务异常"),
/**
* 权限异常
*/
ISV_PERM("40006"),
ISV_PERM("40006", "权限异常"),
/**
* 未知异常
*/
UNKNOWN("99999");
UNKNOWN("99999", "未知异常");
private final String configKey;
private final String configValue;
public static CodeEnum of(String code) {
for (CodeEnum value : CodeEnum.values()) {
if (Objects.equals(value.configKey, code)) {
return value;
}
}
return UNKNOWN;
}
public String getCode() {
return configKey;

View File

@@ -0,0 +1,79 @@
package com.gitee.sop.gateway;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.gitee.sop.gateway.message.CodeEnum;
import com.gitee.sop.gateway.message.ErrorEnum;
import com.gitee.sop.support.message.OpenMessageFactory;
import lombok.Data;
import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author 六如
*/
public class ErrorCodePrintTest {
@Test
public void print() {
OpenMessageFactory.initMessage();
ErrorEnum[] values = ErrorEnum.values();
Map<String, List<ErrorEnum>> codeMap = Stream.of(values)
.collect(Collectors.groupingBy(ErrorEnum::getCode));
AtomicInteger id = new AtomicInteger();
List<ErrorInfo> errorList = codeMap.entrySet()
.stream()
.map(entry -> {
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setId(id.incrementAndGet());
errorInfo.setCode(entry.getKey());
errorInfo.setMsg(CodeEnum.of(entry.getKey()).getConfigValue());
List<SubError> collect = entry.getValue()
.stream()
.filter(errorEnum -> StringUtils.hasText(errorEnum.getSubCode()))
.map(errorEnum -> {
SubError subError = new SubError();
subError.setId(id.incrementAndGet());
subError.setSub_code(errorEnum.getSubCode());
subError.setSub_msg(errorEnum.getError(Locale.CHINESE).getSubMsg());
subError.setSolution("");
return subError;
})
.collect(Collectors.toList());
errorInfo.setChildren(collect);
return errorInfo;
})
.collect(Collectors.toList());
System.out.println(JSON.toJSONString(errorList, JSONWriter.Feature.PrettyFormat));
}
@Data
static class ErrorInfo {
private Integer id;
private String code;
private String msg;
private List<SubError> children;
}
@Data
static class SubError {
private Integer id;
private String sub_code;
private String sub_msg;
private String solution;
}
}