Sentinel服务容错三--自定义异常返回

Sentinel的五种类型规则在触发规则保护时,返回的异常页面是一样的,不好区分是因为哪种规则导致的异常。所以需要自定义异常返回信息,明确是触发了哪种类型的规则。

@Component
public class ExceptionHandlerPage implements UrlBlockHandler {

    @Override
    public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException e) throws IOException {
        response.setContentType("application/json;charset=utf-8");

        Result responseData = null;
        //BlockException  异常接口,包含Sentinel的五个异常
        //  FlowException  限流异常
        //  DegradeException  降级异常
        //  ParamFlowException  参数限流异常
        //  AuthorityException  授权异常
        //  SystemBlockException  系统负载异常
        if (e instanceof FlowException) {
            responseData = Result.getFailResult(410, "接口被限流了...");
        } else if (e instanceof DegradeException) {
            responseData = Result.getFailResult(420, "接口被降级了...");
        } else if (e instanceof ParamFlowException) {
            responseData = Result.getFailResult(430, "参数限流异常...");
        } else if (e instanceof AuthorityException) {
            responseData = Result.getFailResult(440, "授权异常...");
        } else if (e instanceof SystemBlockException) {
            responseData = Result.getFailResult(450, "系统负载异常...");
        }

        response.getWriter().write(JSON.toJSONString(responseData));
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值