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
Prev Previous commit
Extract getRule in BlockException and refine override methods in subc…
…lasses

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 committed Jan 31, 2019
commit 0d2d6364ceff73e9ccde7b5778bfdddb3c207c45
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.alibaba.csp.sentinel.slots.block;

/***
/**
* Abstract exception indicating blocked by Sentinel due to flow control,
* circuit breaking or system protection triggered.
*
Expand Down Expand Up @@ -43,13 +43,20 @@ public abstract class BlockException extends Exception {
THROW_OUT_EXCEPTION.setStackTrace(sentinelStackTrace);
}

protected AbstractRule rule;
private String ruleLimitApp;

public BlockException(String ruleLimitApp) {
super();
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);
}
Expand All @@ -59,6 +66,12 @@ public BlockException(String ruleLimitApp, String message) {
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;
Expand Down Expand Up @@ -99,4 +112,8 @@ public static boolean isBlockException(Throwable t) {

return false;
}

public AbstractRule getRule() {
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@
*/
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;
super(ruleLimitApp, rule);
}

public AuthorityException(String message, Throwable cause) {
Expand All @@ -56,7 +53,8 @@ public Throwable fillInStackTrace() {
* @return triggered rule
* @since 1.4.2
*/
@Override
public AuthorityRule getRule() {
return rule;
return rule.as(AuthorityRule.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@
*/
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;
super(ruleLimitApp, rule);
}

public DegradeException(String message, Throwable cause) {
Expand All @@ -53,7 +50,8 @@ public Throwable fillInStackTrace() {
* @return triggered rule
* @since 1.4.2
*/
@Override
public DegradeRule getRule() {
return rule;
return rule.as(DegradeRule.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@
*/
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;
super(ruleLimitApp, rule);
}

public FlowException(String message, Throwable cause) {
Expand All @@ -53,7 +50,8 @@ public Throwable fillInStackTrace() {
* @return triggered rule
* @since 1.4.2
*/
@Override
public FlowRule getRule() {
return rule;
return rule.as(FlowRule.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ 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;
Expand Down Expand Up @@ -71,7 +69,8 @@ public String getLimitParam() {
* @return triggered rule
* @since 1.4.2
*/
@Override
public ParamFlowRule getRule() {
return rule;
return rule.as(ParamFlowRule.class);
}
}