统一异常处理及自定义异常

(1)统一异常处理

1.异常错误枚举

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public enum ExceptionCode {

    /**
     * 异常信息
     */
    PARAM_ERROR("8000", "填入异常信息"),

    COMMON_ERROR("8001", "填入异常信息"),

    OTHER_ERROR("8002", "填入异常信息");

    private final String code;

    private final String msg;
}

2. 配置统一异常处理类

import com.yang.demo.config.bean.ExceptionCode;
import com.yang.demo.util.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {

    /**
     * 系统异常.
     *
     * @param exception 异常信息
     * @return R
     */
    @ExceptionHandler(Exception.class)
    public ResultJson exception(Exception exception) {
        log.error("系统异常信息 ex={}", exception.getMessage(), exception);
        // 未知异常统一抛出8000
        return new ResultJson(ExceptionCode.OTHER_ERROR.getCode(), ExceptionCode.OTHER_ERROR.getMsg());
    }
}

(2)自定义异常

1. 自定义异常   BaseException(用于细化自定义异常,使自定义异常继承此类)

import com.yang.demo.config.bean.ExceptionCode;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class BaseException extends RuntimeException {

    private final String code;

    private final String errorMsg;

    public BaseException(String code, String errorMsg) {
        super(errorMsg);
        this.code = code;
        this.errorMsg = errorMsg;
    }

    public BaseException(ExceptionCode code) {
        super(code.getMsg());
        this.code = code.getCode();
        this.errorMsg = code.getMsg();
    }

    public BaseException(ExceptionCode code, String errorMsg) {
        super(errorMsg);
        this.code = code.getCode();
        this.errorMsg = errorMsg;
    }
}

2. 编写异常类继承BaseException

import com.yang.demo.config.bean.ExceptionCode;

public class CheckException extends BaseException {

    public CheckException(String code, String errorMsg) {
        super(code, errorMsg);
    }

    public CheckException(ExceptionCode code) {
        super(code);
    }

    public CheckException(ExceptionCode code, String errorMsg) {
        super(code, errorMsg);
    }
}

3. 添加方法

import com.yang.demo.config.bean.ExceptionCode;
import com.yang.demo.util.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {

    /**
     * 系统异常.
     *
     * @param exception 异常信息
     * @return R
     */
    @ExceptionHandler(BaseException.class)
    @Order(99)
    public ResultJson checkException(BaseException exception) {
        log.error("系统异常信息 ex={}", exception.getMessage(), exception);
        // 未知异常统一抛出9999
        return new ResultJson(ExceptionCode.OTHER_ERROR.getCode(), ExceptionCode.OTHER_ERROR.getMsg());
    }

    /**
     * 系统异常.
     *
     * @param exception 异常信息
     * @return R
     */
    @ExceptionHandler(Exception.class)
    @Order(99)
    public ResultJson checkException(Exception exception) {
        log.error("系统异常信息 ex={}", exception.getMessage(), exception);
        // 未知异常统一抛出9999
        return new ResultJson(ExceptionCode.OTHER_ERROR.getCode(), ExceptionCode.OTHER_ERROR.getMsg());
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值