Aviary
Reliability

Routing a run's failure — @OnError

Route a failed run to a handler workflow by error code, and have the run settle with the handler's outcome — orchestrated by the engine, so it works the same for inline, in-app-worker and remote (Python) bodies, and a fan-out over the workflow keeps going when one item fails.

Some failures are not accidents. A line that fails validation, a record a downstream system rejects, an order for a product that no longer exists: retrying won't change the answer, and failing the run isn't the right end state either, because something should happen next (record the line, tell someone, move on).

@OnError routes such a failure to a handler workflow. When a run ends failed with an error whose code matches, the engine starts the handler and the failed run waits for it, then settles with the handler's outcome: its return value becomes the run's output, so the run completes.

import { FatalError, type OnErrorInput, type WorkflowCtx } from '@dudousxd/nestjs-durable-core';
import { OnError, Workflow } from '@dudousxd/nestjs-durable';

export class InvalidLine extends FatalError {
  static readonly code = 'invalid_line';
  constructor(message: string) {
    super(message, InvalidLine.code);
  }
}

@Workflow({ name: 'line' })
export class LineWorkflow {
  constructor(private readonly steps: LineSteps) {}

  async run(ctx: WorkflowCtx, line: Line): Promise<LineResult> {
    await ctx.step(this.steps.validate, line); // throws InvalidLine for a bad line
    await ctx.step(this.steps.ingest, line);
    return { ok: 1, failed: 0 };
  }

  @OnError(InvalidLine)
  async invalid(ctx: WorkflowCtx, failure: OnErrorInput<Line>): Promise<LineResult> {
    await ctx.step(this.steps.recordRejectedLine, { line: failure.input, error: failure.error.message });
    return { ok: 0, failed: 1 };
  }
}

A file that fans out over its lines with ctx.all(LineWorkflow, lines) now completes with one LineResult per line, instead of failing on the first invalid one.

How the engine runs it

The engine does the routing, at the point where it settles a run as failed. It never looks inside the body, so the same thing happens whatever ran it: inline, an in-app worker, or a remote worker in another language.

  1. The run's body fails with an error carrying code: 'invalid_line'. Saga compensations of the body run, as for any failure.
  2. Instead of marking the run failed, the engine starts the handler as its own run, with the id <runId>.onError.<attempt> and an OnErrorInput ({ runId, workflow, input, error }), tags the run onError:<code>, emits run.routed, and suspends the failed run on the handler the way a parent waits for a ctx.child.
  3. From then on the run only follows its handler: the body is never run again, not by a recovery wake nor by a manual resume. When the handler finishes, the run settles with its outcome: completed with the handler's output, or failed with the handler's error.

The handler is an ordinary durable run: it shows up in the dashboard linked to the run it handles, its steps are checkpointed and retried, and the wait survives a crash like any child wait. A @OnError method is registered as the workflow <workflow>.onError.<method> and shares the class's injected dependencies. The onError:<code> tag makes the handled runs a filter away in the console.

The failure's details

error carries the failure's message, code and details. details is whatever JSON-serializable value the failure was raised with, so a handler can record exactly what went wrong:

// in the step
const parsed = LineSchema.safeParse(line);
if (!parsed.success) throw new InvalidLine(`line ${line.n} is invalid`, parsed.error.issues);

// FatalError(message, code, details)
class InvalidLine extends FatalError {
  static readonly code = 'invalid_line';
  constructor(message: string, issues: unknown) {
    super(message, InvalidLine.code, issues);
  }
}

// in the handler: failure.error.details is the issues array

In Python: raise FatalError("line 3 is invalid", "invalid_line", details=issues).

Retrying a run

Retrying a failed run (from the console, or engine.requeue) runs its body again. A handoff the run made before is retired: it stays in the timeline, renamed onError:retried:<handler id>, and is never followed again, and the run's onError:<code> tag is removed. If the body fails again, the engine hands the new failure to a new handler run (<runId>.onError.1, then .2, …), tagged again.

Matching

Routing matches on the error's code, not its class: a dispatched step's error reaches the engine as a plain { message, code }, and a remote body's failure was never a JavaScript error at all.

  • @OnError(InvalidLine) reads InvalidLine.code. A class without a static code is rejected when the decorator runs.
  • @OnError('invalid_line') names the code directly.
  • @OnError() with no argument is the catch-all: any failure no other code matches.
  • Stack the decorator to give one method several codes.

