package com.tongsheng.common.advice;
import com.tongsheng.common.constant.Result;
import com.tongsheng.common.constant.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler(AuthorizationException.class)
public Result handleAuthorizationException() {
return Result.getResult(ResultInfo.FORBIDDEN_ERROR);
}
@ExceptionHandler(value = IllegalArgumentException.class)
@ResponseBody
public Result exceptionHandlerParamsException(IllegalArgumentException e) {
return Result.getResult(ResultInfo.PARAME_EMPTY, e.getMessage());
}
@ResponseBody
@ExceptionHandler(IncorrectCredentialsException.class)
public Result handleTokenException(IncorrectCredentialsException incorrectCredentialsException) {
log.error("token无效异常", incorrectCredentialsException);
return Result.getResult(ResultInfo.TOKEN_ERROR);
}
@ResponseBody
@ExceptionHandler(MissingServletRequestParameterException.class)
public Result handleMissingParameterException(MissingServletRequestParameterException missingServletRequestParameterException) {
log.error("参数校验异常", missingServletRequestParameterException);
return Result.getResult(ResultInfo.MISSING_PARAMS);
}
@ResponseBody
@ExceptionHandler(NumberFormatException.class)
public Result handleNumberFormatException(NumberFormatException numberFormatException) {
log.error("数字格式化异常", numberFormatException);
return Result.getResult(ResultInfo.NUMBER_FORMAT_EXCEPTION);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Result httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
return Result.getResult(ResultInfo.FAILED, "请求方法出错,你请求的方法" + e.getMethod() + ",该接口支持的方法" + e.getSupportedHttpMethods());
}
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result exceptionHandler(Exception e) {
log.error("未知异常!原因是:", e);
return Result.getResult(ResultInfo.SERVER_INTERNAL_EXCEPTION);
}
}