The Itinera bookTypeScript edition

Part 4: Running it for real, Chapter 36

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. The website leaves order out of Quin's data, and sends order A-16 with an empty customer's name:

TypeScript, from the program ch36_orders_refused_before_they_start
// The order, if it has a number, the customer, the price, the discount when
// there is one, the parcel, the card, and whether the order is a gift card.
const orders = [
  {
    customer: "Quin",
    price: 20,
    parcel: { weight: 1, size: [20, 10, 10] },
    card: "quin-2027",
    giftCard: false,
  },
  {
    order: "A-16",
    customer: "",
    price: 25,
    parcel: { weight: 1, size: [20, 10, 10] },
    card: "rae-2028",
    giftCard: false,
  },
];

When the log receives the customer's name, it asks the logging service to file the journey's lines under it, and fails if the service refuses. Filing waits for the service's answer over the network, so receiveGlobalMetadata is now async, as report became in chapter 31:

TypeScript, from the program ch36_orders_refused_before_they_start
async receiveGlobalMetadata(key: string, value: PlainValue): Promise<void> {
  this.metadata.set(key, value);
  if (key === "customer") {
    await this.service.fileUnder(typeof value === "string" ? value : "");
  }
}

The rest of the log is chapter 33's. Its detail is the function chapter 12 shows, with the cases later chapters added, the last in chapter 33 and chapter 35. The faked logging service refuses an empty name with its error, NoCustomer.

For each order, the program first prints a heading, saying what the order lacks:

TypeScript, from the program ch36_orders_refused_before_they_start
/**
 * What the program prints before an order: what the order lacks, its number
 * or its customer's name.
 */
function heading(order: string | undefined, customer: string): string {
  if (order === undefined) return `${customer}'s order, without a number`;
  return `order ${order}, 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:

TypeScript, from the program ch36_orders_refused_before_they_start
let instance: WorkflowInstance<Orders>;
try {
  instance = shop.instance(order, {
    reportersGlobalMetadata: { customer: order.customer },
  });
} catch (error) {
  const message = error instanceof Error ? error.message : String(error);
  console.log(`no instance: ${message}`);
  continue;
}
let result: JourneyResult;
try {
  result = await executor.run(instance);
} catch (error) {
  if (
    !(error instanceof Refusal) ||
    error.refused.kind !== "global metadata not set"
  ) {
    throw error;
  }
  console.log(`the journey was refused: ${error.message}`);
  continue;
}
followUp(result);

shop and executor are made as in chapter 24, JourneyResult is the type of chapter 25, followUp is chapter 34's, and WorkflowInstance, from itinera/instance, is the type of an instance, as in chapter 26. It prints:

What ch36_orders_refused_before_they_start prints, recorded in ch36_orders_refused_before_they_start.txt
Quin's order, without a number:
no instance: the order's 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 name

How it works

  1. An instance isn't created without its journey ID. The ID generator of chapter 11 names each journey after its order's number, read from the initial data with expect. Quin's order has no number, so expect throws an Error with the message the generator gave it, the order's number, and instance(...) throws it in turn, as it was thrown. With no instance, there is nothing to run. The program checks that what it caught is an Error before it reads its message, as the outage policy does in chapter 34.
  2. An executor can refuse an instance. run rejects with a Refusal for 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 failed on customer, so the executor refused the journey: the refusal's refused.kind is global metadata not set, its message ends with the message of the log's error, and refused.error holds the error itself. The program catches only this refusal, and throws any other on, since those of chapter 24 are mistakes in the program, not an order's.
  3. 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 no JourneyResult either, since there was no journey to end.
  4. The order is still the shop's to deal with. The program learns why from what instance(...) threw and what run rejected with. 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. instance(...) throws, and run rejects with a Refusal, saying why, to the program that called them.