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:
/**
* 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:
override fun setGlobalMetadata(key: String, value: Any?) {
if (key == "customer") service.fileUnder(value as? String ?: "")
metadata[key] = value
}
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:
/** 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:
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}")
}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:
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 calls
error(...)when there is none. Quin's order has no number, soerrorthrows anIllegalStateException, andcreate()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'srefusedisRefused.GlobalMetadataNotSet, its message ends with the message of the log's exception, andrefused.errorholds 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. |