diff --git a/cola-framework/cola-core/src/main/java/com/alibaba/cola/event/EventBus.java b/cola-framework/cola-core/src/main/java/com/alibaba/cola/event/EventBus.java index 35d69d61d..a44b7acb1 100644 --- a/cola-framework/cola-core/src/main/java/com/alibaba/cola/event/EventBus.java +++ b/cola-framework/cola-core/src/main/java/com/alibaba/cola/event/EventBus.java @@ -1,19 +1,11 @@ package com.alibaba.cola.event; -import com.alibaba.cola.dto.Response; import com.alibaba.cola.dto.event.Event; -import com.alibaba.cola.exception.BasicErrorCode; -import com.alibaba.cola.exception.AppException; -import com.alibaba.cola.exception.ErrorCodeI; -import com.alibaba.cola.exception.ColaException; import com.alibaba.cola.logger.Logger; import com.alibaba.cola.logger.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.stream.Collector; -import java.util.stream.Collectors; - /** * Event Bus * diff --git a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/Assert.java b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/Assert.java new file mode 100644 index 000000000..69d89fb37 --- /dev/null +++ b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/Assert.java @@ -0,0 +1,90 @@ +package com.alibaba.cola.exception; + +import org.springframework.util.CollectionUtils; + +import java.util.Collection; +import java.util.Map; + +/** + * Assertion utility class that assists in validating arguments. + * + *

Useful for identifying programmer errors early and clearly at runtime. + * + *

For example, if the contract of a public method states it does not + * allow {@code null} arguments, {@code Assert} can be used to validate that + * contract. + * + * For example: + * + *

+ * Assert.notNull(clazz, "The class must not be null");
+ * Assert.isTrue(i > 0, "The value must be greater than zero");
+ * + * This class is empowered by {@link org.springframework.util.Assert} + * + * @author Frank Zhang + * @date 2019-01-13 11:49 AM + */ +public abstract class Assert { + + /** + * Assert a boolean expression, throwing {@code BizException} + * + * for example + * + *
Assert.isTrue(i != 0, errorCode.B_ORDER_illegalNumber, "The order number can not be zero");
+ * + * @param expression a boolean expression + * @param errorCode + * @param message the exception message to use if the assertion fails + * @throws BizException if expression is {@code false} + */ + public static void isTrue(boolean expression, ErrorCodeI errorCode, String message){ + if (!expression) { + throw new BizException(errorCode, message); + } + } + + public static void isTrue(boolean expression, String message) { + isTrue(expression, BasicErrorCode.B_COMMON_ERROR, message); + } + + public static void isTrue(boolean expression) { + isTrue(expression, "[Assertion failed] - this expression must be true"); + } + + public static void notNull(Object object, ErrorCodeI errorCode, String message) { + if (object == null) { + throw new BizException(errorCode, message); + } + } + + public static void notNull(Object object, String message) { + notNull(object, BasicErrorCode.B_COMMON_ERROR, message); + } + + public static void notNull(Object object){ + notNull(object, BasicErrorCode.B_COMMON_ERROR, "[Assertion failed] - the argument "+object+" must not be null"); + } + + public static void notEmpty(Collection collection) { + notEmpty(collection, + "[Assertion failed] - this collection must not be empty: it must contain at least 1 element"); + } + + public static void notEmpty(Collection collection, String message) { + if (CollectionUtils.isEmpty(collection)) { + throw new BizException(message); + } + } + + public static void notEmpty(Map map, String message) { + if (CollectionUtils.isEmpty(map)) { + throw new BizException(message); + } + } + + public static void notEmpty(Map map) { + notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry"); + } +} diff --git a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/AppException.java b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/BaseException.java similarity index 67% rename from cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/AppException.java rename to cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/BaseException.java index e5ec12110..44cd35af2 100644 --- a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/AppException.java +++ b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/BaseException.java @@ -2,21 +2,21 @@ /** * - * Application Exception + * Base Exception is the parent of all exceptions * * @author fulan.zjf 2017年10月22日 上午12:00:39 */ -public abstract class AppException extends RuntimeException{ +public abstract class BaseException extends RuntimeException{ private static final long serialVersionUID = 1L; private ErrorCodeI errCode; - public AppException(String errMessage){ + public BaseException(String errMessage){ super(errMessage); } - public AppException(String errMessage, Throwable e) { + public BaseException(String errMessage, Throwable e) { super(errMessage, e); } diff --git a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/BizException.java b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/BizException.java index 91fa6e8f6..e2dbe2ee7 100644 --- a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/BizException.java +++ b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/BizException.java @@ -1,6 +1,6 @@ package com.alibaba.cola.exception; -public class BizException extends AppException { +public class BizException extends BaseException { private static final long serialVersionUID = 1L; diff --git a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/ColaException.java b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/ColaException.java index ae6d50653..9c39b8e19 100644 --- a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/ColaException.java +++ b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/ColaException.java @@ -6,7 +6,7 @@ * * @author fulan.zjf 2017年10月22日 下午5:56:57 */ -public class ColaException extends AppException { +public class ColaException extends BaseException { private static final long serialVersionUID = 1L; diff --git a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/DefaultExceptionHandler.java b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/DefaultExceptionHandler.java index 77be99831..bf0fe7685 100644 --- a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/DefaultExceptionHandler.java +++ b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/DefaultExceptionHandler.java @@ -24,7 +24,7 @@ public void handleException(Command cmd, Response response, Exception exception) } private void printLog(Command cmd, Response response, Exception exception) { - if(exception instanceof BizException || exception instanceof ParamException){ + if(exception instanceof BizException ){ //biz exception is expected, only warn it logger.warn(buildErrorMsg(cmd, response)); } @@ -41,13 +41,14 @@ private String buildErrorMsg(Command cmd, Response response) { } private void buildResponse(Response response, Exception exception) { - if (exception instanceof AppException) { - ErrorCodeI errCode = ((AppException) exception).getErrCode(); + if (exception instanceof BaseException) { + ErrorCodeI errCode = ((BaseException) exception).getErrCode(); response.setErrCode(errCode.getErrCode()); } else { response.setErrCode(BasicErrorCode.S_UNKNOWN.getErrCode()); } response.setErrMessage(exception.getMessage()); + response.setSuccess(false); } } diff --git a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/ParamException.java b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/ParamException.java deleted file mode 100644 index a56cb46c4..000000000 --- a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/ParamException.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.alibaba.cola.exception; - -public class ParamException extends AppException { - - private static final long serialVersionUID = 1L; - - public ParamException(String errMessage){ - super(errMessage); - this.setErrCode(BasicErrorCode.P_COMMON_ERROR); - } - - public ParamException(ErrorCodeI errCode, String errMessage){ - super(errMessage); - this.setErrCode(errCode); - } - - public ParamException(String errMessage, Throwable e) { - super(errMessage, e); - this.setErrCode(BasicErrorCode.P_COMMON_ERROR); - } -} diff --git a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/Preconditions.java b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/Preconditions.java deleted file mode 100644 index e7d61ee7e..000000000 --- a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/Preconditions.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.alibaba.cola.exception; - -/** - * Created by Danny.Lee on 2017/11/1. - */ -public class Preconditions { - - public static void checkArgument(boolean expression) { - if (!expression) { - throw new ParamException(""); - } - } - - public static void checkArgument(boolean expression, Object errorMessage) { - if (!expression) { - throw new ParamException(String.valueOf(errorMessage)); - } - } - - public static void checkState(boolean expression) { - if (!expression) { - throw new BizException(""); - } - } - - public static void checkState(boolean expression, Object errorMessage) { - if (!expression) { - throw new BizException(String.valueOf(errorMessage)); - } - } -} diff --git a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/SysException.java b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/SysException.java index 6155542fa..7d4b7dbc0 100644 --- a/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/SysException.java +++ b/cola-framework/cola-core/src/main/java/com/alibaba/cola/exception/SysException.java @@ -6,7 +6,7 @@ * @author Frank Zhang * @date 2018-12-29 4:38 PM */ -public class SysException extends AppException { +public class SysException extends BaseException { private static final long serialVersionUID = 1L; diff --git a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/CustomerCommandTest.java b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/CustomerCommandTest.java index 5b1dade66..d809dbcb2 100644 --- a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/CustomerCommandTest.java +++ b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/CustomerCommandTest.java @@ -124,6 +124,6 @@ public void testParamValidationFail(){ //Expect parameter validation error Assert.assertFalse(response.isSuccess()); - Assert.assertEquals(response.getErrCode(), BasicErrorCode.P_COMMON_ERROR.getErrCode()); + Assert.assertEquals(response.getErrCode(), BasicErrorCode.B_COMMON_ERROR.getErrCode()); } } diff --git a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/interceptor/ExceptionHandler.java b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/interceptor/ExceptionHandler.java index ea97f183d..d06511d8d 100644 --- a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/interceptor/ExceptionHandler.java +++ b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/interceptor/ExceptionHandler.java @@ -5,7 +5,6 @@ import com.alibaba.cola.exception.*; import com.alibaba.cola.logger.Logger; import com.alibaba.cola.logger.LoggerFactory; -import com.alibaba.cola.test.customer.AddCustomerCmd; import org.springframework.stereotype.Component; /** @@ -25,8 +24,8 @@ public void handleException(Command cmd, Response response, Exception exception) } private void assembleResponse(Response response, Exception exception) { - if (exception instanceof AppException) { - ErrorCodeI errCode = ((AppException) exception).getErrCode(); + if (exception instanceof BaseException) { + ErrorCodeI errCode = ((BaseException) exception).getErrCode(); response.setErrCode(errCode.getErrCode()); } else { @@ -36,7 +35,7 @@ private void assembleResponse(Response response, Exception exception) { } private void printLog(Command cmd, Response response, Exception exception) { - if(exception instanceof BizException || exception instanceof ParamException){ + if(exception instanceof BizException){ logger.warn(formErrorMessage(cmd, response), exception); } else{ diff --git a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/interceptor/ValidationInterceptor.java b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/interceptor/ValidationInterceptor.java index 4605a65c8..0c7fe92d4 100644 --- a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/interceptor/ValidationInterceptor.java +++ b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/interceptor/ValidationInterceptor.java @@ -3,7 +3,7 @@ import com.alibaba.cola.command.CommandInterceptorI; import com.alibaba.cola.command.PreInterceptor; import com.alibaba.cola.dto.Command; -import com.alibaba.cola.exception.ParamException; +import com.alibaba.cola.exception.BizException; import com.alibaba.cola.validator.ColaMessageInterpolator; import org.hibernate.validator.HibernateValidator; @@ -30,7 +30,7 @@ public void preIntercept(Command command) { Validator validator = factory.getValidator(); Set> constraintViolations = validator.validate(command); constraintViolations.forEach(violation -> { - throw new ParamException(violation.getPropertyPath() + " " + violation.getMessage()); + throw new BizException(violation.getPropertyPath() + " " + violation.getMessage()); }); } } \ No newline at end of file diff --git a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/validator/extension/AddCustomerBizOneValidator.java b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/validator/extension/AddCustomerBizOneValidator.java index e97ba685d..ca165b764 100644 --- a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/validator/extension/AddCustomerBizOneValidator.java +++ b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/validator/extension/AddCustomerBizOneValidator.java @@ -1,5 +1,6 @@ package com.alibaba.cola.test.customer.validator.extension; +import com.alibaba.cola.exception.Assert; import com.alibaba.cola.exception.BizException; import com.alibaba.cola.extension.Extension; import com.alibaba.cola.test.customer.AddCustomerCmd; @@ -20,7 +21,6 @@ public class AddCustomerBizOneValidator implements AddCustomerValidatorExtPt{ public void validate(Object candidate) { AddCustomerCmd addCustomerCmd = (AddCustomerCmd) candidate; //For BIZ TWO CustomerTYpe could not be VIP - if(CustomerType.VIP == addCustomerCmd.getCustomerCO().getCustomerType()) - throw new BizException("Customer Type could not be VIP for Biz One"); + Assert.isTrue(CustomerType.VIP != addCustomerCmd.getCustomerCO().getCustomerType(), "Customer Type could not be VIP for Biz One"); } } diff --git a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/validator/extension/AddCustomerBizTwoValidator.java b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/validator/extension/AddCustomerBizTwoValidator.java index cae12c751..1db55ca6f 100644 --- a/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/validator/extension/AddCustomerBizTwoValidator.java +++ b/cola-framework/cola-core/src/test/java/com/alibaba/cola/test/customer/validator/extension/AddCustomerBizTwoValidator.java @@ -1,6 +1,7 @@ package com.alibaba.cola.test.customer.validator.extension; -import com.alibaba.cola.exception.ParamException; +import com.alibaba.cola.exception.Assert; +import com.alibaba.cola.exception.BizException; import com.alibaba.cola.extension.Extension; import com.alibaba.cola.test.customer.AddCustomerCmd; import com.alibaba.cola.test.customer.Constants; @@ -19,7 +20,7 @@ public class AddCustomerBizTwoValidator implements AddCustomerValidatorExtPt{ public void validate(Object candidate) { AddCustomerCmd addCustomerCmd = (AddCustomerCmd) candidate; //For BIZ TWO CustomerTYpe could not be null - if (addCustomerCmd.getCustomerCO().getCustomerType() == null) - throw new ParamException("CustomerType could not be null"); + Assert.notNull(addCustomerCmd.getCustomerCO()); + Assert.notNull(addCustomerCmd.getCustomerCO().getCustomerType()); } }