/** * Failures a caller cannot fix by changing its input. Handles throw these; anything a * caller can fix and retry is returned as an `Outcome` instead. * * Workers RPC keeps only an error's `name` and `message`, so the code travels in the * message as `refs: `. Callers read it with `refsErrorCode`, never * with `instanceof`. */ export type RefsErrorCode = /** The resource does not exist, or the actor is not allowed to know that it exists. */ | "not_found" /** The operation needs a signed-in human or agent, and the actor is anonymous. */ | "sign_in_required"; const CODES: readonly RefsErrorCode[] = ["not_found", "sign_in_required"]; export function refsErrorCode(error: unknown): RefsErrorCode | null { const message = error instanceof Error ? error.message : typeof error === "string" ? error : ""; const code = /^refs:([a-z_]+)\b/.exec(message)?.[1]; return CODES.find((candidate) => candidate === code) ?? null; } /** The human-readable part of a refs error, without its code. */ export function refsErrorDescription(error: unknown): string { const message = error instanceof Error ? error.message : String(error); return message.replace(/^refs:[a-z_]+\s*/, ""); } export function refsErrorMessage(code: RefsErrorCode, description: string): string { return `refs:${code} ${description}`; }