I have an aggregate which has a map of entities. The entity will handle a command. If I use @AggregateMember and then send a command which references a non-existent entity, it will throw an AggregateEntityNotFoundException. Another way of setting this up might be to annotate a method on the aggregate root and then manually forward the command to the entity only if it exists and possibly throw a different type of exception (eg. IllegalArgumentException) if the entity does not exist. I’m curious about one approach vs the other with regard to what might be considered “best practice”. Does it matter?
I’d personally lean toward having the AggregateEntityNotFoundException, because the command you’re publishing is meant to be handled by that entity.
Hence, the non existence of that entity is quite troublesome and should be dealt with.
The fact it is not found however, could potentially come from the fact the command being published has wrong fields in it.
In that case, IllegalArgumentException would sound reasonable to me too.
Whichever you prefer to me mostly sounds like a question of taste.
If you’d ask me personally, I’d stick with the AggregateEntityNotFoundException, unless the use case you’re in points all arrows towards a wrong argument within the command.
That’s my two cents.