package com.sinata.rest.common.aop;
|
|
import com.sinata.rest.common.ApiUtils;
|
import com.sinata.rest.common.exception.BizExceptionEnum;
|
import io.jsonwebtoken.JwtException;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
import javax.validation.ConstraintViolationException;
|
|
/**
|
* 全局的的异常拦截器(拦截所有的控制器)(带有@RequestMapping注解的方法上都会拦截)
|
*/
|
@Slf4j
|
@ControllerAdvice
|
public class GlobalExceptionHandler extends BaseControllerExceptionHandler {
|
|
/**
|
* 拦截jwt相关异常
|
*/
|
@ExceptionHandler(JwtException.class)
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
@ResponseBody
|
public Object jwtException(JwtException e) {
|
return ApiUtils.returnNG(BizExceptionEnum.TOKEN_ERROR.getCode(), BizExceptionEnum.TOKEN_ERROR.getMessage());
|
}
|
|
/**
|
* 参数异常
|
*
|
* @param e
|
* @return
|
*/
|
@ExceptionHandler({IllegalArgumentException.class, ConstraintViolationException.class})
|
@ResponseStatus(HttpStatus.OK)
|
@ResponseBody
|
public Object resultParamError(Exception e) {
|
StackTraceElement stackTraceElement = e.getStackTrace()[1];
|
log.error("运行异常:{},{},{},{}", stackTraceElement.getFileName(), stackTraceElement.getMethodName(), stackTraceElement.getLineNumber(), e.getMessage());
|
return ApiUtils.returnNG(e.getMessage());
|
}
|
}
|