自定义一个捕捉异常的jar包,项目中只需要引入就可以直接拦截并做自定义的操作。

自定义一个捕捉异常的jar包,项目中只需要引入就可以拦截并做自定义的操作
在这里插入图片描述
新建一个项目,创建文件:
BusinessExceptionHandler.java

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.youming.shuiku.commons.base.exception;

import com.youming.shuiku.commons.exception.BusinessException;
import com.youming.shuiku.commons.response.ResponseCode;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.apache.dubbo.rpc.RpcException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class BusinessExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(BusinessExceptionHandler.class);

    public BusinessExceptionHandler() {
    }

    @ExceptionHandler({BusinessException.class, Exception.class})
    public ResponseEntity<?> handlerException(HttpServletRequest request, Exception ex) {
        Map<String, Object> error = new HashMap(2);
        if (ex instanceof BusinessException) {
            error.put("code", ((BusinessException)ex).getCode());
            error.put("message", ex.getMessage());
            log.warn("[全局业务异常]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), error.get("message"));
        } else {
            BindingResult bindingResult;
            String msg;
            if (ex instanceof MethodArgumentNotValidException) {
                MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException)ex;
                bindingResult = methodArgumentNotValidException.getBindingResult();
                msg = (String)bindingResult.getFieldErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).distinct().collect(Collectors.joining(","));
                error.put("code", HttpStatus.BAD_REQUEST.value());
                error.put("message", msg);
                log.warn("[系统异常--json]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), error.get("message"));
            } else if (ex instanceof BindException) {
                BindException bindException = (BindException)ex;
                bindingResult = bindException.getBindingResult();
                msg = (String)bindingResult.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).distinct().collect(Collectors.joining(","));
                error.put("code", HttpStatus.BAD_REQUEST.value());
                error.put("message", msg);
                log.warn("[系统异常--表单处理]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), error.get("message"));
            } else if (ex instanceof RpcException) {
                error.put("code", ResponseCode.SYSTEM_RPC.code());
                error.put("message", ResponseCode.SYSTEM_RPC.message());
                log.warn("[系统异常--rpc]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), ex.getMessage());
            } else {
                error.put("code", ResponseCode.UNKNOWN.code());
                error.put("message", ResponseCode.SYSTEM_UNKNOWN.message());
                log.warn("[系统异常]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), ex.getMessage());
            }
        }

        return new ResponseEntity(error, HttpStatus.OK);
    }
}

然后在META-INF中:
在这里插入图片描述
写入依赖的标志,这样在其他的微服务中,就可以引入这个异常处理的jar包就可以直接使用了。

举个例子:

服务中引入jar包,启动类加上需要扫描的包:
在这里插入图片描述
在有异常的时候会直接走自己定义的异常:
在这里插入图片描述
成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杵意

谢谢金主打赏呀!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值