kgrzybek/modular-monolith-with-ddd · error · InvalidCommandException

Pricelist item for activation must exist.

Error message

Pricelist item for activation must exist.

What it means

Thrown by ActivatePriceListItemCommandHandler when _aggregateStore.Load(new PriceListItemId(...)) returns null. Activation requires an existing PriceListItem aggregate; a missing item rejects the command with InvalidCommandException (HTTP 400).

Source

Thrown at src/Modules/Payments/Application/PriceListItems/ActivatePriceListItem/ActivatePriceListItemCommandHandler.cs:23

namespace CompanyName.MyMeetings.Modules.Payments.Application.PriceListItems.ActivatePriceListItem
{
    internal class ActivatePriceListItemCommandHandler : ICommandHandler<ActivatePriceListItemCommand>
    {
        private readonly IAggregateStore _aggregateStore;

        public ActivatePriceListItemCommandHandler(IAggregateStore aggregateStore)
        {
            _aggregateStore = aggregateStore;
        }

        public async Task Handle(ActivatePriceListItemCommand command, CancellationToken cancellationToken)
        {
            var priceListItem = await _aggregateStore.Load(new PriceListItemId(command.PriceListItemId));

            if (priceListItem == null)
            {
                throw new InvalidCommandException(["Pricelist item for activation must exist."]);
            }

            priceListItem.Activate();

            _aggregateStore.AppendChanges(priceListItem);
        }
    }
}

View on GitHub (pinned to 91c8ef24b4)

Solutions

  1. Verify the PriceListItemId exists in the price list before activation.
  2. Check the aggregate store/event stream for the id and its tenant scope.
  3. Pre-check existence in the controller and return 404.
  4. Map InvalidCommandException to 400/404 at the API boundary.

Example fix

// before
await _commandDispatcher.SendAsync(new ActivatePriceListItemCommand(itemId));

// after
var item = await _priceListQueries.GetItemAsync(itemId);
if (item is null) return NotFound("Price list item not found.");
await _commandDispatcher.SendAsync(new ActivatePriceListItemCommand(itemId));
Defensive patterns

Strategy: validation

Validate before calling

var item = await _priceListQueries.GetItemAsync(itemId);
if (item is null) return NotFound("Price list item not found.");
await _commandDispatcher.SendAsync(new ActivatePriceListItemCommand(itemId));

Type guard

public static bool IsValidItemId(ActivatePriceListItemCommand c) =>
    c.PriceListItemId != Guid.Empty;

Try / catch

try { await _commandDispatcher.SendAsync(cmd); }
catch (InvalidCommandException ex) when (ex.Errors.Any(m => m.Contains("must exist")))
{ return NotFound(new { errors = ex.Errors }); }

Prevention

When it happens

Trigger: Dispatching ActivatePriceListItemCommand with a PriceListItemId that has no aggregate: wrong/empty Guid, item not created, item from a different country/period scope, or aggregate store cannot find the stream.

Common situations: Admin console activates an item from a stale list; event-sourcing stream missing or not yet snapshotted; tenant scoping returns nothing.

Related errors


AI-assisted analysis of kgrzybek/modular-monolith-with-ddd@91c8ef24b4 (2026-08-13). Data as JSON: /api/errors/5a52fd52551fdd54. Report an issue: GitHub.