BUG #IAFKQV 声明式的onError方法取不到Exception

This commit is contained in:
everywhere.z
2024-07-25 17:03:35 +08:00
parent 4859f856f5
commit 7462b5ff1f
8 changed files with 138 additions and 42 deletions

View File

@@ -1,5 +1,6 @@
package com.yomahub.liteflow.core.proxy;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.exceptions.InvocationTargetRuntimeException;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.util.ArrayUtil;
@@ -36,6 +37,7 @@ import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* 声明式组件的代理核心生成类
@@ -145,7 +147,7 @@ public class DeclComponentProxy {
// 首先需要保证第一个参数是NodeComponent
// 其次需要针对于@LiteflowFact做处理
try {
Object[] realArgs = loadMethodParameter(proxy, currentMethodWrapBean);
Object[] realArgs = loadMethodParameter(proxy, currentMethodWrapBean, args);
return ReflectUtil.invoke(declWarpBean.getRawBean(), currentMethodWrapBean.getMethod(), realArgs);
}catch (InvocationTargetRuntimeException e) {
InvocationTargetException targetEx = (InvocationTargetException) e.getCause();
@@ -157,52 +159,63 @@ public class DeclComponentProxy {
private final ExpressRunner expressRunner = new ExpressRunner();
private Object[] loadMethodParameter(Object proxy, MethodWrapBean methodWrapBean){
private Object[] loadMethodParameter(Object proxy, MethodWrapBean methodWrapBean, Object[] args){
NodeComponent thisNodeComponent = (NodeComponent) proxy;
return methodWrapBean.getParameterWrapBeanList().stream().map(parameterWrapBean -> {
// 如果参数是NodeComponent那就返回proxy本身
if (parameterWrapBean.getParameterType().isAssignableFrom(NodeComponent.class)) {
return proxy;
}
return IntStream.range(0, methodWrapBean.getParameterWrapBeanList().size()).boxed().map(new Function<Integer, Object>() {
@Override
public Object apply(Integer index) {
ParameterWrapBean parameterWrapBean = methodWrapBean.getParameterWrapBeanList().get(index);
// 如果没有@LiteflowFact标注那么不处理直接赋值null
if (parameterWrapBean.getFact() == null) {
return null;
}
// 如果参数是NodeComponent那就返回proxy本身
if (parameterWrapBean.getParameterType().isAssignableFrom(NodeComponent.class)) {
return proxy;
}
// 把上下文数据转换成map形式的key为别名value为上下文
Map<String, Object> contextMap = DataBus.getSlot(thisNodeComponent.getSlotIndex()).getContextBeanList().stream().collect(
Collectors.toMap(tuple -> tuple.get(0), tuple -> tuple.get(1))
);
List<String> errorList = new ArrayList<>();
Object result = null;
// 根据表达式去上下文里搜索相匹配的数据
for(Map.Entry<String, Object> entry : contextMap.entrySet()){
try{
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache(entry.getKey() + "." + parameterWrapBean.getFact().value());
DefaultContext<String, Object> context = new DefaultContext<>();
context.put(entry.getKey(), entry.getValue());
result = expressRunner.execute(instructionSet, context, errorList, false, false);
if (result != null){
break;
if (parameterWrapBean.getFact() == null && ArrayUtil.isNotEmpty(args)){
if (parameterWrapBean.getParameterType().isAssignableFrom(args[index - 1].getClass())){
return args[index -1];
}
}catch (Exception ignore){}
}
}
if (result == null){
try{
// 如果没有搜到,那么尝试推断表达式是指定的上下文,按照指定上下文的方式去再获取
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache("contextMap." + parameterWrapBean.getFact().value());
DefaultContext<String, Object> context = new DefaultContext<>();
context.put("contextMap", contextMap);
result = expressRunner.execute(instructionSet, context, errorList, false, false);
}catch (Exception ignore){}
}
// 如果没有@LiteflowFact标注那么不处理直接赋值null
if (parameterWrapBean.getFact() == null) {
return null;
}
return result;
// 把上下文数据转换成map形式的key为别名value为上下文
Map<String, Object> contextMap = DataBus.getSlot(thisNodeComponent.getSlotIndex()).getContextBeanList().stream().collect(
Collectors.toMap(tuple -> tuple.get(0), tuple -> tuple.get(1))
);
List<String> errorList = new ArrayList<>();
Object result = null;
// 根据表达式去上下文里搜索相匹配的数据
for(Map.Entry<String, Object> entry : contextMap.entrySet()){
try{
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache(entry.getKey() + "." + parameterWrapBean.getFact().value());
DefaultContext<String, Object> context = new DefaultContext<>();
context.put(entry.getKey(), entry.getValue());
result = expressRunner.execute(instructionSet, context, errorList, false, false);
if (result != null){
break;
}
}catch (Exception ignore){}
}
if (result == null){
try{
// 如果没有搜到,那么尝试推断表达式是指定的上下文,按照指定上下文的方式去再获取
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache("contextMap." + parameterWrapBean.getFact().value());
DefaultContext<String, Object> context = new DefaultContext<>();
context.put("contextMap", contextMap);
result = expressRunner.execute(instructionSet, context, errorList, false, false);
}catch (Exception ignore){}
}
return result;
}
}).toArray();
}
}