Skip to content

Commit

Permalink
feat: error code combing
Browse files Browse the repository at this point in the history
  • Loading branch information
CC11001100 committed Jun 30, 2023
1 parent bd04427 commit 3dff18e
Show file tree
Hide file tree
Showing 25 changed files with 147 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public Operation convert() {
try {
o.mergeParameters(this.parseParameters());
} catch (Throwable e) {
DongTaiLog.error("MethodConvertor.convert parseParameters error", e);
DongTaiLog.debug("MethodConvertor.convert parseParameters exception", e);
}

try {
o.setResponses(this.parseResponse());
} catch (Throwable e) {
DongTaiLog.error("MethodConvertor.convert parseResponse error", e);
DongTaiLog.debug("MethodConvertor.convert parseResponse exception", e);
}

// 设置这两个字段
Expand Down Expand Up @@ -100,7 +100,7 @@ private List<Parameter> parseParameters() {
parameterList.add(convert);
}
} catch (Throwable e) {
DongTaiLog.error("MethodConvertor.parseParameters ParameterConvertor error", e);
DongTaiLog.debug("MethodConvertor.parseParameters ParameterConvertor exception", e);
}
}
return parameterList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Map<String, Path> convert() {
path.setDubbo(convert);
pathMap.put(this.buildSign(parseServiceMethod), path);
} catch (Throwable e) {
DongTaiLog.error("ServiceConvertor.convert error", e);
DongTaiLog.debug("ServiceConvertor.convert exception", e);
}
}
return pathMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import io.dongtai.iast.api.openapi.domain.OpenApi;
import io.dongtai.iast.api.openapi.domain.Path;
import io.dongtai.log.DongTaiLog;
import io.dongtai.log.ErrorCode;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
* 两个dubbo分支Service收集共同的逻辑抽象到这里
Expand All @@ -24,6 +24,8 @@
*/
public abstract class AbstractDubboServiceExtractor {

private static final String DUBBO_PROTOCOL_NAME = "dubbo";

// 每个Gather共享同一个Manager
private OpenApiSchemaConvertorManager manager;

Expand All @@ -32,19 +34,19 @@ public AbstractDubboServiceExtractor() {
}

public OpenApi extract() {
Object protocolObject = this.getProtocol("dubbo");
Object protocolObject = this.getProtocol(DUBBO_PROTOCOL_NAME);
if (protocolObject == null) {
DongTaiLog.error("AbstractDubboServiceGather getProtocol null");
DongTaiLog.error(ErrorCode.API_GATHER_DUBBO_PROTOCOL_NULL);
return null;
}
Object exporterMap = this.getExporterMap(protocolObject);
if (exporterMap == null) {
DongTaiLog.error("AbstractDubboServiceGather getExporterMap null");
DongTaiLog.error(ErrorCode.API_GATHER_DUBBO_EXPORT_MAP_NULL);
return null;
}
List<Class> exportedServiceList = this.parseExportedServiceClassList(exporterMap);
if (exportedServiceList == null || exportedServiceList.isEmpty()) {
DongTaiLog.error("AbstractDubboServiceGather parseExportedServiceClassList empty");
DongTaiLog.error(ErrorCode.API_GATHER_DUBBO_EXPORT_LIST_EMPTY);
return null;
}

Expand All @@ -65,23 +67,20 @@ public OpenApi extract() {
}

/**
* 解析导出的类
* 解析dubbo协议导出的Service为Open API的Path
*
* @param exportedServiceList
* @return
*/
private Map<String, Path> parsePaths(List<Class> exportedServiceList) {
Map<String, Path> pathMap = new HashMap<>();
exportedServiceList.forEach(new Consumer<Class>() {
@Override
public void accept(Class aClass) {
try {
Map<String, Path> convert = new ServiceConvertor(manager, aClass).convert();
// 暂不考虑key覆盖的问题
pathMap.putAll(convert);
} catch (Throwable e) {
DongTaiLog.error("AbstractDubboServiceGather parsePaths error", e);
}
exportedServiceList.forEach(aClass -> {
try {
Map<String, Path> convert = new ServiceConvertor(manager, aClass).convert();
// 暂不考虑key覆盖的问题
pathMap.putAll(convert);
} catch (Throwable e) {
DongTaiLog.error(ErrorCode.API_GATHER_DUBBO_SERVICE_CONVERT_ERROR, e);
}
});
return pathMap;
Expand All @@ -95,17 +94,17 @@ public void accept(Class aClass) {
*/
private Object getProtocol(String protocolName) {
Object protocolObj = this.getProtocolObject(protocolName);
for (int i = 0; i < 10; i++) {
try {
try {
for (int i = 0; i < 10; i++) {
Field protocolField = protocolObj.getClass().getDeclaredField("protocol");
protocolField.setAccessible(true);
protocolObj = protocolField.get(protocolObj);
if (protocolObj.getClass() == this.exceptedProtocolClass()) {
break;
}
} catch (Throwable e) {
DongTaiLog.error("AbstractDubboServiceGather getProtocol error", e);
}
} catch (Throwable e) {
DongTaiLog.error(ErrorCode.API_GATHER_DUBBO_GET_PROTOCOL_ERROR, e);
}
return protocolObj;
}
Expand Down Expand Up @@ -137,7 +136,7 @@ private Object getExporterMap(Object protocolObject) {
exporterMapField.setAccessible(true);
return exporterMapField.get(protocolObject);
} catch (Throwable e) {
DongTaiLog.error("AbstractDubboServiceGather getExporterMap error", e);
DongTaiLog.error(ErrorCode.API_GATHER_DUBBO_GET_EXPORT_MAP_ERROR, e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol;
import io.dongtai.iast.api.openapi.domain.OpenApi;
import io.dongtai.log.DongTaiLog;
import io.dongtai.log.ErrorCode;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void accept(String s, Exporter exporter) {
}
});
} catch (Throwable e) {
DongTaiLog.error("AlibabaDubboServiceExtractor parseExportedServiceClassList error", e);
DongTaiLog.error(ErrorCode.API_GATHER_DUBBO_ALIBABA_PARSE_SERVICE_LIST_ERROR, e);
}
return serviceClassList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Schema convert(Class clazz) {

Class componentType = clazz.getComponentType();
if (componentType == null) {
return null;
return new Schema(DataType.ObjectArray());
}

// 如果是多层数组,则直接返回array,swagger spring也是这么处理的
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public BaseOpenApiSchemaConvertor(OpenApiSchemaConvertorManager manager) {

@Override
public boolean canConvert(Class clazz, Field field) {
// 在基类上把Field的转换复用一下Class的转换
return field != null && canConvert(field.getType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
public interface ClassOpenApiSchemaConvertor {

/**
* 转换器的名字,方便日志打印啥的
*
* @return
*/
String getConvertorName();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*/
public class CollectionOpenApiSchemaConvertor extends BaseOpenApiSchemaConvertor {

private final ListOpenApiSchemaConvertor listOpenApiSchemaConvertor;
private final SetOpenApiSchemaConvertor setOpenApiSchemaConvertor;
private final MapOpenApiSchemaConvertor mapOpenApiSchemaConvertor;
final ListOpenApiSchemaConvertor listOpenApiSchemaConvertor;
final SetOpenApiSchemaConvertor setOpenApiSchemaConvertor;
final MapOpenApiSchemaConvertor mapOpenApiSchemaConvertor;

public CollectionOpenApiSchemaConvertor(OpenApiSchemaConvertorManager manager) {
super(manager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ComponentDatabase {
// 类到Schema的映射
private Map<Class, Schema> classToSchemaMap;

// 已经发现了的类,用于避免重复处理
// 已经发现了的类,用于避免重复处理,也避免碰到循环引用时递归爆栈
private Set<Class> existsClassSet = new HashSet<>();

// 符合类型的schema生成完毕的时候的回调方法,用于处理环形依赖
Expand Down Expand Up @@ -72,12 +72,7 @@ public void triggerSchemaCallback(Class clazz, Schema c) {
if (consumers == null) {
return;
}
consumers.forEach(new Consumer<Consumer<Schema>>() {
@Override
public void accept(Consumer<Schema> componentConsumer) {
componentConsumer.accept(c);
}
});
consumers.forEach(x -> x.accept(c));
}

/**
Expand Down Expand Up @@ -130,18 +125,18 @@ public Map<String, Schema> toComponentSchemasMap() {
return m;
}

/**
* 把给定的类注册到组件库中
*
* @param clazz
* @return
*/
public Schema register(Class clazz) {
Schema c = new Schema();
c.setType("object");
c.setName(clazz.getName());
classToSchemaMap.put(clazz, c);
return c.direct();
}
// /**
// * 把给定的类注册到组件库中,但是并不解析名称,暂时未用到先注释掉
// *
// * @param clazz
// * @return
// */
// public Schema register(Class clazz) {
// Schema c = new Schema();
// c.setType("object");
// c.setName(clazz.getName());
// classToSchemaMap.put(clazz, c);
// return c.direct();
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Schema convert(Class clazz) {
return null;
}

// TODO 枚举类型是被看做一个有限取值的string,所以此处有必要查询缓存吗?
// Open API里枚举类型是被看做一个有限取值的string
Schema schema = manager.database.find(clazz);
if (schema != null) {
return schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
public interface FieldOpenApiSchemaConvertor {

/**
* 转换器的名字,方便日志打印啥的
*
* @return
*/
String getConvertorName();

/**
Expand Down
Loading

0 comments on commit 3dff18e

Please sign in to comment.