Should I declare Exceptions in same package in Axon?

Viewed 44

I know Axons messgaes should be located in a same packages. but Should also the exceptions used in other apps be in the same packages?

cuz I'm doing Sagas and compensation transcations and

for example when PaymentService applicatons's PaymentAggregates instance will throw NotEnoughMoneyException(if there is no enough money) and OrderManagermentSaga(orchestration Saga instance) have to receivce Exceptions from PaymentService should the exceptions have to same packes?

I've been talking too much the point is "Should also the exceptions used in other apps be in the same place?"

1 Answers

The short answer to that is "it depends." Let me give you a more detailed explanation, too, though.

What it depends on is how you reply to failure scenarios of handling certain messages. Are you trying to serialize the exception and pushing it over the wire? Or, will you catch the exception and adjust it to something else?

The first step is what Axon used to do. The framework has adjusted to wrapping any exception in a HandlerExecutionException in more recent releases. The HandlerExecutionException (there's a command- and query-specific version of this) carries a so-called details Object. Axon will ensure this object is serialized as part of the HandlerExecutionException.

The intent of this details object is so that you can construct your own exception API in your application. This API should indeed be part of the same package where you store your messages simply because it is part of your core API.

I have actually done a recording on how you can populate these detail objects. If you're interested, you can find it here. There's also another sample, taking a slightly different angle (read: it doesn't use the @ExceptionHandler annotation), which you can find here. And lastly, although rather meager, there's also a bit on this in the Reference Guide.

Related