dotnet/eShop · error · OrderingDomainException
Discount is not valid
Error message
Discount is not valid
What it means
Thrown by OrderItem.SetNewDiscount when discount is less than zero. A negative discount is meaningless (it would raise the price), so the method rejects it as an invariant violation. It is an OrderingDomainException. Note this guard checks negativity only — a discount larger than the line total is not caught here.
Source
Thrown at src/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs:48
if ((unitPrice * units) < discount)
{
throw new OrderingDomainException("The total of order item is lower than applied discount");
}
ProductId = productId;
ProductName = productName;
UnitPrice = unitPrice;
Discount = discount;
Units = units;
PictureUrl = pictureUrl;
}
public void SetNewDiscount(decimal discount)
{
if (discount < 0)
{
throw new OrderingDomainException("Discount is not valid");
}
Discount = discount;
}
public void AddUnits(int units)
{
if (units < 0)
{
throw new OrderingDomainException("Invalid units");
}
Units += units;
}
}
View on GitHub (pinned to 9b4f9434f4)
Solutions
- Validate discount >= 0 at the call site before invoking SetNewDiscount; reject negative values earlier.
- Fix the upstream discount computation so it never yields a negative figure.
- If a percentage model is used, compute absolute = subtotal * pct/100 which is inherently non-negative for non-negative inputs.
- Add a guard in the command/handler: if (request.Discount < 0) return BadRequest(...).
Example fix
// before
item.SetNewDiscount(computedDiscount);
// after
if (computedDiscount < 0) throw new ArgumentException("Discount cannot be negative");
item.SetNewDiscount(computedDiscount); Defensive patterns
Strategy: validation
Validate before calling
if (discount < 0) throw new ArgumentOutOfRangeException(nameof(discount)); item.SetNewDiscount(discount);
Type guard
static bool IsValidDiscount(decimal discount) => discount >= 0;
Try / catch
try {
item.SetNewDiscount(discount);
} catch (OrderingDomainException ex) when (ex.Message == "Discount is not valid") {
// negative discount upstream — fix the promo calculation, do not retry as-is
} Prevention
- Compute discounts from non-negative inputs (e.g. percentage model).
- Guard discount >= 0 at the handler boundary.
- Add unit tests covering discount == 0 and discount == subtotal.
When it happens
Trigger: Calling existingOrderForProduct.SetNewDiscount(discount) (reached via Order.AddOrderItem when a higher discount is supplied for an existing line, or directly) with a negative discount value. Usually a sign-inversion or arithmetic bug computing the discount.
Common situations: A promo calculator returning a negative amount; subtraction producing a negative remainder; client sending a signed discount field; tests passing -1 as a sentinel.
Related errors
- The total of order item is lower than applied discount
- Invalid number of units
- Invalid units
- Is not possible to change the order status from {OrderStatus
- Empty stock, product item {Name} is sold out
AI-assisted analysis of dotnet/eShop@9b4f9434f4 (2026-08-13).
Data as JSON: /api/errors/80b445c338973774.
Report an issue: GitHub.