diff --git a/src/main/java/com/wms/common/BaseResponse.java b/src/main/java/com/wms/common/BaseResponse.java new file mode 100644 index 0000000..9aa79b2 --- /dev/null +++ b/src/main/java/com/wms/common/BaseResponse.java @@ -0,0 +1,35 @@ +package com.wms.common; + +import com.wms.exception.ErrorCode; +import lombok.Data; + +import java.io.Serializable; + +/** + * 全局响应封装类 + * + * @param + */ +@Data +public class BaseResponse implements Serializable { + + private int code; + + private T data; + + private String message; + + public BaseResponse(int code, T data, String message) { + this.code = code; + this.data = data; + this.message = message; + } + + public BaseResponse(int code, T data) { + this(code, data, ""); + } + + public BaseResponse(ErrorCode errorCode) { + this(errorCode.getCode(), null, errorCode.getMessage()); + } +} diff --git a/src/main/java/com/wms/exception/BusinessException.java b/src/main/java/com/wms/exception/BusinessException.java new file mode 100644 index 0000000..d520b3e --- /dev/null +++ b/src/main/java/com/wms/exception/BusinessException.java @@ -0,0 +1,31 @@ +package com.wms.exception; + +import lombok.Getter; + +/** + * 自定义业务异常 + */ +@Getter +public class BusinessException extends RuntimeException { + + /** + * 错误码 + */ + private final int code; + + public BusinessException(int code, String message) { + super(message); + this.code = code; + } + + public BusinessException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.code = errorCode.getCode(); + } + + public BusinessException(ErrorCode errorCode, String message) { + super(message); + this.code = errorCode.getCode(); + } + +} diff --git a/src/main/java/com/wms/exception/ThrowUtils.java b/src/main/java/com/wms/exception/ThrowUtils.java new file mode 100644 index 0000000..866887d --- /dev/null +++ b/src/main/java/com/wms/exception/ThrowUtils.java @@ -0,0 +1,52 @@ +package com.wms.exception; + + +/** + * 异常处理工具类 + */ +public class ThrowUtils { + + /** + * 条件成立则抛异常 + * + * @param condition 条件 + * @param runtimeException 异常 + */ + public static void throwIf(boolean condition, RuntimeException runtimeException) { + if (condition) { + throw runtimeException; + } + } + + /** + * 条件成立则抛异常 + * + * @param condition 条件 + * @param errorCode 错误码 + */ + public static void throwIf(boolean condition,Integer errorCode, String message) { + throwIf(condition, new BusinessException(errorCode,message)); + } + + + /** + * 条件成立则抛异常 + * + * @param condition 条件 + * @param errorCode 错误码 + */ + public static void throwIf(boolean condition, ErrorCode errorCode) { + throwIf(condition, new BusinessException(errorCode.getCode(),errorCode.getMessage())); + } + + /** + * 条件成立则抛异常 + * + * @param condition 条件 + * @param errorCode 错误码 + */ + public static void throwIf(boolean condition, ErrorCode errorCode,String message) { + throwIf(condition, new BusinessException( errorCode)); + } + +} diff --git a/src/main/java/com/wms/filter/GlobalExceptionHandler.java b/src/main/java/com/wms/filter/GlobalExceptionHandler.java index 21c07d0..937c5db 100644 --- a/src/main/java/com/wms/filter/GlobalExceptionHandler.java +++ b/src/main/java/com/wms/filter/GlobalExceptionHandler.java @@ -1,20 +1,27 @@ package com.wms.filter; import com.alibaba.fastjson.JSON; +import com.wms.common.BaseResponse; +import com.wms.common.ResultUtils; import com.wms.entity.app.container.ContainerApiLocalResponse; import com.wms.entity.app.mes.MesApiLocalResponse; +import com.wms.exception.BusinessException; +import jakarta.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.validation.ObjectError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestControllerAdvice; import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; -@ControllerAdvice +@RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(value = MethodArgumentNotValidException.class) @ResponseBody @@ -38,4 +45,26 @@ public class GlobalExceptionHandler { return String.format("{\"code\": 999, \"message\": \"%s\"}", message); } + private static final Logger log = LoggerFactory.getLogger(com.wms.filter.GlobalExceptionHandler.class); + + /** + * 拦截未知的运行时异常 + */ + @ExceptionHandler(RuntimeException.class) + public BaseResponse handleRuntimeException(RuntimeException e, HttpServletRequest request) + { + String requestURI = request.getRequestURI(); + log.error("请求地址'{}',发生未知异常.", requestURI, e); + return ResultUtils.error(400, e.getMessage()); + } + + /** + * 业务异常 + */ + @ExceptionHandler(BusinessException.class) + public BaseResponse handleBusinessException(BusinessException e, HttpServletRequest request) + { + log.error(e.getMessage(), e); + return ResultUtils.error(400,e.getMessage()); + } }