增加系统异常、业务异常,区分

系统异常不用写Code
业务异常必须要写。
This commit is contained in:
dengqichen 2024-11-29 18:26:43 +08:00
parent 65e18a90d4
commit e3aa3d1aca
6 changed files with 67 additions and 71 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<Void> 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<Void> 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<Void> 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);
}
}

View File

@ -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);
}
}