Skip to content

Add AggregateNotFoundError for commands/events on non-existent aggregates#213

Merged
jwijgerd merged 3 commits into
mainfrom
copilot/add-aggregate-not-found-error
Jan 4, 2026
Merged

Add AggregateNotFoundError for commands/events on non-existent aggregates#213
jwijgerd merged 3 commits into
mainfrom
copilot/add-aggregate-not-found-error

Conversation

Copilot AI commented Jan 4, 2026

Copy link
Copy Markdown
Contributor

The framework raised AggregateAlreadyExistsError when create commands/events targeted existing aggregates, but had no equivalent error for non-create operations on non-existent aggregates.

Changes

  • Added AggregateNotFoundErrorEvent in main/shared/src/main/java/org/elasticsoftware/akces/errors/

    • Mirrors AggregateAlreadyExistsErrorEvent structure with aggregateId and aggregateName fields
  • Updated KafkaAggregateRuntime

    • Added aggregateNotFound() method to emit error event with generation -1
    • Modified handleCommand() and handleEvent() to check for null state and call aggregateNotFound()
  • Updated AggregateBeanFactoryPostProcessor

    • Added AggregateNotFoundError to COMMAND_HANDLER_SYSTEM_ERRORS list
    • Created EVENT_HANDLER_SYSTEM_ERRORS list with AggregateNotFoundError
    • Updated generateEventHandlerErrorEventTypes() to use new system error list

Behavior

Non-create commands and external events targeting non-existent aggregates now emit AggregateNotFoundError instead of failing silently or throwing exceptions:

// Attempting to credit non-existent wallet
CreditWalletCommand command = new CreditWalletCommand(nonExistentId, "USD", amount);
List<DomainEvent> result = akcesClient.send("TENANT", command).get();
// Returns: [AggregateNotFoundErrorEvent]
Original prompt

On KafkaAggregateRuntime.java, In KafkaAggregateRuntime.handleCommandRecord we currently raise an AggregateAlreadyExistsError however we don't do the same when the Aggregate does not exist. Add support for an AggregateNotFoundError to be raised in a similar fashion. Don't forget to add it the system errors as well in AggregateBeanFactoryPostProcessor


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 4, 2026 11:30
Co-authored-by: jwijgerd <914840+jwijgerd@users.noreply.github.com>
Co-authored-by: jwijgerd <914840+jwijgerd@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for AggregateNotFoundError in handleCommandRecord Add AggregateNotFoundError for commands/events on non-existent aggregates Jan 4, 2026
Copilot AI requested a review from jwijgerd January 4, 2026 11:36
@jwijgerd
jwijgerd marked this pull request as ready for review January 4, 2026 11:36
@jwijgerd
jwijgerd merged commit 60e6638 into main Jan 4, 2026
5 checks passed
@jwijgerd
jwijgerd deleted the copilot/add-aggregate-not-found-error branch January 4, 2026 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants