统一异常处理

18 篇文章 0 订阅
11 篇文章 0 订阅

异常信息处理类

/**
 * 统一异常处理
 * @author 陌路
 */
@Slf4j
@RestControllerAdvice
public class BaseExceptionHandler extends Exception{

    private final Logger logger = LoggerFactory.getLogger(BaseExceptionHandler.class);
	/**
	 * @Desc 空指针异常拦截器
	 */
    @ExceptionHandler(value = {NullPointerException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R nullPointerExceptionHandler(HttpServletResponse response, Exception e) {
        logger.error("检测到空指针异常:{}", e.getMessage());
        e.printStackTrace();
        return R.err(e.getMessage());
    }

    /**
     * @Desc 其他异常拦截器
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public R exceptionHandler(Exception e) {
        logger.error("检测到异常信息:{}", e.getMessage());
        e.printStackTrace();
        return R.err(e.getMessage());
    }
}

封装自定义异常类

/**
 * @Title 自定义异常
 * @author 陌路
 * @date 2022-05-30 下午7:52:29
 */
public class BusinessException extends RuntimeException {
	private static final long serialVersionUID = 5046932872928928307L;

	private static final Logger LOGGER = LoggerFactory.getLogger(BusinessException.class);

	public BusinessException() {
	}
	
	/**
	 * 返回自定义异常信息
	 */
	public BusinessException(String message) {
		super(message);
		LOGGER.error("自定义异常信息:{}", message);
	}

	/**
	 * 返回自定义异常信息和系统异常信息
	 */
	public BusinessException(String message, Throwable cause) {
		super(message, cause);
		String exMsg = cause.getMessage();
		LOGGER.error("自定义异常信息:{},系统异常信息:{}", message, exMsg);
	}

	/**
	 * 只返回系统异常信息
	 * @param cause
	 */
	public BusinessException(Throwable cause) {
		super(cause);
		String exMsg = cause.getMessage();
		LOGGER.error("系统异常信息:{}", exMsg);
	}

	public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		String exMsg = cause.getMessage();
		LOGGER.error("自定义异常信息:{},系统异常信息:{}", message, exMsg);
	}
}

自定义异常类的使用

@RestController
@RequestMapping("wares")
public class WaresController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WaresController.class);
    
    @PostMapping("getAllWares")
    public R getAllWares(HttpServletRequest req, HttpServletResponse res){
        if (ObjectUtils.isBlank(null)) {
            throw new BusinessException("检测到NULL异常信息!");
        }
       	LOGGER.info("你猜我会不会被执行。。。。");
        return R.ok("调用成功了!");
    }
}

自定义返回实体对象

public class R extends HashMap<String, Object> implements Serializable {

    private static final long serialVersionUID = 1L;
    
     public R() {
    }
    
	/**
     * 添加返回值对象data
     * @return R
     */
    public static R ok(Object data) {
        R r = new R();
        r.put("flag", true);
        r.put("msg", "成功!");
        r.put("code", 200);
        r.put("data", data);
        return r;
    }
    
    /**
     * 添加返回值对象和提示信息
     * @return R
     */
    public static R ok(Object data, String msg) {
        R r = new R();
        r.put("flag", true);
        r.put("msg", msg);
        r.put("code", 200);
        r.put("data", data);
        return r;
    }

    /**
     * 返回默认错误信息 flag msg code
     * @return R
     */
    public static R err() {
        R r = new R();
        r.put("flag", false);
        r.put("msg", "服务器返回失败!");
        r.put("code", 500);
        r.put("data", null);
        return r;
    }
    
    /**
     * 添加自定义错误信息 msg
     * @return R
     */
    public static R err(String msg) {
        R r = new R();
        r.put("flag", false);
        r.put("msg", msg);
        r.put("code", 500);
        return r;
    }
}

测试结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值