Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carry the triggered rule in subclasses of BlockException #469

Merged
merged 2 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@

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 {

private AuthorityRule rule;

public AuthorityException(String ruleLimitApp) {
super(ruleLimitApp);
}

public AuthorityException(String ruleLimitApp, AuthorityRule rule) {
this(ruleLimitApp);
this.rule = rule;
}

public AuthorityException(String message, Throwable cause) {
super(message, cause);
}
Expand All @@ -40,4 +49,14 @@ 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
*/
public AuthorityRule getRule() {
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void checkBlackWhiteAuthority(ResourceWrapper resource, Context context) throws

for (AuthorityRule rule : rules) {
if (!AuthorityRuleChecker.passCheck(rule, context)) {
throw new AuthorityException(context.getOrigin());
throw new AuthorityException(context.getOrigin(), rule);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
*/
public class DegradeException extends BlockException {

private DegradeRule rule;

public DegradeException(String ruleLimitApp) {
super(ruleLimitApp);
}

public DegradeException(String ruleLimitApp, DegradeRule rule) {
super(ruleLimitApp);
this.rule = rule;
}

public DegradeException(String message, Throwable cause) {
super(message, cause);
}
Expand All @@ -38,4 +45,15 @@ public DegradeException(String ruleLimitApp, String message) {
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
*/
public DegradeRule getRule() {
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void checkDegrade(ResourceWrapper resource, Context context, Defau

for (DegradeRule rule : rules) {
if (!rule.passCheck(context, node, count)) {
throw new DegradeException(rule.getLimitApp());
throw new DegradeException(rule.getLimitApp(), rule);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
*/
public class FlowException extends BlockException {

private FlowRule rule;

public FlowException(String ruleLimitApp) {
super(ruleLimitApp);
}

public FlowException(String ruleLimitApp, FlowRule rule) {
super(ruleLimitApp);
this.rule = rule;
}

public FlowException(String message, Throwable cause) {
Expand All @@ -40,4 +46,14 @@ 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
*/
public FlowRule getRule() {
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void checkFlow(ResourceWrapper resource, Context context, DefaultNode node, int
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,34 @@
*/
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ public class ParamFlowException extends BlockException {

private final String resourceName;

private ParamFlowRule rule;

public ParamFlowException(String resourceName, String message, Throwable cause) {
super(message, cause);
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() {
Expand All @@ -45,4 +53,25 @@ public String getResourceName() {
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
*/
public ParamFlowRule getRule() {
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ void checkFlow(ResourceWrapper resourceWrapper, int count, Object... args)
// 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);
}
}
}
Expand Down