From d01db603091eb94e29b63e4d9118e138668204da Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 9 Feb 2023 00:32:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=8E=9F=E5=85=88=20Exceptio?= =?UTF-8?q?nHandler=20=E5=85=B1=E4=BA=AB=20ExceptionInfoHolder=20=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/AllExceptionHandlerConfig.java | 6 ++++-- .../handler/DefaultExceptionHandler.java | 6 ++---- .../config/PlusoneExceptionHandlerConfig.java | 18 ++++++++++++++++++ .../handler/ExceptionInfoHolderFactory.java | 15 --------------- .../InvalidInputExceptionHandler.java | 5 ++--- .../handler/AccountLoginExceptionHandler.java | 6 ++---- .../handler/SaTokenExceptionHandler.java | 7 +++---- 7 files changed, 31 insertions(+), 32 deletions(-) create mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/exception/config/PlusoneExceptionHandlerConfig.java delete mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/exception/handler/ExceptionInfoHolderFactory.java diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java index b74d637..fd05fdb 100644 --- a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java +++ b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java @@ -4,6 +4,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder; + /** * AllExceptionHandlerConfig */ @@ -12,7 +14,7 @@ import org.springframework.context.annotation.Configuration; public class AllExceptionHandlerConfig { @Bean - AllExceptionHandler getAllExceptionHandler() { - return new AllExceptionHandler(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder()); + AllExceptionHandler getAllExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { + return new AllExceptionHandler(exceptionInfoHolder); } } diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java index a63630e..e36f821 100644 --- a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java +++ b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java @@ -11,7 +11,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import lombok.extern.slf4j.Slf4j; -import xyz.zhouxy.plusone.util.RestfulResult; /** * 默认异常的处理器 @@ -40,9 +39,8 @@ import xyz.zhouxy.plusone.util.RestfulResult; @Order(Ordered.LOWEST_PRECEDENCE - 1) @Slf4j public class DefaultExceptionHandler extends BaseExceptionHandler { - - public DefaultExceptionHandler() { - super(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder()); + public DefaultExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { + super(exceptionInfoHolder); set(IllegalArgumentException.class, 4010000, "格式错误", HttpStatus.FORBIDDEN); set(DataAccessException.class, 6030000, "数据库错误", HttpStatus.INTERNAL_SERVER_ERROR, true); set(MethodArgumentNotValidException.class, diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/exception/config/PlusoneExceptionHandlerConfig.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/exception/config/PlusoneExceptionHandlerConfig.java new file mode 100644 index 0000000..fd871aa --- /dev/null +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/exception/config/PlusoneExceptionHandlerConfig.java @@ -0,0 +1,18 @@ +package xyz.zhouxy.plusone.exception.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import xyz.zhouxy.plusone.constant.ErrorCodeConsts; +import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder; + +@Configuration +public class PlusoneExceptionHandlerConfig { + + @Bean + @ConditionalOnMissingBean + ExceptionInfoHolder exceptionInfoHolder() { + return new ExceptionInfoHolder(ErrorCodeConsts.DEFAULT_ERROR_CODE); + } +} diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/exception/handler/ExceptionInfoHolderFactory.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/exception/handler/ExceptionInfoHolderFactory.java deleted file mode 100644 index bf5837d..0000000 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/exception/handler/ExceptionInfoHolderFactory.java +++ /dev/null @@ -1,15 +0,0 @@ -package xyz.zhouxy.plusone.exception.handler; - -import xyz.zhouxy.plusone.constant.ErrorCodeConsts; -import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder; - -public class ExceptionInfoHolderFactory { - - private ExceptionInfoHolderFactory() { - throw new IllegalStateException("Utility class"); - } - - public static ExceptionInfoHolder newDefaultExceptionInfoHolder() { - return new ExceptionInfoHolder(ErrorCodeConsts.DEFAULT_ERROR_CODE); - } -} diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputExceptionHandler.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputExceptionHandler.java index b8eef0f..82ebd4e 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputExceptionHandler.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputExceptionHandler.java @@ -3,13 +3,12 @@ package xyz.zhouxy.plusone.validator; import org.springframework.web.bind.annotation.RestControllerAdvice; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler; -import xyz.zhouxy.plusone.exception.handler.ExceptionInfoHolderFactory; @RestControllerAdvice public class InvalidInputExceptionHandler extends BaseExceptionHandler { - protected InvalidInputExceptionHandler() { - super(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder()); + public InvalidInputExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { + super(exceptionInfoHolder); set(InvalidInputException.class, InvalidInputException.ERROR_CODE, "无效的用户输入"); } } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/AccountLoginExceptionHandler.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/AccountLoginExceptionHandler.java index e742c38..17e4c2c 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/AccountLoginExceptionHandler.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/AccountLoginExceptionHandler.java @@ -7,15 +7,13 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler; -import xyz.zhouxy.plusone.exception.handler.ExceptionInfoHolderFactory; import xyz.zhouxy.plusone.system.application.exception.AccountLoginException; -import xyz.zhouxy.plusone.util.RestfulResult; @RestControllerAdvice public class AccountLoginExceptionHandler extends BaseExceptionHandler { - protected AccountLoginExceptionHandler() { - super(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder()); + public AccountLoginExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { + super(exceptionInfoHolder); } @ExceptionHandler({ AccountLoginException.class }) diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java index f41f4eb..6032a5a 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java @@ -15,8 +15,6 @@ import cn.dev33.satoken.exception.SaTokenException; import cn.dev33.satoken.exception.SameTokenInvalidException; import lombok.extern.slf4j.Slf4j; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler; -import xyz.zhouxy.plusone.exception.handler.ExceptionInfoHolderFactory; -import xyz.zhouxy.plusone.util.RestfulResult; /** * Sa-Token 异常处理器 @@ -27,8 +25,9 @@ import xyz.zhouxy.plusone.util.RestfulResult; @Slf4j public class SaTokenExceptionHandler extends BaseExceptionHandler { - public SaTokenExceptionHandler() { - super(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder()); + + public SaTokenExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { + super(exceptionInfoHolder); set(NotPermissionException.class, 4030103, "会话未能通过权限认证", HttpStatus.FORBIDDEN); set(NotRoleException.class, 4030103, "会话未能通过角色认证", HttpStatus.FORBIDDEN); set(DisableServiceException.class, 4030202, "账号指定服务已被封禁", HttpStatus.FORBIDDEN);