clone

a deno framework for local-first web apps that open instantly, work offline and sync when users choose

feat/184-task-notice-migration
lofi / package / testing / scenario / errors.ts
errors.ts 1.8K
1/** The lifecycle stages a scenario moves through, in order. */2export type ScenarioStage =3 | "boot"4 | "deploy"5 | "peers"6 | "body"7 | "convergence"8 | "assertion"9 | "teardown";1011/** Options for {@link ScenarioError}, extending `ErrorOptions` with scenario context. */12export interface ScenarioErrorOptions extends ErrorOptions {13 /** The peer whose action failed, when the failure is attributable to one peer. */14 peer?: string;15 /**16 * A preformatted multi-line report — e.g. the per-table, per-peer diff a17 * failed convergence produces — appended to the error message.18 */19 details?: string;20}2122/** Thrown when a scenario fails, naming the lifecycle stage that failed via {@link stage}. */23export class ScenarioError extends Error {24 /** Always `"ScenarioError"`. */25 override readonly name = "ScenarioError";2627 /** The peer whose action failed, or `undefined` when no single peer is at fault. */28 readonly peer?: string;2930 /** The preformatted report passed via {@link ScenarioErrorOptions.details}, if any. */31 readonly details?: string;3233 /**34 * Builds the error, recording which lifecycle stage was in progress.35 * @param stage the lifecycle stage that was in progress when the scenario failed36 * @param message what went wrong, in one line37 * @param options standard error options plus the failing peer and a detail report38 */39 constructor(readonly stage: ScenarioStage, message: string, options?: ScenarioErrorOptions) {40 const peerNote = options?.peer !== undefined ? ` (peer ${options.peer})` : "";41 const detailNote = options?.details !== undefined ? `\n${options.details}` : "";42 super(`Scenario failed during ${stage}${peerNote}: ${message}${detailNote}`, options);43 this.peer = options?.peer;44 this.details = options?.details;45 }46}