How to gracefully handle aggregate not found errors

I’m building an application using Axon v2 milestone 1 to process FIX protocol messages.
The application is using all the standard facilities, SimpleEventBus, SimpleCommandBus, GenericAggrgateFactory
and my aggregates extends from AbstractAnnotatedAggregateRoot.

Things are working really well.
There is one situation I am trying to handle in a nicer way.
When I receive FIX message(say an Execution Report) with the wrong id, I’m getting an exception deep inside Axon

ERROR org.axonframework.commandhandling.SimpleCommandBus - Processing of a OrderExecuteCommand resulted in an exception:
java.lang.NullPointerException
at org.axonframework.eventsourcing.AbstractAggregateFactory.createAggregate(AbstractAggregateFactory.java:18)
at org.axonframework.eventsourcing.EventSourcingRepository.doLoad(EventSourcingRepository.java:156)
at org.axonframework.eventsourcing.CachingEventSourcingRepository.doLoad(CachingEventSourcingRepository.java:94)
at org.axonframework.eventsourcing.CachingEventSourcingRepository.doLoad(CachingEventSourcingRepository.java:37)
at org.axonframework.repository.AbstractRepository.load(AbstractRepository.java:64)
at org.axonframework.repository.LockingRepository.load(LockingRepository.java:118)
at org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler.loadAggregate(AggregateAnnotationCommandHandler.java:159)
at org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler.access$000(AggregateAnnotationCommandHandler.java:46)
at org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler$1.handle(AggregateAnnotationCommandHandler.java:140)
at org.axonframework.commandhandling.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:62)
at org.axonframework.commandhandling.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:68)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:118)
at org.axonframework.commandhandling.SimpleCommandBus.dispatch(SimpleCommandBus.java:79)

How can I handle this error in a graceful way?

Hi Ming,

you should be getting an AggregateNotFoundException in this case. There must me something else going wrong. I’ll check it out.

Cheers,

Allard

Allard

Sorry I forgot to mention that I was using my CassandraEventStore.
It did not throw a EventStreamNotFoundException, which is needed to cause a AggregateNotFoundException.
After fixing that problem I am now getting AggregateNotFoundException(below).

Is the best way to handle this situation to catch AggregateNotFoundException everytime I call CommandBus.dispatch()?
Is the a way for me register a delegate that gets called whenever this happens?

ERROR org.axonframework.commandhandling.SimpleCommandBus - Processing of a OrderPartialFillCommand resulted in an exception:
org.axonframework.repository.AggregateNotFoundException: The aggregate was not found
at org.axonframework.eventsourcing.EventSourcingRepository.doLoad(EventSourcingRepository.java:150)
at org.axonframework.eventsourcing.CachingEventSourcingRepository.doLoad(CachingEventSourcingRepository.java:94)
at org.axonframework.eventsourcing.CachingEventSourcingRepository.doLoad(CachingEventSourcingRepository.java:37)
at org.axonframework.repository.AbstractRepository.load(AbstractRepository.java:64)
at org.axonframework.repository.LockingRepository.load(LockingRepository.java:118)
at org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler.loadAggregate(AggregateAnnotationCommandHandler.java:154)
at org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler.access$000(AggregateAnnotationCommandHandler.java:45)
at org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler$1.handle(AggregateAnnotationCommandHandler.java:139)
at org.axonframework.commandhandling.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:62)
at org.axonframework.commandhandling.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:68)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:118)
at org.axonframework.commandhandling.SimpleCommandBus.dispatch(SimpleCommandBus.java:79)

Hi Ming,

your email was just in time. I was about to get started on this ;-).

Currently, exceptions are the only way to handle these “not found” scenarios. From the CQRS point of view, they are really exceptional, since you’d usually get the aggregate identifier from some query database (a.k.a. projection).
If performance is a major issue, you could decide to create just a single instance of the exception and throw that each time. I’ve seen that “trick” being used in e.g. Netty to prevent costly stack trace building. Note that only the EventStreamNotFoundException is caught in the repository. If you throw another exception, it will be thrown as-is.

Hope this helps.

Cheers,

Allard