Search in sources :

Example 1 with AjaxResult

use of com.github.lybgeek.circuitbreaker.framework.ciruitbreaker.model.AjaxResult 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)

Aggregations

AuthorityException (com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)1 DegradeException (com.alibaba.csp.sentinel.slots.block.degrade.DegradeException)1 ParamFlowException (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException)1 SystemBlockException (com.alibaba.csp.sentinel.slots.system.SystemBlockException)1 AjaxResult (com.github.lybgeek.circuitbreaker.framework.ciruitbreaker.model.AjaxResult)1 OutputStream (java.io.OutputStream)1