全局异常管理

1.自定义异常
继承异常
2.全局管理

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义业务异常
     *
     * @param request 请求
     * @param e       自定义业务异常
     * @return map
     * @author ZhaoQingDong
     */
    @ExceptionHandler(value = BusinessException.class)
    public R businessException(HttpServletRequest request, BusinessException e) {
        log.error(request.getRequestURL() + ":业务异常 ========> code:{}, msg:{}", e.getCode(), e.getMsg());
        return R.error(e.getCode(), e.getMsg());
    }

    /**
     * 处理参数校验异常一
     *
     * @param request 请求
     * @param e       参数校验异常一
     * @return map
     * @author ZhaoQingDong
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R methodArgumentNotValidException(HttpServletRequest request, MethodArgumentNotValidException e) {
        log.error(request.getRequestURL() + ":请求参数异常 ========> :{}", e.getMessage());
        BindingResult result = e.getBindingResult();
        Map<String, String> map = new HashMap<>();
        if (result.hasErrors()) {
            List<FieldError> fieldErrors = result.getFieldErrors();
            fieldErrors.forEach(error -> map.put(error.getField(), error.getDefaultMessage()));
        }
        e.printStackTrace();
        return R.error(BusinessExceptionEnum.PARAM_NOT_MATCH.getCode(), BusinessExceptionEnum.PARAM_NOT_MATCH.getMsg()).putData(map);
    }

    /**
     * 处理参数校验异常二
     *
     * @param request 请求
     * @param e       参数校验异常二
     * @return map
     * @author ZhaoQingDong
     */
    @ExceptionHandler(value = ConstraintViolationException.class)
    public R constraintViolationException(HttpServletRequest request, ConstraintViolationException e) {
        log.error(request.getRequestURL() + ":请求参数异常 ========> :{}", e.getMessage());
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        Map<String, String> map = new HashMap<>();
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            map.put(constraintViolation.getPropertyPath().toString(), constraintViolation.getMessageTemplate());
        }
        e.printStackTrace();
        return R.error(BusinessExceptionEnum.PARAM_NOT_MATCH.getCode(), BusinessExceptionEnum.PARAM_NOT_MATCH.getMsg()).putData(map);
    }

    /**
     * ID不存在,查询结果为空
     *
     * @param request 请求
     * @param e       ID不存在,查询结果为空异常
     * @author 赵庆东 2021-10-02 09:08
     */
    @ExceptionHandler(value = EmptyResultDataAccessException.class)
    public R emptyResultDataAccessException(HttpServletRequest request, EmptyResultDataAccessException e) {
        log.error(request.getRequestURL() + ":ID不存在异常 ========> :{}", e.getMessage());
        return R.error(BusinessExceptionEnum.RESULT_NOT_EXIST.getCode(), BusinessExceptionEnum.RESULT_NOT_EXIST.getMsg()).putData(e.getMessage());
    }

}

@RestControllerAdvice
@ @ExceptionHandler(value = EmptyResultDataAccessException.class)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值