Обзор
functionError Handling

recover

Recovers expected domain failures into success or a new domain failure.

Signature

tsSignature
export function recover<I, O, O2, E, E2, C extends object>(
source: Behavior<I, O, E, C>,
handler: (error: E, input: I, context: TaskContext<C>) => Awaitable<Result<O2, E2>>,
): Behavior<I, O | O2, E2, C>;

Imports

tsImport
import { recover } from "@/lib/flow/recover";
import { recover } from "@/lib/flow";

Type Parameters

I

Input type.

O

Source output type.

O2

Handler output type.

E

Source domain error type.

E2

Handler domain error type.

C

Custom context type.

Parameters

source
Behavior<I, O, E, C>

Behavior whose domain failures should be recovered.

handler
(error: E, input: I, context: TaskContext<C>) => Awaitable<Result<O2, E2>>

Callback invoked for source domain failures.

Returns

A behavior that can produce source output, handler output, or handler failure.

Return type
Behavior<I, O | O2, E2, C>

Details

Runtime errors bypass the handler. The returned behavior replaces the source domain error type

with the handler error type.

Examples

tsExample
const loadWithFallback = recover(loadUser, (_error, _input, context) =>
context.ok({ id: "anonymous" }),
);
Awaitable

Represents a value that may be returned directly or through a Promise.

type
Behavior

Executable Flow workflow unit.

type
input

Creates an identity behavior for starting typed pipelines.

function
Result

Explicit success-or-failure outcome used throughout Flow.

type

Source

tsflow/recover.ts:28-47
export function recover<I, O, O2, E, E2, C extends object>(
source: Behavior<I, O, E, C>,
handler: (error: E, input: I, context: TaskContext<C>) => Awaitable<Result<O2, E2>>,
): Behavior<I, O | O2, E2, C> {
return async (input, context) => {
const result = await runBehavior(source, input, context);
if (result.ok) {
return result;
}
if (isRuntimeError(result.error)) {
return err(result.error);
}
return task((_input: I, taskContext: TaskContext<C>) =>
handler(result.error as E, input, taskContext),
)(input, context);
};
}