@@ -15,14 +15,10 @@ | |||
*/ | |||
package com.alibaba.csp.sentinel.annotation; | |||
import java.lang.annotation.ElementType; | |||
import java.lang.annotation.Inherited; | |||
import java.lang.annotation.Retention; | |||
import java.lang.annotation.RetentionPolicy; | |||
import java.lang.annotation.Target; | |||
import com.alibaba.csp.sentinel.EntryType; | |||
import java.lang.annotation.*; | |||
/** | |||
* The annotation indicates a definition of Sentinel resource. | |||
* | |||
@@ -63,4 +59,9 @@ public @interface SentinelResource { | |||
* @return name of the fallback function, empty by default | |||
*/ | |||
String fallback() default ""; | |||
/** | |||
* @return the exception classes to trace, Throwable.class by default | |||
*/ | |||
Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class}; | |||
} |
@@ -59,7 +59,9 @@ public class SentinelResourceAspect extends AbstractSentinelAspectSupport { | |||
} catch (BlockException ex) { | |||
return handleBlockException(pjp, annotation, ex); | |||
} catch (Throwable ex) { | |||
Tracer.trace(ex); | |||
if (isTrackedException(ex, annotation.exceptionsToTrace())) { | |||
Tracer.trace(ex); | |||
} | |||
throw ex; | |||
} finally { | |||
if (entry != null) { | |||
@@ -67,4 +69,23 @@ public class SentinelResourceAspect extends AbstractSentinelAspectSupport { | |||
} | |||
} | |||
} | |||
/** | |||
* whether the exception is in tracked list of exception classes | |||
* | |||
* @param ex | |||
* @param exceptionsToTrace | |||
* @return | |||
*/ | |||
private boolean isTrackedException(Throwable ex, Class<? extends Throwable>[] exceptionsToTrace) { | |||
if (exceptionsToTrace == null) { | |||
return false; | |||
} | |||
for (Class exceptionToTrace : exceptionsToTrace) { | |||
if (exceptionToTrace.isAssignableFrom(ex.getClass())) { | |||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
} |