package com.sinata.rest.common.aop;
|
|
import com.sinata.rest.common.ApiUtils;
|
import com.sinata.rest.core.exception.GunsException;
|
import com.sinata.rest.core.exception.GunsExceptionEnum;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
/**
|
* 全局的的异常拦截器(拦截所有的控制器)(带有@RequestMapping注解的方法上都会拦截)
|
*/
|
@Slf4j
|
public class BaseControllerExceptionHandler {
|
|
/**
|
* 拦截业务异常
|
*/
|
@ResponseBody
|
@ExceptionHandler(GunsException.class)
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
public Object notFount(GunsException e) {
|
log.error("业务异常:", e);
|
return ApiUtils.returnNG(e.getCode(), e.getMessage());
|
}
|
|
/**
|
* 拦截未知的运行时异常
|
*/
|
@ResponseBody
|
@ExceptionHandler(RuntimeException.class)
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
public Object notFount(RuntimeException e) {
|
log.error("服务器异常:{}", e);
|
return ApiUtils.returnNG(GunsExceptionEnum.SERVER_ERROR.getCode(), GunsExceptionEnum.SERVER_ERROR.getMessage());
|
}
|
|
}
|