import { ZodError } from "zod"; import { LOTSE_ERROR_CODES, type LotseActionErrorCode, type LotseActionState } from "@/lib/lotse/action-state"; import { ServiceError } from "@/server/services/context"; import { ForbiddenError } from "@/server/rbac"; import { ModuleDisabledError } from "@/server/modules"; /** Map thrown errors of Lotse actions to a displayable state (plain-language message key, no internals). */ export function lotseErrorState(err: unknown): LotseActionState { const at = Date.now(); if (err instanceof ServiceError) { const reason = (err.details as { reason?: string } | undefined)?.reason; const code = reason && (LOTSE_ERROR_CODES as readonly string[]).includes(reason) ? (reason as LotseActionErrorCode) : err.code === "blocked" ? "not_editable" : err.code; return { status: "error", code, at }; } if (err instanceof ModuleDisabledError) return { status: "error", code: "disabled", at }; if (err instanceof ZodError) return { status: "error", code: "invalid", at }; if (err instanceof ForbiddenError) return { status: "error", code: "forbidden", at }; console.error("[actions/lotse]", err); return { status: "error", code: "generic", at }; } export const str = (fd: FormData, key: string): string | undefined => { const v = fd.get(key); return typeof v === "string" ? v : undefined; };