package com.app.framework.core.exception;

import org.apache.commons.lang.exception.ExceptionUtils;


/**
 * 验证异常工具类
 */
public class ValidUtils {

    /**
     * 抛出异常,expression为true时抛出,否则什么都不做
     * @param expression 是否抛出异常
     * @param msg 异常消息
     */
    public static void valid (boolean expression, String msg) {
        if (expression)
            throw new ValidException(msg);
    }

    /**
     * 抛出异常,expression为true时抛出,否则什么都不做
     * @param expression 是否抛出异常
     */
    public static void validBlank (boolean expression) {
        if (expression) {
            throw new ValidException("必需项不能为空");
        }
    }

    public static boolean isValidException(Throwable e) {
        return ExceptionUtils.indexOfThrowable(e, ValidException.class) != -1;
    }
}
