/** * Error classes for SDK runtime errors. */ import type { z } from "zod"; export const REDACTED_BINARY_RESPONSE_DATA = Object.freeze({ dataType: "binary", redacted: true, }); function isWorkerBufferJson(data: unknown): boolean { if (typeof data !== "object" || data === null) { return false; } if (!("data" in data) || !("type" in data)) { return false; } return data.type === "Buffer" && Array.isArray(data.data); } function redactBinaryValidationData(data: unknown): unknown { if (data instanceof Uint8Array || isWorkerBufferJson(data)) { return REDACTED_BINARY_RESPONSE_DATA; } return data; } /** * Error thrown when REST API response or request body validation fails. * * Contains the complete Zod error object and the actual data that failed validation, * providing full context for debugging schema mismatches. */ export class RestApiValidationError extends Error { readonly details: { data: unknown; zodError: z.ZodError; }; constructor( message: string, details: { data: unknown; zodError: z.ZodError; }, ) { super(message); this.name = "RestApiValidationError"; this.details = { data: redactBinaryValidationData(details.data), zodError: details.zodError, }; Object.setPrototypeOf(this, RestApiValidationError.prototype); } } /** * Error thrown when query results fail schema validation. * * Contains detailed information about which row failed validation * and the specific field-level errors from Zod. */ export class QueryValidationError extends Error { constructor( message: string, public readonly details: { /** Index of the row that failed validation (0-based) */ rowIndex: number; /** Array of field-level validation errors from Zod */ errors: Array<{ path: (string | number)[]; message: string }>; /** The raw row data that failed validation */ row: unknown; }, ) { super(message); this.name = "QueryValidationError"; // Ensure proper prototype chain for instanceof checks Object.setPrototypeOf(this, QueryValidationError.prototype); } } /** * Error thrown when code execution fails. * * Contains context about the code, bindings, and any validation errors * that occurred during execution. */ export class CodeExecutionError extends Error { constructor( message: string, public readonly details?: { /** The code that was executed */ code?: string; /** Bindings that were passed to the code */ bindings?: Record; /** The actual result that failed validation */ result?: unknown; /** Zod validation error if validation failed */ zodError?: z.ZodError; /** Original error from the execution runtime */ originalError?: unknown; }, ) { super(message); this.name = "CodeExecutionError"; // Ensure proper prototype chain for instanceof checks Object.setPrototypeOf(this, CodeExecutionError.prototype); } }