From e3aa3d1aca4aa6375d837470d22335b0276d6663 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Fri, 29 Nov 2024 18:26:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=B3=BB=E7=BB=9F=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E3=80=81=E4=B8=9A=E5=8A=A1=E5=BC=82=E5=B8=B8=EF=BC=8C?= =?UTF-8?q?=E5=8C=BA=E5=88=86=20=E7=B3=BB=E7=BB=9F=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E4=B8=8D=E7=94=A8=E5=86=99Code=20=E4=B8=9A=E5=8A=A1=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=BF=85=E9=A1=BB=E8=A6=81=E5=86=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DependencyInjectionPostProcessor.java | 12 +++--- .../framework/exception/BaseException.java | 26 +++++++++++++ .../exception/BusinessException.java | 28 ++++---------- .../DependencyInjectionException.java | 18 ++------- .../exception/GlobalExceptionHandler.java | 37 +++---------------- .../framework/exception/SystemException.java | 17 +++++++++ 6 files changed, 67 insertions(+), 71 deletions(-) create mode 100644 backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BaseException.java create mode 100644 backend/src/main/java/com/qqchen/deploy/backend/framework/exception/SystemException.java diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/config/DependencyInjectionPostProcessor.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/config/DependencyInjectionPostProcessor.java index 7771d381..994cf2e6 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/framework/config/DependencyInjectionPostProcessor.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/config/DependencyInjectionPostProcessor.java @@ -60,7 +60,9 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp throw e; } catch (Exception e) { log.error("Unexpected error during dependency injection for bean: {}", beanName, e); - throw new DependencyInjectionException(ResponseCode.DEPENDENCY_INJECTION_SERVICE_NOT_FOUND, new Object[] {beanName, e.getMessage()}, e); + throw new DependencyInjectionException( + String.format("Failed to inject dependencies for bean: %s", beanName), + e); } return bean; } @@ -85,7 +87,9 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp service.getClass().getSimpleName(), controller.getClass().getSimpleName()); } catch (Exception e) { - throw new DependencyInjectionException(ResponseCode.DEPENDENCY_INJECTION_SERVICE_NOT_FOUND, new Object[] {entityClass.getSimpleName(), e.getMessage()}, e); + throw new DependencyInjectionException( + String.format("Failed to inject service for controller: %s", controller.getClass().getSimpleName()), + e); } return controller; @@ -150,10 +154,8 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp } catch (DependencyInjectionException e) { throw e; } catch (Exception e) { - log.error("Unexpected error while injecting dependencies for {}", entityClass.getSimpleName(), e); throw new DependencyInjectionException( - ResponseCode.DEPENDENCY_INJECTION_SERVICE_NOT_FOUND, - new Object[] {entityClass.getSimpleName(), e.getMessage()}, + String.format("Failed to inject dependencies for service: %s", service.getClass().getSimpleName()), e); } } diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BaseException.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BaseException.java new file mode 100644 index 00000000..7cb637e2 --- /dev/null +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BaseException.java @@ -0,0 +1,26 @@ +package com.qqchen.deploy.backend.framework.exception; + +import com.qqchen.deploy.backend.framework.enums.ResponseCode; +import lombok.Getter; + +@Getter +public abstract class BaseException extends RuntimeException { + + private final ResponseCode errorCode; + + private final Object[] args; + + protected BaseException(ResponseCode errorCode) { + this(errorCode, null, null); + } + + protected BaseException(ResponseCode errorCode, Object[] args) { + this(errorCode, args, null); + } + + protected BaseException(ResponseCode errorCode, Object[] args, Throwable cause) { + super(cause); + this.errorCode = errorCode; + this.args = args; + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BusinessException.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BusinessException.java index 2d6b44d9..d9ce6940 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BusinessException.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BusinessException.java @@ -1,31 +1,17 @@ package com.qqchen.deploy.backend.framework.exception; import com.qqchen.deploy.backend.framework.enums.ResponseCode; -import lombok.Getter; - -@Getter -public class BusinessException extends RuntimeException { - - private final ResponseCode errorCode; - - private final Object[] args; - - private Throwable cause; +/** + * 业务异常,会直接返回给前端具体的错误信息 + */ +public class BusinessException extends BaseException { + public BusinessException(ResponseCode errorCode) { - this(errorCode, null); + super(errorCode); } public BusinessException(ResponseCode errorCode, Object[] args) { - super(); - this.errorCode = errorCode; - this.args = args; - } - - public BusinessException(ResponseCode errorCode, Object[] args, Throwable cause) { - super(); - this.errorCode = errorCode; - this.args = args; - this.cause = cause; + super(errorCode, args); } } \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/DependencyInjectionException.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/DependencyInjectionException.java index 3fc51e49..d564b95d 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/DependencyInjectionException.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/DependencyInjectionException.java @@ -1,21 +1,11 @@ package com.qqchen.deploy.backend.framework.exception; -import com.qqchen.deploy.backend.framework.enums.ResponseCode; - /** - * 依赖注入异常 + * 依赖注入异常,属于系统异常 */ -public class DependencyInjectionException extends BusinessException { +public class DependencyInjectionException extends SystemException { - public DependencyInjectionException(ResponseCode errorCode) { - super(errorCode); - } - - public DependencyInjectionException(ResponseCode errorCode, Object[] args) { - super(errorCode, args); - } - - public DependencyInjectionException(ResponseCode errorCode, Object[] args, Throwable cause) { - super(errorCode, args, cause); + public DependencyInjectionException(String message, Throwable cause) { + super(message, cause); } } \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/GlobalExceptionHandler.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/GlobalExceptionHandler.java index fbde0c38..4683a418 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/GlobalExceptionHandler.java @@ -2,18 +2,13 @@ package com.qqchen.deploy.backend.framework.exception; import com.qqchen.deploy.backend.framework.api.Response; import com.qqchen.deploy.backend.framework.enums.ResponseCode; -import jakarta.persistence.OptimisticLockException; -import jakarta.persistence.PessimisticLockException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; -import org.springframework.context.NoSuchMessageException; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; -import java.util.ConcurrentModificationException; - @Slf4j @RestControllerAdvice public class GlobalExceptionHandler { @@ -21,16 +16,8 @@ public class GlobalExceptionHandler { @Autowired private MessageSource messageSource; - private String getMessage(String key) { - try { - return messageSource.getMessage(key, null, LocaleContextHolder.getLocale()); - } catch (NoSuchMessageException e) { - return key; - } - } - @ExceptionHandler(BusinessException.class) - public Response handleApiException(BusinessException e) { + public Response handleBusinessException(BusinessException e) { String message = messageSource.getMessage( e.getErrorCode().getMessageKey(), e.getArgs(), @@ -39,27 +26,15 @@ public class GlobalExceptionHandler { return Response.error(e.getErrorCode(), message); } - @ExceptionHandler(OptimisticLockException.class) - public Response handleOptimisticLockException(OptimisticLockException ex) { - log.warn("Optimistic lock exception", ex); - return Response.error(ResponseCode.OPTIMISTIC_LOCK_ERROR, getMessage("system.optimistic.lock.error")); - } - - @ExceptionHandler(PessimisticLockException.class) - public Response handlePessimisticLockException(PessimisticLockException ex) { - log.warn("Pessimistic lock exception", ex); - return Response.error(ResponseCode.PESSIMISTIC_LOCK_ERROR, getMessage("system.pessimistic.lock.error")); - } - - @ExceptionHandler(ConcurrentModificationException.class) - public Response handleConcurrentModificationException(ConcurrentModificationException ex) { - log.warn("Concurrent modification exception", ex); - return Response.error(ResponseCode.CONCURRENT_UPDATE_ERROR, getMessage("system.concurrent.update.error")); + @ExceptionHandler(SystemException.class) + public Response handleSystemException(SystemException e) { + log.error("System error occurred", e); + return Response.error(ResponseCode.ERROR); } @ExceptionHandler(Exception.class) public Response handleException(Exception e) { - log.error("系统错误", e); + log.error("Unexpected error occurred", e); return Response.error(ResponseCode.ERROR); } } \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/SystemException.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/SystemException.java new file mode 100644 index 00000000..93037f76 --- /dev/null +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/SystemException.java @@ -0,0 +1,17 @@ +package com.qqchen.deploy.backend.framework.exception; + +import com.qqchen.deploy.backend.framework.enums.ResponseCode; + +/** + * 系统异常,只返回系统错误给前端,但会记录详细日志 + */ +public class SystemException extends BaseException { + + public SystemException(String message, Throwable cause) { + super(ResponseCode.ERROR, new Object[] {message}, cause); + } + + public SystemException(ResponseCode errorCode, Object[] args, Throwable cause) { + super(errorCode, args, cause); + } +} \ No newline at end of file