Errors and retries
A handler can end in three ways besides returning a value: fail for good, be retried, or pause until someone fixes the cause. Which one you get depends on what you throw.
| You throw | Outcome | Journal |
|---|---|---|
TerminalError(message, code) |
Invocation ends failed with code and message. Not retried |
Kept; the failure is the result |
| Any other error | The turn fails and is retried with backoff | Kept; completed steps replay, only unfinished work runs again |
PauseError(message, code) |
Invocation is paused, waiting for an operator |
Kept; resume continues from the same point |
All three are exported from @cairnflow/sdk.
TerminalError
Section titled “TerminalError”Use it for failures a retry cannot fix: invalid input, a refused payment, a missing record.
import { TerminalError } from "@cairnflow/sdk";
if (typeof input?.amount !== "number") { throw new TerminalError("amount must be a number", 400);}codedefaults to500. Use HTTP-style codes; the code and message reach the caller. A code that is not an integer from 0 to 2³²−1 becomes500.- A caller using
/invokegets500with{"status": "failed", "code": 400, "message": ...}; the typed client throwsInvocationFailed, which is itself aTerminalError. - A handler that called the failed one with
ctx.callgets aTerminalErrorwith the same code and message. - You can catch a
TerminalErrorand carry on — that is how you write compensation (sagas). - Thrown inside a
ctx.runclosure, it becomes the step’s recorded result: the closure does not run again, andctx.runthrows the same error on every replay.
Retried errors
Section titled “Retried errors”Any error that is not a TerminalError or PauseError fails the current turn. Cairnflow
schedules another attempt with backoff. On the next attempt, steps already in the journal return
their recorded results; only the work that did not finish runs again.
Inside a ctx.run closure the rule is strict: once a closure throws a normal error, the turn is
dead even if your code catches the error. The next ctx command throws TurnFailed, and the turn
ends as a failure to retry. Continuing past the error would let later steps depend on a result the
retry is about to replace. If you want a step’s failure recorded so it never runs again, throw
RunFailure or TerminalError from the closure — see Durable steps.
Retry policy
Section titled “Retry policy”Without a policy, retries are unlimited with backoff starting at 100 ms, doubling, capped at 60 seconds. Set a policy per service or per handler:
service("Payments", { charge }, { options: { retryPolicy: { initialInterval: 200, maxInterval: 30_000, exponentiationFactor: 2, maxAttempts: 5, onMaxAttempts: "PAUSE", }, },});| Field | Default | Meaning |
|---|---|---|
initialInterval |
100 |
First backoff, ms |
maxInterval |
60000 |
Largest backoff, ms |
exponentiationFactor |
2 |
Multiplier between attempts; must be at least 1 |
maxAttempts |
unlimited | Total attempts including the first; at least 1 |
onMaxAttempts |
"PAUSE" |
"PAUSE" keeps the invocation for an operator; "KILL" ends it as failed |
When attempts run out, the invocation ends paused (or failed with KILL) with code 575, and
the message includes the last attempt’s error. POST /invocations/{id}/resume gives a paused
invocation another round of attempts.
The policy is captured when the deployment is registered; changing it means registering a new deployment.
Giving up on purpose
Section titled “Giving up on purpose”ctx.attempt counts the attempts since the handler last made progress: 0 on a first run, and it
resets every time the journal grows. Use it to stop retrying a dependency that is down for good:
import { RunFailure } from "@cairnflow/sdk";
const result = await ctx.run("call vendor", async () => { if (ctx.attempt >= 3) { throw new RunFailure("vendor did not answer after 3 attempts", 503); } return callVendor();});Paused invocations
Section titled “Paused invocations”An invocation is paused when it cannot continue without a person:
| Code | Cause | What to do |
|---|---|---|
570 |
Journal mismatch: the code no longer matches the recorded steps | Fix the code or move the invocation — see Deploying new versions |
575 |
Retry attempts exhausted | Fix the cause, then resume |
| your code | You threw PauseError |
Fix the cause, then resume |
Throw PauseError for problems that will not fix themselves but are worth keeping the work for —
stock that must be restocked, a quota that must be raised:
import { PauseError } from "@cairnflow/sdk";
const inStock = await ctx.run("check stock", () => stock.count(sku));if (inStock < input.quantity) { throw new PauseError(`missing ${input.quantity - inStock} of ${sku}`);}POST /invocations/{id}/resume continues the invocation from where it stopped. The answer carries
the status after continuing: it can complete, suspend, or pause again if nothing changed.
A caller waiting with /invoke gets 409 with {"status": "paused", "code": ..., "message": ...};
the typed client throws InvocationPaused.
Don’t swallow control flow
Section titled “Don’t swallow control flow”When your code awaits something that is not ready yet (a timer, a call, a signal, a step result not yet durable), the promise simply stays pending until the turn ends; nothing is thrown. The SDK throws its own errors only in a few cases, and none of them is an error of your code:
| Error | When |
|---|---|
TurnFailed |
A ctx command after a step’s closure threw a normal error; the turn will be retried |
JournalMismatch |
A command does not match the journal; the invocation pauses with 570 |
Suspend |
A ctx command after the turn has already suspended, for example from code still running after it awaited something outside the SDK |
Catching them changes nothing: the turn still suspends, fails or pauses. If you need a broad
catch, rethrow them:
import { JournalMismatch, Suspend, TurnFailed } from "@cairnflow/sdk";
try { await doWork(ctx);} catch (e) { if (e instanceof Suspend || e instanceof TurnFailed || e instanceof JournalMismatch) throw e; // handle your own errors here}For the full list of failure codes, see Errors reference.