# Find out why a workpiece failed, and retry it

When your code throws inside `processWorkpiece`, the workpiece goes to the station's `failed` bin with its error. Nothing else in it changes:

```text
IN1/failed/inv-1001/
├── pointer.json     unchanged
├── invoice.pdf      unchanged: what IN1 was given
├── log.jsonl        includes the station_failed event
├── error.json       every field of the error, for tools
└── error.txt        a short version for people
```

## See what failed

```bash
al-control workpieces errors              # failures on every factory, grouped by error
al-control workpieces show inv-1001       # one workpiece: where it is, its journey, its error
al-control workpieces cat inv-1001 error.txt
```

`error.txt` starts with the time and `Name: message`, then the error's other fields, then the stack. A long `body` is cut at 500 characters; the full text is in `error.json`.

`error.json` holds `failed_at`, `name`, `message`, `stack`, and every field your code put on the error. The station is the folder it sits in, and the run is the `wr` of the `station_failed` line in `log.jsonl`.

## Make failures easy to group

`al-control workpieces errors` counts failures with the same cause together. Start each error message with a short snake_case reason and a colon, and the reason becomes the group:

```js
throw Object.assign(new Error(`total_mismatch: invoice total ${total} does not match its lines (${lines_total})`), {
  invoice_total: total,
  lines_total,
});
```

Without a reason, failures are grouped by the message's first line, with ids, paths and long numbers masked.

Attach the facts someone will need to fix it, as fields on the error: everything you attach lands in `error.json`. When an outside API rejects a request, attach what it sent back (`status`, `body`), so the answer is in the workpiece and not only in a log that scrolls away.

## Retry

Fix the cause, then send the workpiece back to `input`:

```bash
al-control workpieces requeue inv-1001                 # one workpiece; a dry run until you add --yes
al-control workpieces requeue --bin IN1/failed --yes   # everything that failed at IN1
```

To redo work that finished wrong, see [Redo work through the line](/docs/guides/redo-work).

`requeue` also removes the old `error.json` and `error.txt`, so the next failure isn't mixed with this one. The workpiece runs again the next time the station runs: by hand, on its schedule, or as soon as a watch sees it.

## A failed workpiece is not a failed run

A run that processes ten workpieces and sends two to `failed` still completes. Its work record is `completed`, and it reports each workpiece's outcome. The two failures wait in the bin until someone fixes the cause and requeues them.

A station that throws is different: the whole run failed. The Orchestrator runs the station again from the start, up to 3 retries, and then marks the run `failed`. See the [work record object](https://orchestrator.assemblyline.sh/docs/objects/work-record) for every run status.

## Why the folder is safe to retry

The workpiece in `failed` is exactly what the station was given, because the station only ever worked on a copy in `doing/`. Retrying starts from the same input. See [The doing bin](/docs/method/doing-bin) and [Nothing leaves silently](/docs/method/nothing-leaves-silently).
