Search in sources :

Example 1 with AuthorityException

use of com.alibaba.csp.sentinel.slots.block.authority.AuthorityException in project java-demos by powerLeePlus.

the class CustomUrlBlockHandler method handle.

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws Exception {
    String msg = null;
    if (ex instanceof FlowException) {
        msg = "限流了";
    } else if (ex instanceof DegradeException) {
        msg = "降级了";
    } else if (ex instanceof ParamFlowException) {
        msg = "热点参数限流";
    } else if (ex instanceof SystemBlockException) {
        msg = "系统规则(负载/...不满足要求)";
    } else if (ex instanceof AuthorityException) {
        msg = "授权规则不通过";
    }
    // http状态码
    response.setStatus(500);
    response.setCharacterEncoding("utf-8");
    response.setHeader("Content-Type", "application/json;charset=utf-8");
    response.setContentType("application/json;charset=utf-8");
    try (PrintWriter writer = response.getWriter()) {
        writer.write(msg);
        writer.flush();
    }
}
Also used : FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) SystemBlockException(com.alibaba.csp.sentinel.slots.system.SystemBlockException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) AuthorityException(com.alibaba.csp.sentinel.slots.block.authority.AuthorityException) PrintWriter(java.io.PrintWriter)

Example 2 with AuthorityException

use of com.alibaba.csp.sentinel.slots.block.authority.AuthorityException in project springboot-learning by lyb-geek.

the class DefaultBlockExceptionHandler method handle.

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
    response.setStatus(200);
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/json; charset=utf-8");
    StringBuffer url = request.getRequestURL();
    if ("GET".equals(request.getMethod()) && StringUtil.isNotBlank(request.getQueryString())) {
        url.append("?").append(request.getQueryString());
    }
    String msg = "Blocked by Sentinel Flow Limit";
    log.error("sentinel flow limit url:{}", url);
    if (e instanceof AuthorityException) {
        msg = "Blocked by Sentinel Authority Limit";
    } else if (e instanceof SystemBlockException) {
        msg = "Blocked by Sentinel System Limit";
    } else if (e instanceof DegradeException) {
        msg = "Blocked by Sentinel Degrade Limit";
    } else if (e instanceof ParamFlowException) {
        msg = "Blocked by Sentinel ParamFlow Limit";
    }
    AjaxResult result = new AjaxResult();
    result.setSuccess(false);
    result.setCode(429);
    result.setMessage(msg);
    OutputStream out = response.getOutputStream();
    out.write(JSON.toJSONString(result).getBytes("utf-8"));
    out.flush();
    out.close();
}
Also used : AjaxResult(com.github.lybgeek.circuitbreaker.framework.ciruitbreaker.model.AjaxResult) OutputStream(java.io.OutputStream) SystemBlockException(com.alibaba.csp.sentinel.slots.system.SystemBlockException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) AuthorityException(com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)

Example 3 with AuthorityException

use of com.alibaba.csp.sentinel.slots.block.authority.AuthorityException in project amusing-project by m-lvqingyu.

the class GatewayBlockExceptionHandler method handle.

@Override
public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) {
    ServerHttpResponse response = serverWebExchange.getResponse();
    setProps(response);
    ServerHttpRequest request = serverWebExchange.getRequest();
    String path = request.getURI().getPath();
    // 限流
    if (throwable instanceof FlowException) {
        log.error("[gateway-sentinel]-request is flow, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.FLOW_ERROR));
    }
    // 熔断
    if (throwable instanceof DegradeException) {
        log.error("[gateway-sentinel]-request is degrade, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.DEGRADE_ERROR));
    }
    // 热点参数限流
    if (throwable instanceof ParamFlowException) {
        log.error("[gateway-sentinel]-request is paramFlow, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.PARAM_FLOW_ERROR));
    }
    // 系统保护规则
    if (throwable instanceof SystemBlockException) {
        log.error("[gateway-sentinel]-request is systemBlock, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.SYSTEM_BLOCK_ERROR));
    }
    // 授权规则
    if (throwable instanceof AuthorityException) {
        log.error("[gateway-sentinel]-request is authority, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.AUTHORITY_ERROR));
    }
    return responseWrite(response, ApiResult.result(CommCode.FREQUENT_OPERATION_EXCEPTION));
}
Also used : FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) SystemBlockException(com.alibaba.csp.sentinel.slots.system.SystemBlockException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) AuthorityException(com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)

Example 4 with AuthorityException

use of com.alibaba.csp.sentinel.slots.block.authority.AuthorityException in project springboot-learning by lyb-geek.

the class CustomBlockExceptionHandler method handle.

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
    response.setStatus(200);
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/json; charset=utf-8");
    StringBuffer url = request.getRequestURL();
    if ("GET".equals(request.getMethod()) && StringUtil.isNotBlank(request.getQueryString())) {
        url.append("?").append(request.getQueryString());
    }
    String msg = "Blocked by Sentinel Flow Limit";
    log.error("sentinel flow limit url:{}", url);
    if (e instanceof AuthorityException) {
        msg = "Blocked by Sentinel Authority Limit";
    } else if (e instanceof SystemBlockException) {
        msg = "Blocked by Sentinel System Limit";
    } else if (e instanceof DegradeException) {
        msg = "Blocked by Sentinel degrade Limit";
    }
    AjaxResult result = new AjaxResult();
    result.setSuccess(false);
    result.setCode(429);
    result.setMessage(msg);
    PrintWriter out = response.getWriter();
    out.print(JSON.toJSONString(result));
    out.flush();
    out.close();
}
Also used : AjaxResult(com.github.lybgeek.common.model.AjaxResult) SystemBlockException(com.alibaba.csp.sentinel.slots.system.SystemBlockException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) AuthorityException(com.alibaba.csp.sentinel.slots.block.authority.AuthorityException) PrintWriter(java.io.PrintWriter)

Aggregations

AuthorityException (com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)4 DegradeException (com.alibaba.csp.sentinel.slots.block.degrade.DegradeException)4 SystemBlockException (com.alibaba.csp.sentinel.slots.system.SystemBlockException)4 ParamFlowException (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException)3 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)2 PrintWriter (java.io.PrintWriter)2 AjaxResult (com.github.lybgeek.circuitbreaker.framework.ciruitbreaker.model.AjaxResult)1 AjaxResult (com.github.lybgeek.common.model.AjaxResult)1 OutputStream (java.io.OutputStream)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1