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

- extract the original exception from InvocationTargetException
This commit is contained in:
huangxfchn 2019-08-28 22:42:02 +08:00 committed by Eric Zhao
parent 1253471078
commit 4916ec35ee
1 changed files with 26 additions and 10 deletions

View File

@ -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.