diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/BlockException.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/BlockException.java index 605b7b7c..ada98383 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/BlockException.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/BlockException.java @@ -15,8 +15,9 @@ */ package com.alibaba.csp.sentinel.slots.block; -/*** - * Abstract exception indicating blocked by Sentinel due to flow control, degraded or system guard. +/** + * Abstract exception indicating blocked by Sentinel due to flow control, + * circuit breaking or system protection triggered. * * @author youji.zj */ @@ -42,6 +43,7 @@ public abstract class BlockException extends Exception { THROW_OUT_EXCEPTION.setStackTrace(sentinelStackTrace); } + protected AbstractRule rule; private String ruleLimitApp; public BlockException(String ruleLimitApp) { @@ -49,6 +51,12 @@ public abstract class BlockException extends Exception { this.ruleLimitApp = ruleLimitApp; } + public BlockException(String ruleLimitApp, AbstractRule rule) { + super(); + this.ruleLimitApp = ruleLimitApp; + this.rule = rule; + } + public BlockException(String message, Throwable cause) { super(message, cause); } @@ -58,6 +66,12 @@ public abstract class BlockException extends Exception { this.ruleLimitApp = ruleLimitApp; } + public BlockException(String ruleLimitApp, String message, AbstractRule rule) { + super(message); + this.ruleLimitApp = ruleLimitApp; + this.rule = rule; + } + @Override public Throwable fillInStackTrace() { return this; @@ -98,4 +112,8 @@ public abstract class BlockException extends Exception { return false; } + + public AbstractRule getRule() { + return rule; + } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityException.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityException.java index e007ec65..b3d0854b 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityException.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityException.java @@ -17,9 +17,11 @@ package com.alibaba.csp.sentinel.slots.block.authority; import com.alibaba.csp.sentinel.slots.block.BlockException; -/*** +/** + * Block exception for request origin access (authority) control. * * @author youji.zj + * @author Eric Zhao */ public class AuthorityException extends BlockException { @@ -27,6 +29,10 @@ public class AuthorityException extends BlockException { super(ruleLimitApp); } + public AuthorityException(String ruleLimitApp, AuthorityRule rule) { + super(ruleLimitApp, rule); + } + public AuthorityException(String message, Throwable cause) { super(message, cause); } @@ -40,4 +46,15 @@ public class AuthorityException extends BlockException { return this; } + /** + * Get triggered rule. + * Note: the rule result is a reference to rule map and SHOULD NOT be modified. + * + * @return triggered rule + * @since 1.4.2 + */ + @Override + public AuthorityRule getRule() { + return rule.as(AuthorityRule.class); + } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java index e9786c1a..369699c9 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java @@ -58,7 +58,7 @@ public class AuthoritySlot extends AbstractLinkedProcessorSlot { for (AuthorityRule rule : rules) { if (!AuthorityRuleChecker.passCheck(rule, context)) { - throw new AuthorityException(context.getOrigin()); + throw new AuthorityException(context.getOrigin(), rule); } } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeException.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeException.java index 1538373d..718d68f7 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeException.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeException.java @@ -26,6 +26,10 @@ public class DegradeException extends BlockException { super(ruleLimitApp); } + public DegradeException(String ruleLimitApp, DegradeRule rule) { + super(ruleLimitApp, rule); + } + public DegradeException(String message, Throwable cause) { super(message, cause); } @@ -38,4 +42,16 @@ public class DegradeException extends BlockException { public Throwable fillInStackTrace() { return this; } + + /** + * Get triggered rule. + * Note: the rule result is a reference to rule map and SHOULD NOT be modified. + * + * @return triggered rule + * @since 1.4.2 + */ + @Override + public DegradeRule getRule() { + return rule.as(DegradeRule.class); + } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java index 6e3bd7fc..5da818fa 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java @@ -76,7 +76,7 @@ public class DegradeRuleManager { for (DegradeRule rule : rules) { if (!rule.passCheck(context, node, count)) { - throw new DegradeException(rule.getLimitApp()); + throw new DegradeException(rule.getLimitApp(), rule); } } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowException.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowException.java index 81c2a0e3..01e8a3e5 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowException.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowException.java @@ -23,10 +23,13 @@ import com.alibaba.csp.sentinel.slots.block.BlockException; public class FlowException extends BlockException { public FlowException(String ruleLimitApp) { - super(ruleLimitApp); } + public FlowException(String ruleLimitApp, FlowRule rule) { + super(ruleLimitApp, rule); + } + public FlowException(String message, Throwable cause) { super(message, cause); } @@ -40,4 +43,15 @@ public class FlowException extends BlockException { return this; } + /** + * Get triggered rule. + * Note: the rule result is a reference to rule map and SHOULD NOT be modified. + * + * @return triggered rule + * @since 1.4.2 + */ + @Override + public FlowRule getRule() { + return rule.as(FlowRule.class); + } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlot.java index 3f392db1..9aa900ec 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlot.java @@ -151,7 +151,7 @@ public class FlowSlot extends AbstractLinkedProcessorSlot { if (rules != null) { for (FlowRule rule : rules) { if (!canPassCheck(rule, context, node, count, prioritized)) { - throw new FlowException(rule.getLimitApp()); + throw new FlowException(rule.getLimitApp(), rule); } } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemBlockException.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemBlockException.java index a6d6a835..24504e17 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemBlockException.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemBlockException.java @@ -22,25 +22,34 @@ import com.alibaba.csp.sentinel.slots.block.BlockException; */ public class SystemBlockException extends BlockException { - String resourceName; - - public String getResourceName() { - return resourceName; - } + private final String resourceName; public SystemBlockException(String resourceName, String message, Throwable cause) { super(message, cause); this.resourceName = resourceName; } - public SystemBlockException(String resourceName, String ruleLimitApp) { - super(ruleLimitApp); + public SystemBlockException(String resourceName, String limitType) { + super(limitType); this.resourceName = resourceName; } + public String getResourceName() { + return resourceName; + } + @Override public Throwable fillInStackTrace() { return this; } + /** + * Return the limit type of system rule. + * + * @return the limit type + * @since 1.4.2 + */ + public String getLimitType() { + return getRuleLimitApp(); + } } diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowException.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowException.java index a4a659c0..1c47bc53 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowException.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowException.java @@ -32,11 +32,17 @@ public class ParamFlowException extends BlockException { this.resourceName = resourceName; } - public ParamFlowException(String resourceName, String message) { - super(message, message); + public ParamFlowException(String resourceName, String param) { + super(param, param); this.resourceName = resourceName; } + public ParamFlowException(String resourceName, String param, ParamFlowRule rule) { + super(param, param); + this.resourceName = resourceName; + this.rule = rule; + } + public String getResourceName() { return resourceName; } @@ -45,4 +51,26 @@ public class ParamFlowException extends BlockException { public Throwable fillInStackTrace() { return this; } + + /** + * Get the parameter value that triggered the parameter flow control. + * + * @return the parameter value + * @since 1.4.2 + */ + public String getLimitParam() { + return getMessage(); + } + + /** + * Get triggered rule. + * Note: the rule result is a reference to rule map and SHOULD NOT be modified. + * + * @return triggered rule + * @since 1.4.2 + */ + @Override + public ParamFlowRule getRule() { + return rule.as(ParamFlowRule.class); + } } diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java index b3f7bec9..48d34f8f 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java @@ -81,12 +81,12 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot { // Here we add the block count. addBlockCount(resourceWrapper, count, args); - String message = ""; + String triggeredParam = ""; if (args.length > rule.getParamIdx()) { Object value = args[rule.getParamIdx()]; - message = String.valueOf(value); + triggeredParam = String.valueOf(value); } - throw new ParamFlowException(resourceWrapper.getName(), message); + throw new ParamFlowException(resourceWrapper.getName(), triggeredParam, rule); } } }