|
@@ -4,31 +4,47 @@ import cn.seecoder.data.server.utils.enums.ErrorCodeEnum;
|
|
|
import cn.seecoder.data.server.vo.Result;
|
|
import cn.seecoder.data.server.vo.Result;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
|
|
-import cn.seecoder.data.server.utils.enums.ErrorCodeEnum;
|
|
|
|
|
|
|
+import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
@RestControllerAdvice
|
|
@RestControllerAdvice
|
|
|
public class GlobalExceptionHandler {
|
|
public class GlobalExceptionHandler {
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 处理 Exception 异常
|
|
|
|
|
- * @param e 异常
|
|
|
|
|
- * @return 处理结果
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ //请求参数不合法
|
|
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
|
|
+ public Result<Object> handleMethodArgumentNotValidatedException(MethodArgumentNotValidException e) {
|
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
|
+ return Result.buildFailure(ErrorCodeEnum.SERVICE_ERROR_C0004);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //请求参数不匹配
|
|
|
|
|
+ @ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
|
|
|
|
+ public Result<Object> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
|
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
|
+ return Result.buildFailure(ErrorCodeEnum.SERVICE_ERROR_C0005);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //请求方法不支持
|
|
|
|
|
+ @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
|
|
+ public Result<Object> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
|
+ return Result.buildFailure(ErrorCodeEnum.SERVICE_ERROR_C0006);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //处理通用异常
|
|
|
@ExceptionHandler(Exception.class)
|
|
@ExceptionHandler(Exception.class)
|
|
|
public Result<Object> handlerException(Exception e) {
|
|
public Result<Object> handlerException(Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
logger.error(e.getMessage(), e);
|
|
|
return Result.buildFailure(ErrorCodeEnum.SYSTEM_ERROR_B0001);
|
|
return Result.buildFailure(ErrorCodeEnum.SYSTEM_ERROR_B0001);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 处理自定义异常
|
|
|
|
|
- * @param e 异常
|
|
|
|
|
- * @return 处理结果
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ //处理自定义异常
|
|
|
@ExceptionHandler(GlobalException.class)
|
|
@ExceptionHandler(GlobalException.class)
|
|
|
public Result<Object> handlerGlobalException(GlobalException e) {
|
|
public Result<Object> handlerGlobalException(GlobalException e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
logger.error(e.getMessage(), e);
|