Orders refused before they start
What you want to do
The website now sends orders before they are finished: Quin's arrives without an order number, so nothing can name its journey, and order A-16 without the customer's name, so the logging service has nothing to file its lines under. You want such an order stopped before anything is done for it, and to know why.
The code
The shop is chapter 35's, with the order's number now an Optional<String> in its initial data, since the website may leave it out. An empty number adds no key, as an empty discount doesn't in chapter 20:
/**
* What the shop knows about an order when its journey starts. An order with no number or no card
* leaves it out.
*/
record NewOrder(
Optional<String> order,
String customer,
long price,
Optional<Long> discount,
Parcel parcel,
Optional<String> card,
boolean giftCard) {
/** An order with a number, without a discount, that isn't a gift card. */
NewOrder(String order, String customer, long price, Parcel parcel, String card) {
this(Optional.of(order), customer, price, Optional.empty(), parcel, Optional.of(card), false);
}
/** An order without a number or a discount, that isn't a gift card. */
NewOrder(String customer, long price, Parcel parcel, String card) {
this(Optional.empty(), customer, price, Optional.empty(), parcel, Optional.of(card), false);
}
}
Parcel is chapter 21's. When the log receives the customer's name, it asks the logging service to file the journey's lines under it, and lets the service's exception out if it refuses:
@Override
public void setGlobalMetadata(String key, Object value) {
if (key.equals("customer")) service.fileUnder(value instanceof String name ? name : "");
metadata.put(key, value);
}
The faked logging service refuses an empty name with its exception, NoCustomer. The rest of the log is chapter 33's, with chapter 35's detail.
For each order, main first prints a heading, saying what the order lacks:
/** What an order lacks, as a heading for what its journey prints. */
static String heading(NewOrder order) {
if (order.order().isEmpty()) return order.customer() + "'s order, without a number";
return "order " + order.order().get() + ", without a customer's name";
}
Then it creates the order's instance, runs it, and says why the order went no further at either point. The instance is a WorkflowInstance<Sync>, from org.itineradev.itinera.instance and org.itineradev.itinera.workflow, declared before the try so that the run after it can use it:
WorkflowInstance<Sync> instance;
try {
instance =
new Orders("email")
.instance()
.reportersGlobalMetadata("customer", order.customer())
.initialData(order)
.create();
} catch (IllegalStateException error) {
System.out.println("no instance: " + error.getMessage());
continue;
}
try {
followUp(executor.run(instance));
} catch (Refusal refusal) {
System.out.println("the journey was refused: " + refusal.getMessage());
}Orders is the workflow chapter 23 declares, with chapter 35's provider for cards, executor is made before the loop, as in chapter 24, and followUp is chapter 34's. It prints:
Quin's order, without a number:
no instance: The ID generator threw while the instance was created: an order without a number
order A-16, without a customer's name:
the journey was refused: run refused an instance: the global metadata could not be set: the logging service files no line without a customer's nameHow it works
- An instance isn't created without its journey ID. The ID generator of chapter 11 names each journey after its order's number, and throws an
IllegalStateExceptionwhen there is none. Quin's order has no number, socreate()throws one in turn, whose message says that the ID generator threw, followed by the generator's own message. With no instance, there is nothing to run. - An executor can refuse an instance.
runthrows aRefusalfor a journey that can't start, as chapter 24 showed. Before the first event, the executor gives every reporter the journey ID, then the instance's entries of global metadata, as chapter 33 showed. The log threw oncustomer, so the executor refused the journey: the refusal'sgetRefused()is aRefused.GlobalMetadataNotSet, its message ends with the message of the log's exception, andgetRefused().getError()holds theNoCustomeritself. - A refused journey never started. There is no
journey_started, no step ran, and the bank was never called: unlike Pat's journey in chapter 31, aborted after the money was taken, A-16's has nothing to undo. There is noJourneyResulteither, since there was no journey to end. - The order is still the shop's to deal with.
mainlearns why from whatcreate()andrunthrew. Once the website sends what was missing, a new instance runs the order, as chapter 24 said.
These orders were stopped by what they lacked. A mistake in how the shop's workflow is put together, such as two steps under one name, would stop every order instead, however complete.
Outcomes
We have shown how an order that can't be named or logged is stopped before its journey starts, and how the program learns why.
| The usual way | With Itinera |
|---|---|
| An incomplete request often gets halfway through, past the payment, before something notices the missing field. | An order that can't be named, or whose journey a reporter can't take, is stopped before its first step. |
| Why a request never ran is often only a log line somewhere. | create() throws, and run throws a Refusal, saying why, to the program that called them. |