paascloud/paascloud-master · error · OmcBizException

MDC10021016

MDC10021016

Error message

MDC10021016

What it means

MDC10021016 means '商品库存不足' (insufficient stock for product, productId=%s). getCartOrderItem compares the requested cart quantity against the live product stock and throws when cartItem.getQuantity() > product.getStock(), preventing overselling.

Solutions

  1. Reduce the cart quantity to at most product.getStock() and retry checkout.
  2. Restock the product in MDC if the requested quantity is legitimate.
  3. Catch OmcBizException code 10021016 and tell the user how many units are actually available.
  4. Add optimistic stock reservation/decrement at add-to-cart or checkout time to fail earlier and avoid races.

Example fix

// before
if (cartItem.getQuantity() > product.getStock()) {
    throw new OmcBizException(ErrorCodeEnum.MDC10021016, product.getId());
}
// after (caller-side clamp)
int sellable = Math.min(cartItem.getQuantity(), product.getStock());
if (sellable <= 0) {
    omcCartService.deleteByUserIdProductId(userId, cartItem.getProductId());
    continue;
}
cartItem.setQuantity(sellable);
Defensive patterns

Strategy: validation

Validate before calling

// Java, pre-check stock before checkout
for (OmcCart item : cartList) {
    ProductDto p = mdcProductService.selectById(item.getProductId());
    if (p == null || item.getQuantity() > p.getStock()) {
        throw new OmcBizException(ErrorCodeEnum.MDC10021016, item.getProductId());
    }
}

Try / catch

try {
    orderService.createOrderDoc(loginAuthDto, shippingId);
} catch (OmcBizException e) {
    if (e.getCode() == ErrorCodeEnum.MDC10021016.getCode()) {
        return ResultHelper.fail(e.getCode(), "Insufficient stock, please adjust quantities");
    }
    throw e;
}

Prevention

When it happens

Trigger: Requesting more units of a product than currently in stock — concurrent purchases drained stock after the item was added to the cart, or stock was reduced by admin/inventory adjustments.

Common situations: Flash-sale or high-traffic scenarios where several buyers race for limited stock; stock decremented in MDC without updating carts; user edited quantity in a stale cart page; inventory sync issues between services.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of paascloud/paascloud-master@781281a950 (2026-09-10). Data as JSON: /api/errors/47dfcc360ef6dcdc. Report an issue: GitHub.

Appendix: source

Thrown at paascloud-provider/paascloud-provider-omc/src/main/java/com/paascloud/provider/service/impl/OmcCartServiceImpl.java:325

	public List<OmcOrderDetail> getCartOrderItem(Long userId, List<OmcCart> cartList) {
		List<OmcOrderDetail> orderItemList = Lists.newArrayList();
		if (CollectionUtils.isEmpty(cartList)) {
			throw new OmcBizException(ErrorCodeEnum.OMC10031001, userId);
		}

		//校验购物车的数据,包括产品的状态和数量
		for (OmcCart cartItem : cartList) {
			OmcOrderDetail orderDetail = new OmcOrderDetail();
			ProductDto product = mdcProductService.selectById(cartItem.getProductId());
			if (MdcApiConstant.ProductStatusEnum.ON_SALE.getCode() != product.getStatus()) {
				logger.error("商品不是在线售卖状态, productId={}", product.getId());
				throw new OmcBizException(ErrorCodeEnum.MDC10021015, product.getId());
			}

			//校验库存
			if (cartItem.getQuantity() > product.getStock()) {
				logger.error("商品库存不足, productId={}", product.getId());
				throw new OmcBizException(ErrorCodeEnum.MDC10021016, product.getId());
			}

			orderDetail.setUserId(userId);
			orderDetail.setProductId(product.getId());
			orderDetail.setProductName(product.getName());
			orderDetail.setProductImage(product.getMainImage());
			orderDetail.setCurrentUnitPrice(product.getPrice());
			orderDetail.setQuantity(cartItem.getQuantity());
			orderDetail.setTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(), cartItem.getQuantity()));
			orderItemList.add(orderDetail);
		}
		return orderItemList;
	}

	private OrderItemVo assembleOrderItemVo(OmcOrderDetail orderItem) {
		OrderItemVo orderItemVo = new OrderItemVo();
		orderItemVo.setOrderNo(orderItem.getOrderNo());
		orderItemVo.setProductId(orderItem.getProductId());

View on GitHub (pinned to 781281a950)