bug #I7TYS3 当组件出现Exception的时候,afterProcess获取不到

This commit is contained in:
everywhere.z
2023-08-17 15:32:05 +08:00
parent 36c8974e04
commit f9c8d8d1c4
14 changed files with 102 additions and 17 deletions

View File

@@ -22,4 +22,8 @@ public interface ICmpAroundAspect {
void afterProcess(NodeComponent cmp);
void onSuccess(NodeComponent cmp);
void onError(NodeComponent cmp, Exception e);
}

View File

@@ -110,17 +110,6 @@ public abstract class NodeComponent {
cmpStep.setSuccess(false);
cmpStep.setException(e);
if (!(e instanceof ChainEndException)){
String chainId = this.getCurrChainId();
// 这里事先取到exception set到slot里为了方便finally取到exception
if (slot.isSubChain(chainId)) {
slot.setSubException(chainId, e);
}
else {
slot.setException(e);
}
}
// 执行失败后回调方法
// 这里要注意,失败方法本身抛出错误,只打出堆栈,往外抛出的还是主要的异常
try {
@@ -161,10 +150,16 @@ public abstract class NodeComponent {
public void onSuccess() throws Exception {
// 如果需要在成功后回调某一个方法,请覆盖这个方法
// 全局切面只在spring体系下生效这里用了spi机制取到相应环境下的实现类
// 非spring环境下全局切面为空实现
CmpAroundAspectHolder.loadCmpAroundAspect().onSuccess(this.self);
}
public void onError(Exception e) throws Exception {
// 如果需要在抛错后回调某一段逻辑,请覆盖这个方法
// 全局切面只在spring体系下生效这里用了spi机制取到相应环境下的实现类
// 非spring环境下全局切面为空实现
CmpAroundAspectHolder.loadCmpAroundAspect().onError(this.self, e);
}
public void afterProcess() {

View File

@@ -15,4 +15,8 @@ public interface CmpAroundAspect extends SpiPriority {
void afterProcess(NodeComponent cmp);
void onSuccess(NodeComponent cmp);
void onError(NodeComponent cmp, Exception e);
}

View File

@@ -22,6 +22,16 @@ public class LocalCmpAroundAspect implements CmpAroundAspect {
// 无spring环境下为空实现
}
@Override
public void onSuccess(NodeComponent cmp) {
// 无spring环境下为空实现
}
@Override
public void onError(NodeComponent cmp, Exception e) {
// 无spring环境下为空实现
}
@Override
public int priority() {
return 2;

View File

@@ -37,6 +37,20 @@ public class SolonCmpAroundAspect implements CmpAroundAspect {
}
}
@Override
public void onSuccess(NodeComponent cmp) {
if (ObjectUtil.isNotNull(cmpAroundAspect)) {
cmpAroundAspect.onSuccess(cmp);
}
}
@Override
public void onError(NodeComponent cmp, Exception e) {
if (ObjectUtil.isNotNull(cmpAroundAspect)) {
cmpAroundAspect.onError(cmp, e);
}
}
@Override
public int priority() {
return 1;

View File

@@ -28,6 +28,20 @@ public class SpringCmpAroundAspect implements CmpAroundAspect {
}
}
@Override
public void onSuccess(NodeComponent cmp) {
if (ObjectUtil.isNotNull(ComponentScanner.cmpAroundAspect)) {
ComponentScanner.cmpAroundAspect.onSuccess(cmp);
}
}
@Override
public void onError(NodeComponent cmp, Exception e) {
if (ObjectUtil.isNotNull(ComponentScanner.cmpAroundAspect)) {
ComponentScanner.cmpAroundAspect.onError(cmp, e);
}
}
@Override
public int priority() {
return 1;

View File

@@ -72,6 +72,7 @@ public class GlobalAOPELDeclMultiSpringbootTest extends BaseTest {
Assertions.assertEquals("before_after", context.getData("b"));
Assertions.assertEquals("before_after", context.getData("c"));
Assertions.assertEquals("before_after", context.getData("f"));
Assertions.assertEquals("test error", context.getData("f_error"));
}
@AfterAll

View File

@@ -20,4 +20,15 @@ public class CmpAspect implements ICmpAroundAspect {
context.setData(cmp.getNodeId(), StrUtil.format("{}_{}", context.getData(cmp.getNodeId()), "after"));
}
@Override
public void onSuccess(NodeComponent cmp) {
}
@Override
public void onError(NodeComponent cmp, Exception e) {
DefaultContext context = cmp.getFirstContextBean();
context.setData(cmp.getNodeId()+"_error", e.getMessage());
}
}

View File

@@ -71,6 +71,7 @@ public class GlobalAOPELDeclSpringbootTest extends BaseTest {
Assertions.assertEquals("before_after", context.getData("b"));
Assertions.assertEquals("before_after", context.getData("c"));
Assertions.assertEquals("before_after", context.getData("f"));
Assertions.assertEquals("test error", context.getData("f_error"));
}
@AfterAll

View File

@@ -20,4 +20,15 @@ public class CmpAspect implements ICmpAroundAspect {
context.setData(cmp.getNodeId(), StrUtil.format("{}_{}", context.getData(cmp.getNodeId()), "after"));
}
@Override
public void onSuccess(NodeComponent cmp) {
}
@Override
public void onError(NodeComponent cmp, Exception e) {
DefaultContext context = cmp.getFirstContextBean();
context.setData(cmp.getNodeId()+"_error", e.getMessage());
}
}

View File

@@ -66,7 +66,9 @@ public class GlobalAOPELSpringbootTest extends BaseTest {
Assertions.assertEquals("before_after", context.getData("a"));
Assertions.assertEquals("before_after", context.getData("b"));
Assertions.assertEquals("before_after", context.getData("c"));
Assertions.assertEquals("test error", context.getData("f"));
Assertions.assertEquals("before_after", context.getData("f"));
Assertions.assertEquals("test error", context.getData("f_error"));
}

View File

@@ -18,12 +18,18 @@ public class CmpAspect implements ICmpAroundAspect {
@Override
public void afterProcess(NodeComponent cmp) {
DefaultContext context = cmp.getFirstContextBean();
if (ObjectUtil.isNotNull(cmp.getSlot().getException())){
context.setData(cmp.getNodeId(), cmp.getSlot().getException().getMessage());
}else{
context.setData(cmp.getNodeId(), StrUtil.format("{}_{}", context.getData(cmp.getNodeId()), "after"));
}
context.setData(cmp.getNodeId(), StrUtil.format("{}_{}", context.getData(cmp.getNodeId()), "after"));
}
@Override
public void onSuccess(NodeComponent cmp) {
}
@Override
public void onError(NodeComponent cmp, Exception e) {
DefaultContext context = cmp.getFirstContextBean();
context.setData(cmp.getNodeId()+"_error", e.getMessage());
}
}

View File

@@ -63,6 +63,7 @@ public class GlobalAOPELSpringTest extends BaseTest {
Assertions.assertEquals("before_after", context.getData("b"));
Assertions.assertEquals("before_after", context.getData("c"));
Assertions.assertEquals("before_after", context.getData("f"));
Assertions.assertEquals("test error", context.getData("f_error"));
}
@AfterAll

View File

@@ -20,4 +20,15 @@ public class CmpAspect implements ICmpAroundAspect {
context.setData(cmp.getNodeId(), StrUtil.format("{}_{}", context.getData(cmp.getNodeId()), "after"));
}
@Override
public void onSuccess(NodeComponent cmp) {
}
@Override
public void onError(NodeComponent cmp, Exception e) {
DefaultContext context = cmp.getFirstContextBean();
context.setData(cmp.getNodeId()+"_error", e.getMessage());
}
}