From a265256f4e4bf47b24280afcd7f992a7160b0b11 Mon Sep 17 00:00:00 2001 From: laifeng <1572427111@qq.com> Date: Mon, 17 Jun 2024 16:38:18 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E5=B0=86?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=E8=BF=9B=E8=A1=8C=E4=BA=86=E7=AE=80?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E5=87=8F=E5=B0=91=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/core/util/AuthExceptionUtil.java | 15 +++++---------- .../core/handler/GlobalExceptionUtil.java | 18 ++++++------------ 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/snowy-plugin/snowy-plugin-auth/src/main/java/vip/xiaonuo/auth/core/util/AuthExceptionUtil.java b/snowy-plugin/snowy-plugin-auth/src/main/java/vip/xiaonuo/auth/core/util/AuthExceptionUtil.java index 0358ab88..54d76038 100644 --- a/snowy-plugin/snowy-plugin-auth/src/main/java/vip/xiaonuo/auth/core/util/AuthExceptionUtil.java +++ b/snowy-plugin/snowy-plugin-auth/src/main/java/vip/xiaonuo/auth/core/util/AuthExceptionUtil.java @@ -29,31 +29,26 @@ public class AuthExceptionUtil { **/ public static CommonResult getCommonResult(Exception e) { CommonResult commonResult; - if (e instanceof NotLoginException) { + if (e instanceof NotLoginException notLoginException) { // 如果是未登录异常 401 - NotLoginException notLoginException = (NotLoginException) e; commonResult = CommonResult.get(HttpStatus.HTTP_UNAUTHORIZED, notLoginException.getMessage(), null); - } else if (e instanceof NotRoleException) { + } else if (e instanceof NotRoleException notRoleException) { // 如果是角色异常 403 - NotRoleException notRoleException = (NotRoleException) e; commonResult = CommonResult.get(HttpStatus.HTTP_FORBIDDEN, "无此角色:" + notRoleException.getRole() + ",接口地址:" + CommonServletUtil.getRequest().getServletPath(), null); - } else if (e instanceof NotPermissionException) { + } else if (e instanceof NotPermissionException notPermissionException) { // 如果是权限异常 403 - NotPermissionException notPermissionException = (NotPermissionException) e; commonResult = CommonResult.get(HttpStatus.HTTP_FORBIDDEN, "无此权限:" + notPermissionException.getPermission(), null); - } else if (e instanceof DisableServiceException) { + } else if (e instanceof DisableServiceException disableServiceException) { // 如果是被封禁异常 403 - DisableServiceException disableServiceException = (DisableServiceException) e; commonResult = CommonResult.get(HttpStatus.HTTP_FORBIDDEN, "账号被封禁:" + disableServiceException.getDisableTime() + "秒后解封", null); - } else if (e instanceof SaTokenException) { + } else if (e instanceof SaTokenException saTokenException) { // 如果是SaToken异常 直接返回 - SaTokenException saTokenException = (SaTokenException) e; commonResult = CommonResult.error(saTokenException.getMessage()); } else { // 未知异常才打印 diff --git a/snowy-web-app/src/main/java/vip/xiaonuo/core/handler/GlobalExceptionUtil.java b/snowy-web-app/src/main/java/vip/xiaonuo/core/handler/GlobalExceptionUtil.java index c7abee11..a7ab5368 100644 --- a/snowy-web-app/src/main/java/vip/xiaonuo/core/handler/GlobalExceptionUtil.java +++ b/snowy-web-app/src/main/java/vip/xiaonuo/core/handler/GlobalExceptionUtil.java @@ -81,25 +81,21 @@ public class GlobalExceptionUtil { log.error(">>> 参数传递格式异常:", e); // 如果是JSON参数格式错误异常 415 commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, "参数格式错误", null); - } else if (e instanceof MethodArgumentNotValidException) { + } else if (e instanceof MethodArgumentNotValidException methodArgumentNotValidException) { // 如果是参数校验异常(MethodArgumentNotValidException) 415 - MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e; commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, getArgNotValidMessage(methodArgumentNotValidException.getBindingResult()), null); - } else if (e instanceof BindException) { + } else if (e instanceof BindException bindException) { // 如果是参数校验异常(BindException) 415 - BindException bindException = (BindException) e; commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, getArgNotValidMessage(bindException.getBindingResult()), null); - } else if (e instanceof ConstraintViolationException) { + } else if (e instanceof ConstraintViolationException constraintViolationException) { // 如果是参数校验异常(ConstraintViolationException) 415 - ConstraintViolationException constraintViolationException = (ConstraintViolationException) e; commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, getArgNotValidMessage(constraintViolationException.getConstraintViolations()), null); - } else if (e instanceof MissingServletRequestParameterException) { + } else if (e instanceof MissingServletRequestParameterException missingServletRequestParameterException) { // 如果是参数校验异常(MissingServletRequestParameterException) 415 - MissingServletRequestParameterException missingServletRequestParameterException = (MissingServletRequestParameterException) e; commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, missingServletRequestParameterException.getMessage(), null); } else if (e instanceof MultipartException) { @@ -120,8 +116,7 @@ public class GlobalExceptionUtil { Throwable cause = e.getCause(); if (cause instanceof PersistenceException) { Throwable secondCause = cause.getCause(); - if (secondCause instanceof CommonException) { - CommonException commonException = (CommonException) secondCause; + if (secondCause instanceof CommonException commonException) { commonResult = CommonResult.get(commonException.getCode(), commonException.getMsg(), null); } else { log.error(">>> 数据操作异常:", e); @@ -131,10 +126,9 @@ public class GlobalExceptionUtil { log.error(">>> 数据操作异常:", e); commonResult = CommonResult.error("数据操作异常"); } - } else if (e instanceof CommonException) { + } else if (e instanceof CommonException commonException) { // 通用业务异常,直接返回给前端 - CommonException commonException = (CommonException) e; commonResult = CommonResult.get(commonException.getCode(), commonException.getMsg(), null); } else { // 未知异常打印详情