@brickkit/core/workflow
Typed errors for workflow steps, safe to import in workflow code.
@brickkit/core/workflow exports 4 names: 3 functions, 1 type. It also re-exports 1 from @sinclair/typebox.
import { defineError, stepErrors, stepFailure } from "@brickkit/core/workflow";Functions
| Function | Description |
|---|---|
defineError | Define a reusable error. |
stepErrors | Helpers that raise typed errors from a workflow step: error.CODE(message?, details?), as in an action or listener. |
stepFailure | Read a failed step in the workflow: the typed error it raised with stepErrors, or \{ code: "UNKNOWN", message, cause \} for anything else (a step that threw so… |
defineError
function defineError<
const TCode extends string,
TDetails extends TSchema | undefined = undefined,
>(
code: TCode,
definition: { status?: number; message: string; details?: TDetails },
): { [K in TCode]: { status?: number; message: string } & (TDetails extends TSchema ? { details: TDetails } : {}); }Define a reusable error. Spread it into the errors of any action, listener,
command, socket or step; give it a details schema and
error.CODE(message, details) checks the details against it in types. The
details are not validated at runtime.
Parameters
| Name | Type | Description |
|---|---|---|
code | TCode | |
definition | { status?: number; message: string; details?: TDetails } |
Returns { [K in TCode]: { status?: number; message: string } & (TDetails extends TSchema ? { details: TDetails } : {}); }
Example
const SlugExists = defineError("SLUG_EXISTS", {
status: 409,
message: "Slug is already taken",
details: t.Object({ slug: t.String() }),
});
// errors: { ...SlugExists } then error.SLUG_EXISTS(undefined, { slug })Source: packages/core/src/workflow.ts:26
stepErrors
function stepErrors<const TErrors extends Record<string, ActionErrorDefinition>>(
errors: TErrors,
): StepErrorBuilder<TErrors>Helpers that raise typed errors from a workflow step: error.CODE(message?, details?), as in an action or listener. A declared error is an outcome, not
a glitch, so the Workflow SDK does not retry the step: the workflow's await
rejects at once. Read it in the workflow with stepFailure.
Parameters
| Name | Type | Description |
|---|---|---|
errors | TErrors | The codes the step can raise: defineError(...) results or errorsOf(action), spread into one object. |
Returns StepErrorBuilder<TErrors> — One helper per code. Each always throws.
Example
import { stepErrors } from "@brickkit/core/workflow";
export const carrierErrors = { ...CarrierRejected };
const error = stepErrors(carrierErrors);
async function bookCarrier(orderId: string) {
"use step";
const booking = await carrier.book(orderId);
if (!booking.ok) error.CARRIER_REJECTED(undefined, { reason: booking.reason });
return booking.trackingId;
}Source: packages/core/src/workflow.ts:83
stepFailure
function stepFailure<
const TErrors extends Record<string, ActionErrorDefinition>,
>(
error: unknown,
errors: TErrors,
): ListenerFailure<TErrors>Read a failed step in the workflow: the typed error it raised with
stepErrors, or { code: "UNKNOWN", message, cause } for anything
else (a step that threw something undeclared, or ran out of retries). The
same shape a listener's retry receives.
Parameters
| Name | Type | Description |
|---|---|---|
error | unknown | What the workflow caught from await step(). |
errors | TErrors | The codes to recognize: the same object given to stepErrors. |
Returns ListenerFailure<TErrors> — A union with one member per declared code, narrowed by code.
Example
try {
await bookCarrier(order.id);
} catch (caught) {
const failure = stepFailure(caught, carrierErrors);
if (failure.code !== "CARRIER_REJECTED") throw caught;
await notifyCustomer(order, failure.details.reason);
}Source: packages/core/src/workflow.ts:127
Types
StepErrorBuilder
type StepErrorBuilder<
TErrors extends Record<string, ActionErrorDefinition>,
> = {
[K in keyof TErrors]: TErrors[K] extends { details: TSchema }
? (
message: string | undefined,
details: ErrorDetails<TErrors[K]>,
options?: ErrorOptions,
) => never
: (message?: string, details?: unknown, options?: ErrorOptions) => never;
};The error helpers of a step: one per declared code, each throws a step error that is not retried.
Source: packages/core/src/workflow.ts:49
Re-exported from @sinclair/typebox
These come from @sinclair/typebox, which documents them. They are exported here so one import covers them.
| Name | Kind | Description |
|---|---|---|
t | variable | JavaScript Type Builder with Static Resolution for TypeScript |