|
@@ -1,101 +0,0 @@
|
|
-package com.zanxiang.game.module.base.handler;
|
|
|
|
-
|
|
|
|
-import com.zanxiang.game.module.base.domain.ResultVO;
|
|
|
|
-import com.zanxiang.game.module.base.enums.HttpStatusEnum;
|
|
|
|
-import com.zanxiang.game.module.base.exception.BaseException;
|
|
|
|
-import com.zanxiang.game.module.base.exception.CustomException;
|
|
|
|
-import com.zanxiang.game.module.base.exception.ParamNullException;
|
|
|
|
-import com.zanxiang.game.module.base.exception.PreAuthorizeException;
|
|
|
|
-import com.zanxiang.game.module.base.utils.StringUtils;
|
|
|
|
-import com.zanxiang.game.module.base.utils.http.AjaxResult;
|
|
|
|
-import org.slf4j.Logger;
|
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
|
-import org.springframework.validation.BindException;
|
|
|
|
-import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
-import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
-import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
-import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
|
|
-
|
|
|
|
-/**
|
|
|
|
- * 全局异常处理器
|
|
|
|
- */
|
|
|
|
-@RestControllerAdvice
|
|
|
|
-public class GlobalExceptionHandler {
|
|
|
|
- private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 基础异常
|
|
|
|
- */
|
|
|
|
- @ExceptionHandler(BaseException.class)
|
|
|
|
- public ResultVO<?> baseException(BaseException e) {
|
|
|
|
- return ResultVO.fail(e.getMessage());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 业务异常
|
|
|
|
- */
|
|
|
|
- @ExceptionHandler(CustomException.class)
|
|
|
|
- public ResultVO<?> businessException(CustomException e) {
|
|
|
|
- if (StringUtils.isNull(e.getCode())) {
|
|
|
|
- return ResultVO.fail(e.getMessage());
|
|
|
|
- }
|
|
|
|
- return new ResultVO<>(e.getCode(), e.getMessage());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 参数异常
|
|
|
|
- */
|
|
|
|
- @ExceptionHandler(ParamNullException.class)
|
|
|
|
- public ResultVO<?> paramNullException(CustomException e) {
|
|
|
|
- if (StringUtils.isNull(e.getCode())) {
|
|
|
|
- return new ResultVO<>(HttpStatusEnum.PARAM_IS_NULL.getCode(), HttpStatusEnum.PARAM_IS_NULL.getMsg() + e.getMessage());
|
|
|
|
- }
|
|
|
|
- return new ResultVO<>(e.getCode(), e.getMessage());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @ExceptionHandler(Exception.class)
|
|
|
|
- public ResultVO<?> handleException(Exception e) {
|
|
|
|
- log.error(e.getMessage(), e);
|
|
|
|
- return ResultVO.fail("操作异常");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 自定义验证异常
|
|
|
|
- */
|
|
|
|
- @ExceptionHandler(BindException.class)
|
|
|
|
- public ResultVO<?> validatedBindException(BindException e) {
|
|
|
|
- log.error(e.getMessage(), e);
|
|
|
|
- String message = e.getAllErrors().get(0).getDefaultMessage();
|
|
|
|
- return ResultVO.fail(message);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 参数类型不匹配导致转换异常
|
|
|
|
- *
|
|
|
|
- * @param e
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- @ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
|
|
|
- public ResultVO<?> mismatchErrorHandler(MethodArgumentTypeMismatchException e) {
|
|
|
|
- log.error("方法:{},字段:{},参数:{},错误信息:{}", e.getParameter().getMethod(), e.getName(), e.getValue(), e.getMessage());
|
|
|
|
- return ResultVO.fail("参数异常,请勿非法操作");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 自定义验证异常
|
|
|
|
- */
|
|
|
|
- @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
|
- public Object validExceptionHandler(MethodArgumentNotValidException e) {
|
|
|
|
- log.error(e.getMessage(), e);
|
|
|
|
- String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
|
|
|
- return AjaxResult.error(message);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 权限异常
|
|
|
|
- */
|
|
|
|
- @ExceptionHandler(PreAuthorizeException.class)
|
|
|
|
- public ResultVO<?> preAuthorizeException(PreAuthorizeException e) {
|
|
|
|
- return ResultVO.fail("没有权限,请联系管理员授权");
|
|
|
|
- }
|
|
|
|
-}
|
|
|