Search in sources :

Example 1 with AjaxResult

use of com.github.lybgeek.common.model.AjaxResult 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)1 DegradeException (com.alibaba.csp.sentinel.slots.block.degrade.DegradeException)1 SystemBlockException (com.alibaba.csp.sentinel.slots.system.SystemBlockException)1 AjaxResult (com.github.lybgeek.common.model.AjaxResult)1 PrintWriter (java.io.PrintWriter)1