Throw errors from a station
To fail, throw an Error. Where you throw it decides what fails.
| Thrown from | What fails | What happens next |
|---|---|---|
A processWorkpiece body | That workpiece | It goes to failed/ with its error, and the station goes on with the rest. See Find out why a workpiece failed. |
execute, outside any body | The whole run | The run is retried, then fails |
Before execute: the config doesn't fit configSchema | The whole run | The same |
| Nowhere: the factory session has no handler for the station's code | The whole run | The run fails at once with Unknown station, without a retry |
Fail the whole run only for what stops every workpiece: missing credentials, a service that is down, a tool that isn't installed. Anything about one workpiece (a file that won't parse, a total that doesn't add up) belongs inside its body, so the other workpieces still finish.
Check what every workpiece needs once, before the loop. A client that is missing its settings says which:
// src/utils/ledger.js
export function ledgerClient() {
const missing = ['LEDGER_URL', 'LEDGER_API_KEY'].filter((name) => !process.env[name]);
if (missing.length) throw new Error(`ledger_env: missing ${missing.join(', ')} (see .env.example)`);
return createClient({ url: process.env.LEDGER_URL, api_key: process.env.LEDGER_API_KEY });
}
Without that check, every workpiece fails with the same error, and each one has to be requeued after the fix.
How a failed run is retried
- The station runs again, from the start, with the same config. The run is queued again at once and keeps its place, so it runs on the factory session's next poll, seconds later. A run is retried up to 3 times.
- Every thrown error is retried, including one that will fail the same way each time, such as a config that doesn't fit the schema. lib-factory reports every throw as worth retrying; a property such as
err.retryableon the error is ignored. - After the last retry, the run is
failed. The retry count and every run status are in the work record object.
A station must be safe to rerun
A retry runs execute again from the start, and so does a redo or a second run started by hand. Nothing remembers how far the last attempt got, so write every station to be run twice:
- Workpieces already finished stay finished.
processWorkpiecehas moved them out ofinput/, so the rerun picks up only what is left. - A line head skips what it has already created. It checks its registry before it creates a workpiece.
- An outside write made before the throw is made again, unless the station checks first. Before writing to another system, look for the record you are about to create, and skip it if it is already there.
Start the message with a reason
Begin each message with a short snake_case reason and a colon: ledger_env:, pdf_unreadable:, total_mismatch:. Use the same reason for the same cause everywhere in the factory. al-control groups failed workpieces by it, and a person scanning failed runs can tell causes apart at a glance. Put the facts someone needs to fix it on the error as fields. Both are covered in Make failures easy to group.
A model can answer without an error and still fail
A call to a model can return without an error and give you nothing you can use: prose instead of JSON, JSON in the wrong shape, an answer cut off at the length limit, a refusal. If the station trusts it, it writes a clean success over a bad answer, and nobody sees the failure. Check the answer before you use it, inside the workpiece's body, and throw if it fails:
| Check | If it fails, throw |
|---|---|
| The call says it succeeded: no error flag, not stopped early by a length or budget limit | model_failed: stopped at the length limit |
| There is a structured answer, or JSON you can parse | model_failed: no JSON in the answer |
| It fits your zod schema | invalid_extraction: total: expected number, received string |
| Its values are possible: a real date, an amount above zero, page ranges inside the document | invalid_extraction: date 2026-02-31 is not a real date |
const answer = await askModel({ system, text, schema: extraction_schema }); // your wrapper in src/utils/
if (answer.error) throw new Error(`model_failed: ${answer.error}`);
const parsed = extraction_schema.safeParse(answer.output);
if (!parsed.success) {
throw new Error(`invalid_extraction: ${parsed.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join('; ')}`);
}
const problems = validateExtraction(parsed.data); // your own checks, in code
if (problems.length) throw new Error(`invalid_extraction: ${problems.join('; ')}`);
Where the model supports it, ask for structured output with a JSON schema, so the first two checks rarely fail. Don't rescue a bad answer by parsing it loosely. Why a model's answer is checked by code, and which checks to write, is in Code first, the model for judgment.
The workpiece then lands in failed/ with the reason, and al-control groups it with the others that failed the same way.
Related
- Find out why a workpiece failed, and retry it: what a failed workpiece holds, and requeueing it
- Call a command-line tool from a station: the same split for a missing tool and a bad file
- Nothing leaves silently: why every failure has to leave its reason behind