/** * Maps a preview response (or a refused call) to the panel the console shows. * Boundary: pure mapping, no I/O. The outcome is only ever held in memory for display; the * console never stores sample rows. Server message text is not shown, only the status and * failure kind pick the wording. */ import type { PreviewFailure, PreviewIllegalType, PreviewOutcome, PreviewRows } from "./types"; import { isDisabledRejection } from "./errors"; export const FAILURE_KINDS = [ "timeout", "unreachable", "auth_failed", "rejected", "sql_error", "limit_exceeded", "validation", "engine_readonly_unsupported", ] as const; export type PreviewView = | { kind: "none" } | { kind: "running" } | { kind: "success"; outcome: PreviewRows } | { kind: "empty"; outcome: PreviewRows } | { kind: "illegal_type"; outcome: PreviewIllegalType } | { kind: "failure"; failureKind: string; outcome: PreviewFailure } /** scope "query": the definition is disabled (HTTP 409); "datasource": its source is disabled. */ | { kind: "disabled"; scope: "query" | "datasource" } | { kind: "circuit_open"; until: string | null } | { kind: "error"; text: string }; /** Connection-class failures count against the datasource circuit (M03); the others do not. */ export function isConnectionKind(kind: string): boolean { return kind === "unreachable" || kind === "auth_failed" || kind === "rejected"; } export function viewFromOutcome(outcome: PreviewOutcome): PreviewView { if (outcome.status === "illegal_type") { return { kind: "illegal_type", outcome }; } if (outcome.status !== "failure") { return outcome.status === "success" ? { kind: "success", outcome } : { kind: "empty", outcome }; } const kind = outcome.failure_kind ?? "unknown"; if (kind === "disabled") { return { kind: "disabled", scope: "datasource" }; } if (kind === "circuit_open") { const until = outcome.details?.circuit_open_until; return { kind: "circuit_open", until: typeof until === "string" ? until : null }; } return { kind: "failure", failureKind: kind, outcome }; } /** A refused preview call: 409 disabled gets its own panel; anything else is a plain error. */ export function viewFromError(error: unknown, describe: (error: unknown) => string): PreviewView { if (isDisabledRejection(error)) { return { kind: "disabled", scope: "query" }; } return { kind: "error", text: describe(error) }; }