自定义全局异常捕获

/**
 * 自定义异常类
 *
 * @author WangYaoLong
 */
public class BusinessException extends RuntimeException {

    private final int code;

    public BusinessException(int code, String message) {
        super(message);
        this.code = code;
    }
    
    public BusinessException(String msg){
        this.code = ErrorCode.SUCCESS.getCode();
    }

    public BusinessException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
    }

    public BusinessException(ErrorCode errorCode, String message) {
        super(message);
        this.code = errorCode.getCode();
    }

    public int getCode() {
        return code;
    }
}
/**
 * @author WangYaoLong
 * create_time  2022/7/25 19:38
 * comment 异常处理
 */
@Slf4j
@ControllerAdvice
public class DefaultExceptionHandler {

    public DefaultExceptionHandler() {
    }

    @ExceptionHandler
    @ResponseBody
    public Result<Object> handleException(Exception ex) {

        if (ex instanceof BusinessException) {
            Result<Object> failed = Result.failed(((BusinessException) ex).getMessage());
            failed.setCode(((BusinessException) ex).getCode());
            log.error("", ex);
            return failed;
        } else if (ex instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException e = (MethodArgumentNotValidException) ex;
            BindingResult bindingResult = e.getBindingResult();
            StringBuilder sb = new StringBuilder();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                sb.append(fieldError.getDefaultMessage()).append("  ");
            }
            if (CollectionUtils.isEmpty(bindingResult.getFieldErrors())) {
                List<ObjectError> allErrors = bindingResult.getAllErrors();
                for (ObjectError objectError : allErrors) {
                    sb.append(objectError.getDefaultMessage()).append("  ");
                }
                return Result.failed(sb.toString());
            }
            String field = Objects.requireNonNull(bindingResult.getFieldError()).getField();
            log.error(sb.toString());
            return Result.failed("[" + field + "]" + sb);
        } else if (ex instanceof BindException) {
            BindException bindException = (BindException) ex;
            BindingResult bindingResult = bindException.getBindingResult();
            String field = Objects.requireNonNull(bindingResult.getFieldError()).getField();
            StringBuilder sb = new StringBuilder();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                sb.append(fieldError.getDefaultMessage()).append("  ");
            }
            log.error(sb.toString());
            return Result.failed("[" + field + "]" + sb);
        } else if (ex instanceof MethodArgumentTypeMismatchException) {
            Result<Object> failed = Result.failed();
            failed.setCode(-1);
            failed.setMsg("类型转换错误,请填写正确的请求参数");
            log.error(ex.getMessage());
            return failed;
        } else if (ex instanceof ExcelAnalysisException) {
            log.error(ex.getMessage());
            Result<Object> failed = com.wang.dog.common.Result.failed();
            failed.setCode(2);
            failed.setMsg(ex.getMessage());
            return failed;
        } else if (ex instanceof AccessDeniedException) {
            log.error(ex.getMessage());
            Result<Object> failed = Result.failed();
            failed.setCode(1);
            failed.setMsg("权限不足,无法访问");
            return failed;
        } else {
            log.error(ex.getMessage(), ex);
            return Result.failed(ex.getMessage());
        }
    }

}

全局异常捕获类 GlobalExceptionHandler

/**
 * 全局异常处理器
 *
 * @author WangYaoLong
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public Result<?> businessExceptionHandler(BusinessException e) {
        log.error("businessException: " + e.getMessage(), e);
        return Result.failed(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return Result.failed(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值