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();
}
Aggregations