Skip to content

Commit

Permalink
remove ParamException and add Assert
Browse files Browse the repository at this point in the history
  • Loading branch information
fulan.zjf committed Jan 13, 2019
1 parent eaff739 commit 47c1bfa
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>Useful for identifying programmer errors early and clearly at runtime.
*
* <p>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:
*
* <pre class="code">
* Assert.notNull(clazz, "The class must not be null");
* Assert.isTrue(i > 0, "The value must be greater than zero");</pre>
*
* 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
*
* <pre class="code">Assert.isTrue(i != 0, errorCode.B_ORDER_illegalNumber, "The order number can not be zero");</pre>
*
* @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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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 {
Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,7 +30,7 @@ public void preIntercept(Command command) {
Validator validator = factory.getValidator();
Set<ConstraintViolation<Command>> constraintViolations = validator.validate(command);
constraintViolations.forEach(violation -> {
throw new ParamException(violation.getPropertyPath() + " " + violation.getMessage());
throw new BizException(violation.getPropertyPath() + " " + violation.getMessage());
});
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}

0 comments on commit 47c1bfa

Please sign in to comment.