import * as z from "zod/mini"; import { clientMetaSchema } from "./client-meta.js"; // --------------------------------------------------------------------------- // Error-telemetry definitions shared across widget / host. // --------------------------------------------------------------------------- export const ErrorCategory = { INTERNAL: "INTERNAL", INVALID_USAGE: "INVALID_USAGE", HOST: "HOST", } as const; export type ErrorCategory = (typeof ErrorCategory)[keyof typeof ErrorCategory]; export const ErrorCategorySchema = z.enum([ ErrorCategory.INTERNAL, ErrorCategory.INVALID_USAGE, ErrorCategory.HOST, ]); export const ErrorEventSchema = z.object({ id: z.string(), name: z.string(), message: z.string(), stack: z.optional(z.string()), category: ErrorCategorySchema, timestamp: z.number(), probability: z.optional(z.number()), context: z.object({ widgetVersion: z.string(), clientMeta: z.optional(clientMetaSchema), /** When true, telemetry was captured with debug mode on (affects how to interpret context). */ debug: z.optional(z.boolean()), }), }); export type ErrorEvent = z.output; export type ProtocolErrorCode = "PARSE" | "VERSION" | "UNKNOWN_TYPE"; const MAX_DETAIL_LENGTH = 80; export class ProtocolError extends Error { code: ProtocolErrorCode; details: string; constructor(code: ProtocolErrorCode, details: string) { const short = details.length > MAX_DETAIL_LENGTH ? details.slice(0, MAX_DETAIL_LENGTH) : details; super(short); this.code = code; this.details = short; } }