Browse Source

修改删除参数, 增加异常处理

Gaokun Wang 1 month ago
parent
commit
08919945e7

+ 48 - 4
eco-common/com-web/src/main/java/org/eco/vip/web/core/handler/GlobalExceptionHandler.java

@@ -11,18 +11,24 @@ import cn.hutool.core.exceptions.ExceptionUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import jakarta.servlet.http.HttpServletRequest;
+import jakarta.validation.ConstraintViolation;
+import jakarta.validation.ConstraintViolationException;
+import jakarta.validation.ValidationException;
 import lombok.extern.slf4j.Slf4j;
 import org.eco.vip.orm.exception.BusinessException;
-import org.eco.vip.orm.exception.enums.GlobalErrorCode;
 import org.eco.vip.orm.pojo.CommonResult;
 import org.springframework.validation.FieldError;
 import org.springframework.validation.ObjectError;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.servlet.resource.NoResourceFoundException;
 
 import java.util.List;
 
+import static org.eco.vip.orm.exception.enums.GlobalErrorCode.BAD_REQUEST;
+import static org.eco.vip.orm.exception.enums.GlobalErrorCode.NOT_FOUND;
+
 /**
  * @description GlobalExceptionHandler 全局异常处理器
  *
@@ -33,10 +39,19 @@ import java.util.List;
 @Slf4j
 public class GlobalExceptionHandler {
 
-    public CommonResult<Void> allExceptionHandler(HttpServletRequest request, Throwable throwable) {
+    public CommonResult<?> allExceptionHandler(HttpServletRequest request, Throwable throwable) {
         if (throwable instanceof MethodArgumentNotValidException) {
             return handleMethodArgumentNotValidException((MethodArgumentNotValidException) throwable);
         }
+        if (throwable instanceof ConstraintViolationException) {
+            return constraintViolationExceptionHandler((ConstraintViolationException) throwable);
+        }
+        if (throwable instanceof ValidationException) {
+            return validationException((ValidationException) throwable);
+        }
+        if (throwable instanceof NoResourceFoundException) {
+            return noResourceFoundExceptionHandler(request, (NoResourceFoundException) throwable);
+        }
         return defaultExceptionHandler(request, throwable);
     }
 
@@ -67,9 +82,38 @@ public class GlobalExceptionHandler {
             errorMessage = fieldError.getDefaultMessage();
         }
         if (StrUtil.isEmpty(errorMessage)) {
-            return CommonResult.fail(GlobalErrorCode.BAD_REQUEST);
+            return CommonResult.fail(BAD_REQUEST);
         }
-        return CommonResult.fail(GlobalErrorCode.BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", errorMessage));
+        return CommonResult.fail(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", errorMessage));
+    }
+
+    /**
+     * 处理 Validator 校验不通过产生的异常
+     */
+    @ExceptionHandler(value = ConstraintViolationException.class)
+    public CommonResult<?> constraintViolationExceptionHandler(ConstraintViolationException exception) {
+        log.warn("[constraintViolationExceptionHandler]", exception);
+        ConstraintViolation<?> constraintViolation = exception.getConstraintViolations().iterator().next();
+        return CommonResult.fail(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", constraintViolation.getMessage()));
+    }
+
+    /**
+     * 处理 Dubbo Consumer 本地参数校验时,抛出的 ValidationException 异常
+     */
+    @ExceptionHandler(value = ValidationException.class)
+    public CommonResult<?> validationException(ValidationException exception) {
+        log.warn("[validationException]", exception);
+        // 无法拼接明细的错误信息,因为 Dubbo Consumer 抛出 ValidationException 异常时,是直接的字符串信息,且人类不可读
+        return CommonResult.fail(BAD_REQUEST);
+    }
+
+    /**
+     * 处理 SpringMVC 请求地址不存在
+     */
+    @ExceptionHandler(NoResourceFoundException.class)
+    private CommonResult<?> noResourceFoundExceptionHandler(HttpServletRequest req, NoResourceFoundException throwable) {
+        log.warn("[noResourceFoundExceptionHandler]", throwable);
+        return CommonResult.fail(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", throwable.getResourcePath()));
     }
 
     /**

+ 2 - 2
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/controller/client/ClientController.java

@@ -58,12 +58,12 @@ public class ClientController {
         return success();
     }
 
-    @DeleteMapping("/edit")
+    @PostMapping("/edit")
     public CommonResult<String> edit(@RequestBody @Valid ClientBO clientBO) {
         return success();
     }
 
-    @PostMapping("/delete")
+    @DeleteMapping("/delete")
     public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空") List<String> ids) {
         boolean result = clientService.delete(ids);
         if (!result) {

+ 2 - 2
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/controller/menu/MenuController.java

@@ -64,12 +64,12 @@ public class MenuController {
         return success();
     }
 
-    @DeleteMapping("/edit")
+    @PostMapping("/edit")
     public CommonResult<String> edit(@RequestBody @Valid MenuBO menuBO) {
         return success();
     }
 
-    @PostMapping("/delete")
+    @DeleteMapping("/delete")
     @SaCheckPermission("system:menu:delete")
     public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空") List<String> ids) {
         boolean result = menuService.delete(ids);

+ 2 - 2
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/controller/org/OrgController.java

@@ -61,12 +61,12 @@ public class OrgController {
         return success();
     }
 
-    @DeleteMapping("/edit")
+    @PostMapping("/edit")
     public CommonResult<String> edit(@RequestBody @Valid OrgBO orgBO) {
         return success();
     }
 
-    @PostMapping("/delete")
+    @DeleteMapping("/delete")
     @SaCheckPermission("system:org:delete")
     public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空") List<String> orgIds) {
         boolean result = orgService.delete(orgIds);

+ 3 - 5
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/controller/position/PositionController.java

@@ -64,19 +64,17 @@ public class PositionController {
         return success();
     }
 
-    @DeleteMapping("/edit")
+    @PostMapping("/edit")
     public CommonResult<String> edit(@RequestBody @Valid PositionBO positionBO) {
         return success();
     }
 
-    @PostMapping("/delete")
-    @SaCheckPermission("system:position:delete")
-    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空") List<String> ids) {
+    @DeleteMapping("/delete")
+    public CommonResult<Void> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空") List<String> ids) {
         boolean result = positionService.delete(ids);
         if (!result) {
             return fail("删除职位失败!");
         }
-
         return success();
     }
 }

+ 2 - 2
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/controller/role/RoleController.java

@@ -59,12 +59,12 @@ public class RoleController {
         return success();
     }
 
-    @DeleteMapping("/edit")
+    @PostMapping("/edit")
     public CommonResult<String> edit(@RequestBody @Valid RoleBO roleBO) {
         return success();
     }
 
-    @PostMapping("/delete")
+    @DeleteMapping("/delete")
     public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空") List<String> ids) {
         boolean result = roleService.delete(ids);
         if (!result) {

+ 1 - 1
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/service/position/PositionService.java

@@ -69,7 +69,7 @@ public class PositionService extends BaseService<PositionMapper, Position> imple
 
     @Override
     public boolean delete(List<String> ids) {
-        return false;
+        return this.removeByIds(ids);
     }
 
     @Override