Genel bakış
functionError Handling

mapError

Maps expected domain errors without catching runtime errors.

Signature

tsSignature
export function mapError<I, O, E, E2, C extends object>(
source: Behavior<I, O, E, C>,
mapper: (error: E, context: RuntimeContext<C>) => Awaitable<E2>,
): Behavior<I, O, E2, C>;

Imports

tsImport
import { mapError } from "@/lib/flow/map-error";
import { mapError } from "@/lib/flow";

Type Parameters

I

Source input type.

O

Source output type.

E

Source domain error type.

E2

Mapped domain error type.

C

Custom context type.

Parameters

source
Behavior<I, O, E, C>

Behavior whose domain errors should be mapped.

mapper
(error: E, context: RuntimeContext<C>) => Awaitable<E2>

Callback invoked for source domain errors.

Returns

A behavior with the same output type and mapped domain error type.

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

Details

Successful results pass through unchanged. Runtime errors such as cancellation and thrown

exceptions bypass the mapper so only expected failures are transformed.

Examples

tsExample
const publicLoad = mapError(loadUser, (error) => ({ kind: "load-user", cause: error }));
Awaitable

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

type
Behavior

Executable Flow workflow unit.

type
thrown

Re-exports core result helpers used by advanced record internals.

re-export

Source

tsflow/map-error.ts:24-41
export function mapError<I, O, E, E2, C extends object>(
source: Behavior<I, O, E, C>,
mapper: (error: E, context: RuntimeContext<C>) => Awaitable<E2>,
): Behavior<I, O, 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 err(await mapper(result.error, context));
};
}