Catch exception after respository.load

Hi,
I’m upgrading axon 2 to axon 4 in out project. Most of changing are made. I know in new axon framework we expect globleIndex in DomainEventEntry table. It’s unique index. Since we already have 200 million data in production table. It’s impossible to add this new column. So we decide to have a new table start to fresh, all projects have new axon are going to add to new table, the projects not upgrade yet so still can go to old table.
Then the problem is data migration. We are going to adding data to the new table. Everying working fine if we have the data in SnapEventEntry. But if we don’t have data in SnapEventEntry (config 100 events add/update snapshot), then we get AggregateNotFoundException which is ok. We want to add this event as first data in the new table. Do we have a way to catch this exception and do my logic from framework level? I don’t want to add same logic in every gateway send command method.
I tried to use @ExceptionHandler inside Aggregate, but not go to my logic in debug mode.
Application code:
gateway.updateSecurityQuestionAccount(updateSecurityQuestionAccountCommand);

CommandHandler
@CommandHandler
public void handleUpdateSecurityQuestionAccount(UpdateSecurityQuestionAccountCommand command) {
load(command.getId(), aggregate → {
aggregate.updateSecurityQuestionAccount(
command.getQuestions()
);
});
}

Load method in base command handler, I tried to catch the exception here but not work
protected final void load(J id, Consumer consumer) {
repository.load(String.valueOf(id)).execute(consumer);
}

Aggregate
@Aggregate
public class PersonAggregateRoot {
public void updateSecurityQuestionAccount(List questions) {
AggregateLifecycle.apply(new SecurityQuestionAccountUpdatedEvent(id, realm, creator, creatorAppId, creation, questions));
}
}

1 Like

Firstly, welcome to the forum, @fanzero! :wave:

Super nice that you’re migrating from Axon Framework 2 to Axon Framework 4. It’s been quite some time for met that I developed anything in AF2, but seeing your question, I don’t think that’s an issues.

Now, back to your specific question concerning the catching of exceptions when loading an aggregate.
There is, sadly, no easy hook into the exception handling of the Repository, @fanzero.

Hence, your best bet IF this is the solution you want to go to, is to wrap the EventSourcingRepository with your own “exception-logging-Repository.” All your implementation would do, is invoke a delegate EventSourcingRepository and catch the exception.

I understand that’s not the most ideal solution. Then again, you have a rather specific scenario, of course. Nonetheless, I hope my delegate-suggestion will help you out, @fanzero!

Be sure to come back with follow-up questions for this migration process you’re doing when necessary!

Thank you, I find the best solution for us, I’m using repository.loadOrCreate to create new aggregate in the new table, it’s handling AggregateNotFoundException internally.

Sounds fair, @fanzero! If that suits your use case. :slight_smile: