The Itinera bookKotlin 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, with the order's number now a String? in its initial data, since the website may leave it out. A null number adds no key, as a null property doesn't in chapter 20:

Kotlin, from the program ch36_orders_refused_before_they_start
/**
 * What the shop knows about an order when its journey starts. An order with no number or no card
 * leaves it out.
 */
data class NewOrder(
    val order: String?,
    val customer: String,
    val price: Long,
    val discount: Long? = null,
    val parcel: Parcel,
    val card: String? = null,
    val giftCard: Boolean = 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:

Kotlin, from the program ch36_orders_refused_before_they_start
override fun setGlobalMetadata(key: String, value: Any?) {
    if (key == "customer") service.fileUnder(value as? String ?: "")
    metadata[key] = value
}
Kotlin, the same in its asynchronous form, from the program ch36_orders_refused_before_they_start_async
override suspend fun setGlobalMetadata(key: String, value: Any?) {
    if (key == "customer") service.fileUnder(value as? String ?: "")
    metadata[key] = value
}

The faked logging service refuses an empty name with its exception, NoCustomer. In the asynchronous form, filing waits for the network, so fileUnder is a suspend function, which setGlobalMetadata, a suspend function of AsyncReporter, can call. 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:

Kotlin, from the program ch36_orders_refused_before_they_start
/** What main prints before an order: what the order lacks, its number or its customer's name. */
fun heading(order: NewOrder): String =
    if (order.order == null) "${order.customer}'s order, without a number"
    else "order ${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:

Kotlin, from the program ch36_orders_refused_before_they_start
val instance =
    try {
        Orders(channel = "email")
            .instance()
            .reportersGlobalMetadata("customer", order.customer)
            .initialData(order)
            .create()
    } catch (error: IllegalStateException) {
        println("no instance: ${error.message}")
        continue
    }
try {
    followUp(executor.run(instance))
} catch (refusal: Refusal) {
    println("the journey was refused: ${refusal.message}")
}
Kotlin, the same in its asynchronous form, from the program ch36_orders_refused_before_they_start_async
runBlocking {
    for (order in orders) {
        println("${heading(order)}:")
        val instance =
            try {
                Orders(channel = "email")
                    .instance()
                    .reportersGlobalMetadata("customer", order.customer)
                    .initialData(order)
                    .create()
            } catch (error: IllegalStateException) {
                println("no instance: ${error.message}")
                continue
            }
        try {
            followUp(AsyncLocalExecutor().run(instance))
        } catch (refusal: Refusal) {
            println("the journey was refused: ${refusal.message}")
        }
    }
}

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. In the asynchronous form, the orders run one after the other inside runBlocking, each with an AsyncLocalExecutor of its own, 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 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 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, and calls error(...) when there is none. Quin's order has no number, so error throws an IllegalStateException, and create() 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.
  2. An executor can refuse an instance. run throws 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 threw on customer, so the executor refused the journey: the refusal's refused is Refused.GlobalMetadataNotSet, its message ends with the message of the log's exception, and refused.error holds the NoCustomer itself.
  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. main learns why from what create() and run threw. 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.