Parcourir la source

Improve annotation aspect to support throwing original exception in fallback and blockHandler (#986)

- extract the original exception from InvocationTargetException
master
huangxfchn Eric Zhao il y a 5 ans
Parent
révision
4916ec35ee
1 fichiers modifiés avec 26 ajouts et 10 suppressions
  1. +26
    -10
      sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java

+ 26
- 10
sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java Voir le fichier

@@ -21,10 +21,10 @@ import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.MethodUtil;
import com.alibaba.csp.sentinel.util.StringUtil;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
@@ -100,10 +100,16 @@ public abstract class AbstractSentinelAspectSupport {
args = Arrays.copyOf(originArgs, originArgs.length + 1);
args[args.length - 1] = ex;
}
if (isStatic(fallbackMethod)) {
return fallbackMethod.invoke(null, args);

try {
if (isStatic(fallbackMethod)) {
return fallbackMethod.invoke(null, args);
}
return fallbackMethod.invoke(pjp.getTarget(), args);
} catch (InvocationTargetException e) {
// throw the actual exception
throw e.getTargetException();
}
return fallbackMethod.invoke(pjp.getTarget(), args);
}
// If fallback is absent, we'll try the defaultFallback if provided.
return handleDefaultFallback(pjp, defaultFallback, fallbackClass, ex);
@@ -116,10 +122,15 @@ public abstract class AbstractSentinelAspectSupport {
if (fallbackMethod != null) {
// Construct args.
Object[] args = fallbackMethod.getParameterTypes().length == 0 ? new Object[0] : new Object[] {ex};
if (isStatic(fallbackMethod)) {
return fallbackMethod.invoke(null, args);
try {
if (isStatic(fallbackMethod)) {
return fallbackMethod.invoke(null, args);
}
return fallbackMethod.invoke(pjp.getTarget(), args);
} catch (InvocationTargetException e) {
// throw the actual exception
throw e.getTargetException();
}
return fallbackMethod.invoke(pjp.getTarget(), args);
}

// If no any fallback is present, then directly throw the exception.
@@ -137,10 +148,15 @@ public abstract class AbstractSentinelAspectSupport {
// Construct args.
Object[] args = Arrays.copyOf(originArgs, originArgs.length + 1);
args[args.length - 1] = ex;
if (isStatic(blockHandlerMethod)) {
return blockHandlerMethod.invoke(null, args);
try {
if (isStatic(blockHandlerMethod)) {
return blockHandlerMethod.invoke(null, args);
}
return blockHandlerMethod.invoke(pjp.getTarget(), args);
} catch (InvocationTargetException e) {
// throw the actual exception
throw e.getTargetException();
}
return blockHandlerMethod.invoke(pjp.getTarget(), args);
}

// If no block handler is present, then go to fallback.


Chargement…
Annuler
Enregistrer