alibaba/COLA · error · BizException

${errorCode}

${errorCode}

Error message

${errMessage}

What it means

Assert.isTrue(boolean, String errorCode, String errMessage) is a convenience assertion that throws BizException(errorCode, errMessage) when the given expression is false. The message and error code are caller-supplied, so they appear as ${errMessage}/${errorCode} in the template.

Source

Thrown at cola-components/cola-component-exception/src/main/java/com/alibaba/cola/exception/Assert.java:42

 * @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 errMessage the exception message to use if the assertion fails
     * @throws BizException if expression is {@code false}
     */
    public static void isTrue(boolean expression, String errorCode, String errMessage) {
        if (!expression) {
            throw new BizException(errorCode, errMessage);
        }
    }

    /**
     * Assert a boolean expression, if expression is true, throwing {@code BizException}
     *
     * for example
     *
     * <pre class="code">Assert.isFalse(i == 0, errorCode.B_ORDER_illegalNumber, "The order number can not be zero");</pre>
     *
     * This is more intuitive than isTure.
     */
    public static void isFalse(boolean expression, String errorCode, String errMessage) {
        if (expression) {
            throw new BizException(errorCode, errMessage);
        }
    }

View on GitHub (pinned to 352e1a8675)

Solutions

  1. Inspect the thrown errMessage/errCode to identify which assertion failed and why the expression was false.
  2. Validate the input/state before the call, or fix the upstream logic that produced the unexpected value.
  3. If the assertion is wrong, correct the condition; if it is a legitimate business rule failure, handle BizException at the API boundary.

Example fix

// before
Assert.isTrue(order.getCount() > 0, ErrorCode.B_ORDER_illegalNumber, "count must be positive"); // throws when count<=0
// after
if (order.getCount() <= 0) {
    return Response.buildFailure(ErrorCode.B_ORDER_illegalNumber.getErrCode(), "count must be positive");
}
Assert.isTrue(order.getCount() > 0, ErrorCode.B_ORDER_illegalNumber, "count must be positive");
Defensive patterns

Strategy: validation

Validate before calling

if (order == null || order.getCount() <= 0) {
    return Response.buildFailure(ErrorCode.B_ORDER_illegalNumber.getErrCode(), "count must be positive");
}
Assert.isTrue(order.getCount() > 0, ErrorCode.B_ORDER_illegalNumber, "count must be positive");

Try / catch

try {
    Assert.isTrue(expr, errorCode, errMessage);
} catch (BizException e) {
    return Response.buildFailure(e.getErrCode(), e.getErrMessage());
}

Prevention

When it happens

Trigger: Calling Assert.isTrue(expression, errorCode, msg) where expression evaluates to false at runtime — e.g. Assert.isTrue(order != null, ErrorCode.B_ORDER_notFound, "Order not found").

Common situations: Precondition checks on request parameters or loaded entities failing; null/invalid inputs reaching service code that uses COLA's Assert utility for validation.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of alibaba/COLA@352e1a8675 (2026-09-08). Data as JSON: /api/errors/fd3b6613a288e4ba. Report an issue: GitHub.