//#region src/core/degraded.d.ts /** Small, JSON-serializable context for a degrade: an id, a count, a status. * It's logged, so never put a secret, a token, or personal data in it. */ type DegradedDetails = Readonly>; /** What a {@link DegradedListener} receives for each {@link reportDegraded} call. */ interface DegradedEvent { /** Which fallback fired, as a stable dotted name, for example, `"forms.notify.webhook"`. */ readonly name: string; /** The cause, reduced to one line of text: `"TypeError: fetch failed"`. An * `UpstreamError` adds its operation and what the provider said. */ readonly message: string; /** * The cause, as passed, unless it holds a failed query: then it's * `loggableError`'s copy, of the same class and with the same own fields, * without the SQL and bound values. `undefined` when there wasn't one. */ readonly cause: unknown; /** The details, as passed. */ readonly details: DegradedDetails | undefined; } /** Hears every degrade in this isolate. See {@link onDegraded}. */ type DegradedListener = (event: DegradedEvent) => void; /** The line prefix every degrade logs with. Grep a log stream for it. */ declare const DEGRADED_LOG_PREFIX = "[louise] degraded"; /** * Report that a fallback fired: the code caught a failure and served something * lesser (seed or stale content, an empty result, a skipped side effect) rather * than failing the request. * * Logs exactly one line at error level, in a fixed shape you can grep for: * * ```text * [louise] degraded :
* ``` * * `name` says which fallback fired. Keep it stable and dotted, area first * (`"content.read"`, `"commerce.products"`), so one search finds every * occurrence across deploys. `cause` is whatever the `catch` caught—an `Error`, * a string, anything. `details` is optional, small, JSON-serializable context. * * It returns nothing and never throws, whatever `cause` and `details` hold: a * reporter that threw would turn a graceful degrade into the crash it was * written to avoid. * * Import it from the `errors` subpath. (The example leaves the import out: * this comment ships inside the lightweight Turnstile entry, whose emitted * code the export-map check scans for package imports.) * * @example * ```ts * let products; * try { * products = await listProducts(env); * } catch (err) { * reportDegraded("commerce.products", err, { source: "seed" }); * products = seedProducts; * } * ``` */ declare function reportDegraded(name: string, cause?: unknown, details?: DegradedDetails): void; /** * Listen for every {@link reportDegraded} call in this isolate, for example, * to forward degrades to an error tracker or count them in a metric. Returns a * function that removes the listener. * * Listeners run synchronously, after the log line, and a listener that throws * is ignored. Register one at module scope rather than per request, because * the set lives as long as the isolate does. Louise's own incident capture is * meant to hook in here, so a site's `reportDegraded` calls won't need to * change when it does. */ declare function onDegraded(listener: DegradedListener): () => void; //#endregion //#region src/core/query-error.d.ts /** * A value that's safe to pass to `console.error`. A failed query's error, or * an error that holds one anywhere (in its `cause` chain, in `AggregateError`'s * `errors`, or in another own field), comes back as a copy whose query errors * carry their reduced message and a stack of frames only, so the log line * keeps the trace and loses the values. Each copied error keeps its original's * class, so `instanceof` still matches, and its own fields, such as a * `LouiseError`'s `name`, `code`, and `status`; a query error's `query` and * `params` are left out, and so is any field it can't check. A query error's * `cause`, the driver's own error, becomes its first line cut at the first * quoted literal, since a driver can quote a value. An error too big or too * deep to check (a `cause` chain past ten levels, or more than 5,000 fields or * values) is copied too, with what's past the limit left out. A string that * quotes a failed query comes back reduced. Anything else comes back as it * was. It never throws. */ declare function loggableError(value: unknown): unknown; //#endregion //#region src/core/errors.d.ts declare global { interface ErrorConstructor { captureStackTrace(targetObject: object, constructorOpt?: Function): void; } } /** * Base class for all Louise errors. * All primitives throw LouiseError or a typed subclass—never a raw Error. * * @example * try { * await createMagicLink({ kv, email, to }) * } catch (e) { * if (e instanceof LouiseAuthError) { * // auth-specific handling * } else if (e instanceof LouiseError) { * // any Louise error—e.code tells you which primitive threw * } else { * throw e // re-throw unknown errors * } * } */ declare class LouiseError extends Error { readonly code: string; readonly cause?: unknown | undefined; constructor(message: string, code: string, cause?: unknown | undefined); } /** For authentication / sign-in failures. */ declare class LouiseAuthError extends LouiseError { constructor(message: string, cause?: unknown); } /** For database (D1) failures. */ declare class LouiseDbError extends LouiseError { constructor(message: string, cause?: unknown); } /** * The database hasn't applied migrations the running code needs—a deploy * landed before its `wrangler d1 migrations apply`. `files` names the pending * migrations, in order. Thrown by `assertMigrationsApplied` * (`louise-toolkit/db`); a subclass of LouiseDbError, so `instanceof * LouiseDbError` still catches it. */ declare class LouisePendingMigrationsError extends LouiseDbError { readonly files: readonly string[]; constructor(files: readonly string[]); } /** For object-storage (R2) failures. */ declare class LouiseStorageError extends LouiseError { constructor(message: string, cause?: unknown); } /** For cache (KV) failures. */ declare class LouiseCacheError extends LouiseError { constructor(message: string, cause?: unknown); } /** For email-sending failures. */ declare class LouiseEmailError extends LouiseError { constructor(message: string, cause?: unknown); } /** For session failures. */ declare class LouiseSessionError extends LouiseError { constructor(message: string, cause?: unknown); } /** For queue failures. */ declare class LouiseQueueError extends LouiseError { constructor(message: string, cause?: unknown); } /** For workflow failures (starting an instance; a step's own throw surfaces * as-is so Cloudflare Workflows can retry it per the step's config). */ declare class LouiseWorkflowError extends LouiseError { constructor(message: string, cause?: unknown); } /** For content (collection / Local API) failures. */ declare class LouiseContentError extends LouiseError { constructor(message: string, cause?: unknown); } /** * Thrown by the content `createLocalApi` when a collection's `access` function * rejects an operation. A distinct subclass (rather than a plain * LouiseContentError) so a routing layer can map it to **403** by `instanceof`, * not by matching on message text. */ declare class LouiseAccessDeniedError extends LouiseContentError { constructor(message: string, cause?: unknown); } /** * One failed field-validation rule (issue #16). `path` is the field's key * (flattened, for example, `shippingAddress_city` for a group subfield). `severity` * lets a rule warn without blocking the write—only `"error"` violations * cause createLocalApi to throw; `"warning"` ones are carried through for * the editor to surface non-blockingly. */ interface ValidationViolation { path: string; message: string; severity: "error" | "warning"; } /** * Thrown by `createLocalApi` when a collection's field-validation rules * (Sanity-style chainable `Rule` API—see content/validation.ts) reject a * create/update. Carries the structured `violations` so a UI can surface * per-field messages and a routing layer can map it to **422** by `instanceof` * rather than message matching. A subclass of LouiseContentError, so existing * `instanceof LouiseContentError` handling still catches it. Only `"error"`-severity * violations are ever thrown. */ declare class LouiseValidationError extends LouiseContentError { readonly violations: ValidationViolation[]; constructor(message: string, violations: ValidationViolation[], cause?: unknown); } /** * Carries an HTTP `status` and parsed `body` for a failed request against a * Louise-backed API surface, so callers can branch on `status` (for example, 403 → * access denied, 404 → not found) instead of re-parsing `{ error: string }` * response bodies by hand. */ declare class LouiseApiError extends LouiseError { readonly status: number; readonly body: unknown; constructor(message: string, status: number, body: unknown); } //#endregion export { reportDegraded as S, DEGRADED_LOG_PREFIX as _, LouiseContentError as a, DegradedListener as b, LouiseError as c, LouiseSessionError as d, LouiseStorageError as f, loggableError as g, ValidationViolation as h, LouiseCacheError as i, LouisePendingMigrationsError as l, LouiseWorkflowError as m, LouiseApiError as n, LouiseDbError as o, LouiseValidationError as p, LouiseAuthError as r, LouiseEmailError as s, LouiseAccessDeniedError as t, LouiseQueueError as u, DegradedDetails as v, onDegraded as x, DegradedEvent as y };