Skip to content

Commit 97ab713

Browse files
committed
Create dedicated UnconfirmedOrderException
Create a dedicated UnconfirmedOrderException and use it from the OrderAggregate instead of the IllegalStateException BAEL-2435
1 parent 65183f0 commit 97ab713

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

axon/src/main/java/com/baeldung/axon/commandmodel/OrderAggregate.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
1414
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
1515
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
16+
import com.baeldung.axon.coreapi.exceptions.UnconfirmedOrderException;
1617

1718
@Aggregate
1819
public class OrderAggregate {
@@ -34,7 +35,7 @@ public void handle(ConfirmOrderCommand command) {
3435
@CommandHandler
3536
public void handle(ShipOrderCommand command) {
3637
if (!orderConfirmed) {
37-
throw new IllegalStateException("Cannot ship an order which has not been confirmed yet.");
38+
throw new UnconfirmedOrderException();
3839
}
3940

4041
apply(new OrderShippedEvent(orderId));
@@ -43,12 +44,12 @@ public void handle(ShipOrderCommand command) {
4344
@EventSourcingHandler
4445
public void on(OrderPlacedEvent event) {
4546
this.orderId = event.getOrderId();
46-
orderConfirmed = false;
47+
this.orderConfirmed = false;
4748
}
4849

4950
@EventSourcingHandler
5051
public void on(OrderConfirmedEvent event) {
51-
orderConfirmed = true;
52+
this.orderConfirmed = true;
5253
}
5354

5455
protected OrderAggregate() {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.axon.coreapi.exceptions;
2+
3+
public class UnconfirmedOrderException extends IllegalStateException {
4+
5+
public UnconfirmedOrderException() {
6+
super("Cannot ship an order which has not been confirmed yet.");
7+
}
8+
}

0 commit comments

Comments
 (0)