# Mistakes caught before a journey

## What you want to do

The shop's workflow is now put together from many parts: steps and their names, policies attached to them, an input adapter, services and their providers, a rescue step and roles. You want to see how it is put together without running an order, and to know that a mistake in putting it together is caught before any order runs.

## The code

### How the workflow is put together

The shop is the one [chapter 36](https://itinera-dev.org/book/java/36-orders-refused-before-they-start.md) runs: `Orders` is the workflow [chapter 23](https://itinera-dev.org/book/java/23-undoing-a-charge-when-the-shipping-fails.md) declares, with [chapter 35](https://itinera-dev.org/book/java/35-giving-the-rescue-step-a-service-of-its-own.md)'s provider for `cards`. `main` prints its **listing**, one entry for each step. `WorkflowDescriptor.of(Orders.class)`, from `org.itineradev.itinera.workflow`, reads the workflow's declaration from its annotations, and `listing()` lists it:

Java, from the program [`ch37_listing`](https://github.com/itinera-dev/book/blob/main/java/examples/ch37_listing.java)

```java
for (var listed : WorkflowDescriptor.of(Orders.class).listing()) {
  System.out.println(entry(listed));
}
```

`entry` writes each step's line from what the listing says about it, and `services`, with `service` for each one, writes the services of a step or of its rescue step. The entries are `ListedStep`s, and their services `ListedService`s, both records from the same package. An entry's `adapter()` and `rescue()` are `null` when the step has none:

Java, from the program [`ch37_listing`](https://github.com/itinera-dev/book/blob/main/java/examples/ch37_listing.java)

```java
/** A service in the listing: its key, and the provider that serves it. */
static String service(ListedService listed) {
  return listed.key() + " from " + listed.provider();
}

/** The services of a listing's entry, separated by commas. */
static String services(List<ListedService> listed) {
  return listed.stream().map(Main::service).collect(Collectors.joining(", "));
}

/**
 * One entry of the listing: a step's position and name, its policies, its adapter, its services
 * with the provider that serves each, and its rescue step with its services.
 */
static String entry(ListedStep listed) {
  var entry = new StringBuilder(listed.position() + ". " + listed.step());
  if (!listed.policies().isEmpty()) {
    var policies = listed.policies().stream().map(String::valueOf);
    entry.append(", with ").append(policies.collect(Collectors.joining(", ")));
  }
  if (listed.adapter() != null) entry.append("; adapted by ").append(listed.adapter());
  if (!listed.services().isEmpty()) {
    entry.append("; services: ").append(services(listed.services()));
  }
  var rescue = listed.rescue();
  if (rescue != null) {
    entry.append("; rescued by ").append(rescue.step());
    entry.append(", services: ").append(services(rescue.services()));
  }
  return entry.toString();
}

```

It prints:

What `ch37_listing` prints, recorded in [`ch37_listing.txt`](https://github.com/itinera-dev/book/blob/main/java/expected/ch37_listing.txt)

```text
1. lookup, with alreadyCompleted; services: orderBook from ledger
2. price
3. charge, with audit, outage; services: payments from bank; rescued by charge_rescue, services: cards from cards
4. ship, with failedShipping; adapted by parcels; services: carrier from carriers
```

### Mistakes that don't compile

Every program in this book is built with itinera's checker, `itinera-checker`, which checks each annotated workflow while javac compiles the program. An application adds it to javac's annotation processors: in Gradle's Kotlin DSL, `annotationProcessor(platform("org.itinera-dev:itinera-bom:VERSION"))` and `annotationProcessor("org.itinera-dev:itinera-checker")` under `dependencies`.

Each program below is a small shop with one mistake, and none of them compiles. The shipping listed twice:

Java, the program [`ch37_duplicate_step_name`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_duplicate_step_name.java). **Does not compile**

```java
// The shipping listed twice.

package ch37_duplicate_step_name;

import org.itineradev.itinera.executor.LocalExecutor;
import org.itineradev.itinera.executor.Refusal;
import org.itineradev.itinera.journey.Contributor;
import org.itineradev.itinera.step.ItineraStep;
import org.itineradev.itinera.step.Outcome;
import org.itineradev.itinera.step.Step;
import org.itineradev.itinera.workflow.ItineraWorkflow;
import org.itineradev.itinera.workflow.WithStep;
import org.itineradev.itinera.workflow.Workflow;

/** Charges an order. */
@Step
final class Charge implements ItineraStep {
  public Outcome run(Contributor contributor) {
    contributor.contribute("receipt", "R-1");
    return succeed();
  }
}

/** Ships an order. */
@Step
final class Ship implements ItineraStep {
  public Outcome run() {
    return succeed();
  }
}

/** The shop's workflow, with the shipping listed twice under one name. */
@Workflow(name = "orders")
@WithStep(step = Charge.class, name = "charge")
@WithStep(step = Ship.class, name = "ship")
@WithStep(step = Ship.class, name = "ship")
final class Orders implements ItineraWorkflow {}

final class Main {
  private Main() {}

  public static void main(String[] args) throws Refusal {
    var result = new LocalExecutor().run(new Orders().instance().create());
    System.out.println("the journey " + result.status());
  }
}
```

What the compiler says about `ch37_duplicate_step_name`, recorded in [`ch37_duplicate_step_name.txt`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_duplicate_step_name.txt)

```text
compile-errors/ch37_duplicate_step_name.java:37: error: duplicate step name: two steps are named ship
final class Orders implements ItineraWorkflow {}
      ^
```

Two policies of the charge that both define `on step success`:

Java, the program [`ch37_hook_defined_twice`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_hook_defined_twice.java). **Does not compile**

```java
// Two policies of one step that both define `on step success`.

package ch37_hook_defined_twice;

import org.itineradev.itinera.executor.LocalExecutor;
import org.itineradev.itinera.executor.Refusal;
import org.itineradev.itinera.journey.Contributor;
import org.itineradev.itinera.policy.FromStep;
import org.itineradev.itinera.policy.ItineraStepPolicy;
import org.itineradev.itinera.policy.OnStepSuccess;
import org.itineradev.itinera.policy.OnSuccess;
import org.itineradev.itinera.policy.StepPolicy;
import org.itineradev.itinera.step.ItineraStep;
import org.itineradev.itinera.step.Outcome;
import org.itineradev.itinera.step.Step;
import org.itineradev.itinera.workflow.ItineraWorkflow;
import org.itineradev.itinera.workflow.WithStep;
import org.itineradev.itinera.workflow.Workflow;

/** Charges an order. */
@Step
final class Charge implements ItineraStep {
  public Outcome run(Contributor contributor) {
    contributor.contribute("charged", 40L);
    return succeed();
  }
}

/** Audits every successful charge. */
@StepPolicy(name = "audit")
final class Audit implements ItineraStepPolicy {
  @OnStepSuccess
  public OnSuccess audited(@FromStep long charged) {
    System.out.println("audit: charged " + charged);
    return keepDefault();
  }
}

/** Writes a receipt for every successful charge. */
@StepPolicy(name = "receipts")
final class Receipts implements ItineraStepPolicy {
  @OnStepSuccess
  public OnSuccess written(@FromStep long charged) {
    System.out.println("receipt: R-" + charged);
    return keepDefault();
  }
}

/** The shop's workflow, with both policies attached to the charge. */
@Workflow(name = "orders")
@WithStep(
    step = Charge.class,
    name = "charge",
    policies = {Audit.class, Receipts.class})
final class Orders implements ItineraWorkflow {}

final class Main {
  private Main() {}

  public static void main(String[] args) throws Refusal {
    var result = new LocalExecutor().run(new Orders().instance().create());
    System.out.println("the journey " + result.status());
  }
}
```

What the compiler says about `ch37_hook_defined_twice`, recorded in [`ch37_hook_defined_twice.txt`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_hook_defined_twice.txt)

```text
compile-errors/ch37_hook_defined_twice.java:55: error: hook defined twice: step policies audit, receipts of step charge all define on step success
final class Orders implements ItineraWorkflow {}
      ^
```

A policy that tells the customer through the `Messages` [role](https://itinera-dev.org/book/java/19-letting-policies-act-on-the-outside-world.md), on a workflow that doesn't implement it:

Java, the program [`ch37_role_not_provided`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_role_not_provided.java). **Does not compile**

```java
// A policy that needs a role the workflow does not provide.

package ch37_role_not_provided;

import org.itineradev.itinera.executor.LocalExecutor;
import org.itineradev.itinera.executor.Refusal;
import org.itineradev.itinera.policy.FromWorkflow;
import org.itineradev.itinera.policy.ItineraWorkflowPolicy;
import org.itineradev.itinera.policy.OnWorkflowSuccess;
import org.itineradev.itinera.policy.Role;
import org.itineradev.itinera.policy.WorkflowPolicy;
import org.itineradev.itinera.step.ItineraStep;
import org.itineradev.itinera.step.Outcome;
import org.itineradev.itinera.step.Step;
import org.itineradev.itinera.workflow.ItineraWorkflow;
import org.itineradev.itinera.workflow.WithStep;
import org.itineradev.itinera.workflow.Workflow;

/** Charges an order. */
@Step
final class Charge implements ItineraStep {
  public Outcome run() {
    return succeed();
  }
}

/** The shop's way of telling a customer something. */
@Role
interface Messages {
  void tell(String customer, String message);
}

/** Tells the customer once the order is complete. */
@WorkflowPolicy(name = "notify")
final class Notify implements ItineraWorkflowPolicy {
  @OnWorkflowSuccess
  public void completed(@FromWorkflow String customer, @Role Messages messages) {
    messages.tell(customer, "your order is complete");
  }
}

/** The shop's workflow. It can tell a customer something, but does not implement the role. */
@Workflow(
    name = "orders",
    policies = {Notify.class})
@WithStep(step = Charge.class, name = "charge")
final class Orders implements ItineraWorkflow {
  public void tell(String customer, String message) {
    System.out.println("to " + customer + ": " + message);
  }
}

/** What the shop knows about an order when its journey starts. */
record NewOrder(String customer) {}

final class Main {
  private Main() {}

  public static void main(String[] args) throws Refusal {
    var instance = new Orders().instance().initialData(new NewOrder("Ann")).create();
    System.out.println("the journey " + new LocalExecutor().run(instance).status());
  }
}
```

What the compiler says about `ch37_role_not_provided`, recorded in [`ch37_role_not_provided.txt`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_role_not_provided.txt)

```text
compile-errors/ch37_role_not_provided.java:47: error: role not provided: policy notify requests role ch37_role_not_provided.Messages, which the workflow does not provide
final class Orders implements ItineraWorkflow {
      ^
```

An `on step failure` hook that tries to finish the journey as succeeded, which only `on step success` may do:

Java, the program [`ch37_invalid_lifecycle`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_invalid_lifecycle.java). **Does not compile**

```java
// An `on step failure` hook that tries to finish the journey as succeeded.

package ch37_invalid_lifecycle;

import org.itineradev.itinera.executor.LocalExecutor;
import org.itineradev.itinera.executor.Refusal;
import org.itineradev.itinera.policy.ItineraStepPolicy;
import org.itineradev.itinera.policy.OnFailure;
import org.itineradev.itinera.policy.OnStepFailure;
import org.itineradev.itinera.policy.StepPolicy;
import org.itineradev.itinera.step.ItineraStep;
import org.itineradev.itinera.step.Outcome;
import org.itineradev.itinera.step.Step;
import org.itineradev.itinera.workflow.ItineraWorkflow;
import org.itineradev.itinera.workflow.WithStep;
import org.itineradev.itinera.workflow.Workflow;

/** Charges an order, and fails: the card was declined. */
@Step
final class Charge implements ItineraStep {
  public Outcome run() {
    return fail("card-declined");
  }
}

/** Tries to finish the journey as succeeded when the charge fails. */
@StepPolicy(name = "forgiving")
final class Forgiving implements ItineraStepPolicy {
  @OnStepFailure
  public OnFailure declined() {
    return finishWorkflow();
  }
}

/** The shop's workflow. */
@Workflow(name = "orders")
@WithStep(
    step = Charge.class,
    name = "charge",
    policies = {Forgiving.class})
final class Orders implements ItineraWorkflow {}

final class Main {
  private Main() {}

  public static void main(String[] args) throws Refusal {
    var result = new LocalExecutor().run(new Orders().instance().create());
    System.out.println("the journey " + result.status());
  }
}
```

What the compiler says about `ch37_invalid_lifecycle`, recorded in [`ch37_invalid_lifecycle.txt`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_invalid_lifecycle.txt)

```text
compile-errors/ch37_invalid_lifecycle.java:31: error: incompatible types: FinishWorkflow cannot be converted to OnFailure
    return finishWorkflow();
                         ^
```

The same hook, declared to return an `OnSuccess` so that `finishWorkflow()` fits:

Java, the program [`ch37_hook_of_the_wrong_kind`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_hook_of_the_wrong_kind.java). **Does not compile**

```java
// An `on step failure` hook declared to return what only `on step success` returns.

package ch37_hook_of_the_wrong_kind;

import org.itineradev.itinera.executor.LocalExecutor;
import org.itineradev.itinera.executor.Refusal;
import org.itineradev.itinera.policy.ItineraStepPolicy;
import org.itineradev.itinera.policy.OnStepFailure;
import org.itineradev.itinera.policy.OnSuccess;
import org.itineradev.itinera.policy.StepPolicy;
import org.itineradev.itinera.step.ItineraStep;
import org.itineradev.itinera.step.Outcome;
import org.itineradev.itinera.step.Step;
import org.itineradev.itinera.workflow.ItineraWorkflow;
import org.itineradev.itinera.workflow.WithStep;
import org.itineradev.itinera.workflow.Workflow;

/** Charges an order, and fails: the card was declined. */
@Step
final class Charge implements ItineraStep {
  public Outcome run() {
    return fail("card-declined");
  }
}

/** Tries to finish the journey as succeeded when the charge fails. */
@StepPolicy(name = "forgiving")
final class Forgiving implements ItineraStepPolicy {
  @OnStepFailure
  public OnSuccess declined() {
    return finishWorkflow();
  }
}

/** The shop's workflow. */
@Workflow(name = "orders")
@WithStep(
    step = Charge.class,
    name = "charge",
    policies = {Forgiving.class})
final class Orders implements ItineraWorkflow {}

final class Main {
  private Main() {}

  public static void main(String[] args) throws Refusal {
    var result = new LocalExecutor().run(new Orders().instance().create());
    System.out.println("the journey " + result.status());
  }
}
```

What the compiler says about `ch37_hook_of_the_wrong_kind`, recorded in [`ch37_hook_of_the_wrong_kind.txt`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_hook_of_the_wrong_kind.txt)

```text
compile-errors/ch37_hook_of_the_wrong_kind.java:30: error: ch37_hook_of_the_wrong_kind.Forgiving.declined returns OnSuccess, not OnFailure
  public OnSuccess declined() {
                   ^
```

A rescue step that tries to skip the charge rather than repair it:

Java, the program [`ch37_rescue_step_skips`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_rescue_step_skips.java). **Does not compile**

```java
// A rescue step that tries to skip the charge rather than repair it.

package ch37_rescue_step_skips;

import org.itineradev.itinera.executor.LocalExecutor;
import org.itineradev.itinera.executor.Refusal;
import org.itineradev.itinera.step.ItineraRescueStep;
import org.itineradev.itinera.step.ItineraStep;
import org.itineradev.itinera.step.Outcome;
import org.itineradev.itinera.step.RescueOutcome;
import org.itineradev.itinera.step.RescueStep;
import org.itineradev.itinera.step.Step;
import org.itineradev.itinera.workflow.ItineraWorkflow;
import org.itineradev.itinera.workflow.WithStep;
import org.itineradev.itinera.workflow.Workflow;

/** Charges an order, and fails: the card has expired. */
@Step
final class Charge implements ItineraStep {
  public Outcome run() {
    return fail("card-expired");
  }
}

/** Gives up on the card, and tries to skip the charge. */
@RescueStep
final class LetItGo implements ItineraRescueStep {
  public RescueOutcome run() {
    return skipBecause("card-expired");
  }
}

/** The shop's workflow. */
@Workflow(name = "orders")
@WithStep(step = Charge.class, name = "charge", rescue = LetItGo.class)
final class Orders implements ItineraWorkflow {}

final class Main {
  private Main() {}

  public static void main(String[] args) throws Refusal {
    var result = new LocalExecutor().run(new Orders().instance().create());
    System.out.println("the journey " + result.status());
  }
}
```

What the compiler says about `ch37_rescue_step_skips`, recorded in [`ch37_rescue_step_skips.txt`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_rescue_step_skips.txt)

```text
compile-errors/ch37_rescue_step_skips.java:29: error: cannot find symbol
    return skipBecause("card-expired");
           ^
  symbol:   method skipBecause(String)
  location: class LetItGo
```

An input adapter that asks for its read access to the data bag, a `DataBagAccess` from `org.itineradev.itinera.journey`, and tries to write the weight into it:

Java, the program [`ch37_adapter_writes_the_data_bag`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_adapter_writes_the_data_bag.java). **Does not compile**

```java
// An input adapter that asks for the data bag, and tries to write the weight into it.

package ch37_adapter_writes_the_data_bag;

import org.itineradev.itinera.executor.LocalExecutor;
import org.itineradev.itinera.executor.Refusal;
import org.itineradev.itinera.journey.DataBagAccess;
import org.itineradev.itinera.policy.FromWorkflow;
import org.itineradev.itinera.step.Input;
import org.itineradev.itinera.step.ItineraStep;
import org.itineradev.itinera.step.Outcome;
import org.itineradev.itinera.step.Step;
import org.itineradev.itinera.workflow.InputAdapter;
import org.itineradev.itinera.workflow.ItineraWorkflow;
import org.itineradev.itinera.workflow.Key;
import org.itineradev.itinera.workflow.Supplied;
import org.itineradev.itinera.workflow.WithStep;
import org.itineradev.itinera.workflow.Workflow;

/** Ships an order's parcel. */
@Step
final class Ship implements ItineraStep {
  private final long weight;

  Ship(@Input long weight) {
    this.weight = weight;
  }

  public Outcome run() {
    System.out.println("shipping " + weight + " kg");
    return succeed();
  }
}

/** How the warehouse describes an order's parcel: its weight in kg. */
record Parcel(long weight) {}

/** The shop's workflow. */
@Workflow(name = "orders")
@WithStep(step = Ship.class, name = "ship")
final class Orders implements ItineraWorkflow {
  /** Gives the shipping the parcel's weight, and tries to keep it in the data bag too. */
  @InputAdapter(steps = {"ship"})
  public Supplied parcels(@Key String key, @FromWorkflow Parcel parcel, DataBagAccess data) {
    data.write("weight", parcel.weight());
    return key.equals("weight") ? supply(parcel.weight()) : fromDataBag();
  }
}

/** What the shop knows about an order when its journey starts. */
record NewOrder(Parcel parcel) {}

final class Main {
  private Main() {}

  public static void main(String[] args) throws Refusal {
    var instance = new Orders().instance().initialData(new NewOrder(new Parcel(2))).create();
    System.out.println("the journey " + new LocalExecutor().run(instance).status());
  }
}
```

What the compiler says about `ch37_adapter_writes_the_data_bag`, recorded in [`ch37_adapter_writes_the_data_bag.txt`](https://github.com/itinera-dev/book/blob/main/java/compile-errors/ch37_adapter_writes_the_data_bag.txt)

```text
compile-errors/ch37_adapter_writes_the_data_bag.java:45: error: cannot find symbol
    data.write("weight", parcel.weight());
        ^
  symbol:   method write(String,long)
  location: variable data of type DataBagAccess
```

## How it works

1. **The listing says how the workflow is put together.** It lists each step in order, with its position, the policies attached to it, its input adapter, each service it asks for with the provider that serves it, and its rescue step with that step's own services. It is read from the declaration: nothing runs, and no provider is called. The listing alone shows, for example, that the card registry serves `charge_rescue` and nothing else.
2. **A mistake in putting a workflow together is a violation.** Checking how a workflow is put together, before any journey, is called **admission**, and the specification names each mistake it catches: `duplicate step name`, `hook defined twice`, `service not provided`, `rescued step retried` and a few more, all listed in [3.6, Configuration errors](https://github.com/itinera-dev/spec/blob/main/spec/03-workflow-definitions.md#36-configuration-errors). Every violation found is reported at once, not only the first.
3. **With the checker, a violation is a compile error.** The checker reads the annotations as javac runs it, and reports each violation as an error of javac's, on the class or method it concerns, so a workflow put together wrongly never runs.
4. **Some mistakes can't be written at all.** Each hook's return type lists only the [lifecycles](https://itinera-dev.org/book/java/17-giving-up-a-step-early.md) its kind may return, and `finishWorkflow()` is an `OnSuccess`, as [chapter 22](https://itinera-dev.org/book/java/22-finishing-a-journey-early.md) said, never an `OnFailure`. A hook declared with another kind's type still compiles as Java, so the checker reports it instead. A [`RescueOutcome`](https://itinera-dev.org/book/java/14-repairing-then-trying-once-more.md) is a success or a failure, never a skip, so a rescue step has no `skipBecause`. A `DataBagAccess` has methods that read and none that write, since an adapter reads and never writes, as [chapter 21](https://itinera-dev.org/book/java/21-deciding-where-a-steps-data-comes-from.md) said.

Each of these is caught because the annotations declare the workflow for you, and the checker reads that declaration as the program is built. But you haven't seen what the annotations stand for.

## Outcomes

We have shown how the listing says how a workflow is put together, and how a mistake in putting it together is caught before any order runs.

| The usual way                                                                                                        | With Itinera                                                                              |
| -------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| Which hooks run after a step, or which client serves a repair, is often learned only by reading every module.        | The listing names each step's policies, its adapter, and the services of its rescue step. |
| A wiring mistake, such as two handlers for one event, often shows only when a request takes that path in production. | The program doesn't build, and the message names the mistake.                             |