The code comes from FatalError's second argument, from a code on whatever a step handler threw, or from the code a remote worker reported for the failed step. A failure from a step reaches the route once the step's retries are spent; a FatalError skips them.

A schema failure is a SchemaValidationError, with the code invalid_input (an input schema, on the workflow or a step, or a stored input that no longer parses) or invalid_output (an output schema). @OnError(SchemaValidationError) reads its static code, schema_validation, and catches both when no route names them, as well as a rejected searchAttributes write:

@OnError('invalid_output') // just this one
async reportBadResponse(ctx: WorkflowCtx, failure: OnErrorInput<Input>): Promise<Output> { /* … */ }

@OnError(SchemaValidationError) // every other schema failure
async quarantine(ctx: WorkflowCtx, failure: OnErrorInput<Input>): Promise<Output> { /* … */ }

Never routed: cancellation (a run past its executionTimeout is cancelled too), dead-lettering, and the failures that mean the deployment is broken (non-determinism, a ctx call nested in a step). Those keep their usual meaning. A step that times out fails like any other step, so its failure is routable.

Typing

The method is checked against the workflow: it receives OnErrorInput<Input>, where Input is run's input, and must return run's output type. A handler that returns a different shape doesn't compile, so ctx.all over the workflow stays honestly typed.

Remote workflows

For a workflow the engine serves by convention (a Python body, or any worker it discovers by its live queue), the routes come from what the workers announce in their handshake descriptor, the same place the engine reads the workflow's version from. In the Python SDK:

@worker.workflow("line", on_error=[{"code": "invalid_line", "workflow": "line-rejected"}])
def line(ctx, input): ...

Or declare the handler inline, the Python twin of an @OnError method: .on_error(code) on the workflow function registers the handler as <workflow>.onError.<function> and announces the route to it. code is a code string, an exception class with a code attribute, or nothing for the catch-all:

@worker.workflow("line")
def line(ctx, input): ...

@line.on_error(InvalidLine)
def reject_line(ctx, failure):  # {"runId", "workflow", "input", "error"}
    ctx.step("record_rejected_line", {"line": failure["input"], "error": failure["error"]})
    return {"ok": 0, "failed": 1}

A workflow class does the same with methods: @on_error(InvalidLine) on a method of a class registered with @worker.workflow("line") makes it the handler line.onError.<method>. See Python: workflows as classes.

The handler can be a workflow of any runtime; the engine only starts a run by name. If the live workers announce different routes for the workflow (a rollout in progress), the failure is not routed, the same way two announced versions leave a run's version undeclared, and the engine emits run.unrouted with the reason. The engine reuses what it read for a few seconds, so a burst of failures costs one read.

Catching a misnamed handler

After boot, the Nest module warns about any route whose handler workflow is neither registered in the process nor served by a live worker, so a typo shows up before the first failure goes nowhere. A route unserved at boot is checked again 30 seconds later before it is reported, so a handler whose workers start after the API is not flagged. The same check is engine.unservedOnErrorRoutes().

Seeing it

The console shows the failure's code and its details (as JSON) on the failed step and on a failed run. Handoffs are counted as routed in the metrics, marked on the run's OpenTelemetry span, and tagged routed in Telescope.

A handler in another workflow

To route to a separate workflow (shared across workflows, or one registered by another runtime), list it on the class:

@Workflow({
  name: 'line',
  onError: [{ code: 'upstream_unavailable', workflow: UpstreamOutageWorkflow }],
})

On the engine directly, both forms are register(name, version, fn, { onError: [{ code?, workflow }] }). registerRemote / remote take the same option, for a remote workflow registered by hand rather than served by convention.

@OnError, ctx.allSettled and @DeadLetter

Who decidesFires on
@OnErrorthe workflow, for every callera run that failed with a routed code
ctx.allSettledthe parent, for one fana failed child or step
@DeadLetterthe workflowa run that crash recovery gave up on (dead)

Reach for @OnError when the failure policy belongs to the workflow, and for ctx.allSettled when it belongs to one caller.

On this page