package org.springblade.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: lianghanqiang
 * @description: 全局异常处理
 * @since: 7/29/21 -- 2:22 PM
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	/**
	 * 	业务自定义异常处理
	 * */
	@ExceptionHandler(BizException.class)
	public R bizHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {

		BizException exception = (BizException) e;

		log.error("全局异常[BizException]:", e);

		return R.fail(exception.getStatus(),exception.getErrMsg());
	}

	/**
	 * 	运行时异常处理
	 * */
	@ExceptionHandler(RuntimeException.class)
	public R runtimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {

		log.error("全局异常[runTimeExceptionException]:", e);

		return R.fail(e.getMessage());
	}
}
