Spring Boot 定义全局异常处理返回统一异常信息

概述

Spring Boot项目定义全局异常处理器,统一返回异常信息。

环境
JDK1.8

设计

1.自定义一个异常类

import lombok.Data;

/**
 * @Classname 自定义异常
 * @Description:
 * @Date 2021/6/30 16:16
 * @Created by LSH
 */
@Data
public class BizException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    protected String errorCode;
    /**
     * 错误信息
     */
    protected String errorMsg;
    /**
     * 返回数据
     */
    protected Object data;

    /**
     * 主要业务参数
     */
    protected String taskParam;
    /**
     * 业务参数
     */
    protected String taskParams;
    /**
     * 业务描述
     */
    protected String taskName;
    /**
     * 业务节点描述
     */
    protected String nodeName;
    /**
     * 执行的方法
     */
    protected String method;

    public BizException() {
        super();
    }

    public BizException(BaseErrorInfoInterface errorInfoInterface) {
        super(errorInfoInterface.getResultCode());
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }
    public BizException(BaseErrorInfoInterface errorInfoInterface,String taskParam, String taskName,String nodeName,String method,String taskParams) {
        super(errorInfoInterface.getResultCode());
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
        this.taskParam=taskParam;
        this.taskName=taskName;
        this.nodeName=nodeName;
        this.taskParams=taskParams;
        this.method=method;
    }

    public BizException(BaseErrorInfoInterface errorInfoInterface,String taskParam, String taskName,String nodeName,String method,String taskParams,Object data) {
        super(errorInfoInterface.getResultCode());
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
        this.taskParam=taskParam;
        this.taskName=taskName;
        this.nodeName=nodeName;
        this.taskParams=taskParams;
        this.method=method;
        this.data=data;
    }

    public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
        super(errorInfoInterface.getResultCode(), cause);
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }

    public BizException(String errorMsg) {
        super(errorMsg);
        this.errorMsg = errorMsg;
    }

    public BizException(String errorCode, String errorMsg) {
        super(errorCode);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public BizException(String errorCode, String errorMsg, Throwable cause) {
        super(errorCode, cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public String getErrorCode() {
        return errorCode;
    }

    @Override
    public String getMessage() {
        return errorMsg;
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }
}

注意: 该异常类设计时字段设计的多有冗余,可以先按照例子设计,通篇看完后就知道这些字段都用来干嘛了,然后即可按需设计。

2.定义一个异常信息接口模板

/**
 * @Classname BaseErrorInfoInterface
 * @Description:
 * @Date 2021/6/30 16:14
 * @Created by LSH
 */
public interface BaseErrorInfoInterface {
    /** 错误码*/
    String getResultCode();

    /** 错误描述*/
    String getResultMsg();
}

3.定义一个异常枚举,实现步骤2的接口

/**
 * @Classname CommonEnum
 * @Description:
 * @Date 2021/6/30 16:15
 * @Created by LSH
 */
public enum CommonEnum implements BaseErrorInfoInterface {
    // 数据操作错误定义
    SUCCESS("200", "success"),

    BODY_NOT_MATCH("400","数据空指针!"),
    SIGNATURE_NOT_MATCH("401","请求的数字签名不匹配!"),
    NOT_FOUND("404", "未找到该资源!"),

    INTERNAL_SERVER_ERROR("500", "服务器内部错误!"),
    PARAM_VERIFY_ERROR("501", "业务参数为空!"),
    PARAM_ILLEGAL_ERROR("502", "非法的业务参数!"),
    SERVER_BUSY("503","服务器正忙,请稍后再试!"),
    DUPLICATE_KEY_ERROR("504","DB主键冲突!"),
    TOO_MANY_RESULT_ERROR("505","Query异常!"),
    QUERY_RESULT_NULL("506","There are some scenarios where DB Query is necessary,But null"),
    HTTP_API_ERROR("507","HTTP API服务异常!"),
    BAD_SQL("508","BAD SQL!"),
    DML_ERROR("510","DML ERROR!"),

    EXPORT_ERROR("600","File Export Error!"),
    FILE_NOT_FOUND("601","File Not Found!"),
    FILE_READ_ERROR("603","File Parsing Error!"),

    HTTP_ERROR("700","Http API Error!");

    /** 错误码 */
    private String resultCode;

    /** 错误描述 */
    private String resultMsg;

    CommonEnum(String resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }


    @Override
    public String getResultCode() {
        return resultCode;
    }

    @Override
    public String getResultMsg() {
        return resultMsg;
    }
}

注意:该枚举类中的code以及code对应的错误描述按需自行设计即可。

4.定义一个异常实体类,用于发生异常时返回给前端

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

/**
 * @Classname ResultBody
 * @Description:
 * @Date 2021/6/30 16:17
 * @Created by LSH
 */
@Data
public class ResultBody {
    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应结果
     */
    private Object result;

    public ResultBody() {
    }

    public ResultBody(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }

    /**
     * 成功,不带返回数据
     * @return
     */
    public static ResultBody success() {
        return success(null);
    }

    /**
     * 成功,带返回数据
     * @param data
     * @return
     */
    public static ResultBody success(Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(CommonEnum.SUCCESS.getResultCode());
        rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
        rb.setResult(data);
        return rb;
    }

    /**
     * 失败,不带返回数据
     */
    public static ResultBody error(BaseErrorInfoInterface errorInfo) {
        ResultBody rb = new ResultBody();
        rb.setCode(errorInfo.getResultCode());
        rb.setMessage(errorInfo.getResultMsg());
        rb.setResult(null);
        return rb;
    }

    /**
     * 失败,不带返回数据
     */
    public static ResultBody error(String code, String message) {
        ResultBody rb = new ResultBody();
        rb.setCode(code);
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }
    /**
     * 失败,带返回数据
     */
    public static ResultBody error(String code, String message,Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(code);
        rb.setMessage(message);
        rb.setResult(data);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error( String message) {
        ResultBody rb = new ResultBody();
        rb.setCode("-1");
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}

5.定义一个全局异常处理器

@ExceptionHandler 注解中中预定义好运行时可能会遇到的异常类型,当异常发生时,就能够被该类捕获。

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname GlobalExceptionHandler
 * @Description: 全局异常处理器
 * @Date 2021/6/30 16:19
 * @Created by LSH
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //@Autowired
    //private ErrorLogService errorLogService;

    /**
     * 处理自定义业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ResultBody bizExceptionHandler(BizException e){
        log.error(e.getMethod(),e);
        return ResultBody.error(e.getErrorCode(),e.getErrorMsg(),e.getData());
    }

    /**
     * 处理空指针异常
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public ResultBody exceptionHandler(NullPointerException e){
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"空指针异常!");
        log.error("空指针异常!",e);
        return ResultBody.error(CommonEnum.BODY_NOT_MATCH);
    }

    /**
     * 处理未知异常
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public ResultBody exceptionHandler(Exception e){
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"未知异常!");
        log.error("未知异常!",e);
        return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }
    
    /**
     * DB异常
     * @param e
     * @return
     */
    @ExceptionHandler(UncategorizedSQLException.class)
    @ResponseBody
    public ResultBody handleUncategorizedSQLException(UncategorizedSQLException e){
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"DB主键冲突!");
        log.error("DB异常!", e);
        return ResultBody.error(CommonEnum.BAD_SQL);
    }
    
    /**
     * DB主键冲突异常
     * @param e
     * @return
     */
    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseBody
    public ResultBody handleDuplicateKeyException(DuplicateKeyException e){
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"DB异常!");
        log.error("DB主键冲突!", e);
        return ResultBody.error(CommonEnum.DUPLICATE_KEY_ERROR);
    }

    /**
     * 非法sql异常
     * @param e
     * @return
     */
    @ExceptionHandler(BadSqlGrammarException.class)
    @ResponseBody
    public ResultBody badSqlGrammarException(BadSqlGrammarException e){
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"BAD SQL!");
        log.error("BAD SQL!", e);
        return ResultBody.error(CommonEnum.BAD_SQL);
    }

    /**
     * Query异常
     * @param e
     * @return
     */
    @ExceptionHandler(MyBatisSystemException.class)
    @ResponseBody
    public ResultBody queryException(MyBatisSystemException e){
        //errorLogService.insertErrorLog("","","Query Data",exceptionMethod(e),e,"Query异常!");
        log.error("Query异常!", e);
        return ResultBody.error(CommonEnum.TOO_MANY_RESULT_ERROR);
    }

    /**
     * DML 操作异常
     * @param e
     * @return
     */
    @ExceptionHandler(DataIntegrityViolationException.class)
    @ResponseBody
    public ResultBody dataIntegrityViolationException(MyBatisSystemException e){
        //errorLogService.insertErrorLog("","","Query Data",exceptionMethod(e),e,"DML 操作异常!");
        log.error("DML 操作异常!", e);
        return ResultBody.error(CommonEnum.DML_ERROR);
    }

    /**
     * 运算异常
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public ResultBody arithmeticExceptionHandler(ArithmeticException e) {
        //errorLogService.insertErrorLog("","","Data Process",exceptionMethod(e),e,"运算异常!");
        log.error("运算异常!",e);
        return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 类型转换异常
     */
    @ExceptionHandler(ClassCastException.class)
    @ResponseBody
    public ResultBody classCastExceptionHandler(ClassCastException e) {
        //errorLogService.insertErrorLog("","","Data Process",exceptionMethod(e),e,"类型转换异常!");
        log.error("类型转换异常!",e);
        return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 数据下标越界异常
     */
    @ExceptionHandler(IndexOutOfBoundsException.class)
    @ResponseBody
    public ResultBody indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException e) {
        //errorLogService.insertErrorLog("","","Data Process",exceptionMethod(e),e,"数据下标越界异常!");
        log.error("数据下标越界异常!",e);
        return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 文件未找到异常
     */
    @ExceptionHandler(FileNotFoundException.class)
    @ResponseBody
    public ResultBody fileNotFoundExceptionHandler(FileNotFoundException e) {
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"文件未找到异常!");
        log.error("文件未找到异常!",e);
        return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * IO异常
     */
    @ExceptionHandler(IOException.class)
    @ResponseBody
    public ResultBody iOExceptionHandler(IOException e) {
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"IO异常!");
        log.error("IO异常!",e);
        return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 参数类型不匹配
     */
    @ExceptionHandler({MethodArgumentTypeMismatchException.class})
    @ResponseBody
    public ResultBody requestTypeMismatch(MethodArgumentTypeMismatchException e) {
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"参数类型不匹配!");
        log.error("参数类型不匹配!",e);
        return ResultBody.error("参数类型不匹配");
    }

    /**
     * 必要的参数为null
     */
    @ExceptionHandler({MissingServletRequestParameterException.class})
    @ResponseBody
    public ResultBody requestMissingServletRequest(MissingServletRequestParameterException e) {
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"必要的参数为null");
        log.error("必要的参数为null!",e);
        return ResultBody.error("必要的参数为null");
    }

    /**
     * 请求method不匹配
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    @ResponseBody
    public ResultBody requestMissingServletRequest(HttpRequestMethodNotSupportedException e) {
        //errorLogService.insertErrorLog("","","",exceptionMethod(e),e,"请求method不匹配");
        log.error("请求method不匹配!",e);
        return ResultBody.error("请求method不匹配");
    }

    /**
     * 获取异常堆栈信息
     * @param e
     * @return
     */
    public Map<String,Object> getExceptionStackTraceInfo(Exception e){
        if(e==null){
            return null;
        }

        Map<String,Object> exceptionStackTraceInfo=new HashMap<>();
        StackTraceElement[] stackTrace = e.getStackTrace();
        String methodName = "";
        String className = "";
        if(stackTrace.length>0){
            methodName = stackTrace[0].getMethodName();
            className = e.getStackTrace()[0].getClassName();
        }
        exceptionStackTraceInfo.put("method",methodName);
        exceptionStackTraceInfo.put("class",className);
        return exceptionStackTraceInfo;
    }

    /**
     * 获取发生异常时的方法
     * @param e
     * @return
     */
    public String exceptionMethod(Exception e){
        Map<String, Object> exceptionStackTraceInfo = this.getExceptionStackTraceInfo(e);
        if(exceptionStackTraceInfo==null){
            return null;
        }
        return (String) exceptionStackTraceInfo.get("method");
    }

    /**
     * 获取发生异常时的类
     * @param e
     * @return
     */
    public String exceptionClass(Exception e){
        Map<String, Object> exceptionStackTraceInfo = this.getExceptionStackTraceInfo(e);
        if(exceptionStackTraceInfo==null){
            return null;
        }
        return (String) exceptionStackTraceInfo.get("class");
    }
}

6.实际效果样例

1.在API内部预设一个空指针异常
预设一个异常
2.使用API管理工具ApiPost调用该API
调用API
3.异常返回
异常返回信息
该异常在全局异常处理类中已经预定义,所以当空指针异常发生时就能够被捕获到
空指针异常

通过统一的响应码和响应信息,在遇到Bug时即可大致判断出了什么错误,快速定位线上Bug。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值