/** * Runtime error types. * * Provides structured error types for validation failures, * integration errors, and execution errors. */ /** * Error codes for categorizing runtime errors. */ export const ErrorCode = { /** Input validation failed against the Zod schema */ INPUT_VALIDATION: "INPUT_VALIDATION", /** Output validation failed against the Zod schema */ OUTPUT_VALIDATION: "OUTPUT_VALIDATION", /** Integration not found or not configured */ INTEGRATION_NOT_FOUND: "INTEGRATION_NOT_FOUND", /** Integration operation failed */ INTEGRATION_ERROR: "INTEGRATION_ERROR", /** User code threw an error during execution */ EXECUTION_ERROR: "EXECUTION_ERROR", /** Internal SDK error */ INTERNAL_ERROR: "INTERNAL_ERROR", } as const; export type ErrorCodeType = (typeof ErrorCode)[keyof typeof ErrorCode]; /** * Base class for SDK runtime errors. */ export class SdkError extends Error { readonly code: ErrorCodeType; readonly details?: unknown; constructor(code: ErrorCodeType, message: string, details?: unknown) { super(message); this.name = "SdkError"; this.code = code; this.details = details; // Fix prototype chain for proper instanceof checks in transpiled output Object.setPrototypeOf(this, new.target.prototype); // Maintain proper stack trace in V8 environments if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } } /** * Converts the error to a plain object for serialization. */ toJSON(): { code: ErrorCodeType; message: string; details?: unknown } { return { code: this.code, message: this.message, details: this.details, }; } } /** * Error thrown when input validation fails. */ export class InputValidationError extends SdkError { constructor(message: string, details?: unknown) { super(ErrorCode.INPUT_VALIDATION, message, details); this.name = "InputValidationError"; } } /** * Error thrown when output validation fails. */ export class OutputValidationError extends SdkError { constructor(message: string, details?: unknown) { super(ErrorCode.OUTPUT_VALIDATION, message, details); this.name = "OutputValidationError"; } } /** * Error thrown when a requested integration is not found. */ export class IntegrationNotFoundError extends SdkError { readonly integrationName: string; constructor(integrationName: string) { super( ErrorCode.INTEGRATION_NOT_FOUND, `Integration not found: "${integrationName}"`, { integrationName }, ); this.name = "IntegrationNotFoundError"; this.integrationName = integrationName; } } /** * Error thrown when an integration operation fails. */ export class IntegrationError extends SdkError { readonly integrationName: string; readonly operation: string; constructor(integrationName: string, operation: string, cause?: unknown) { const message = `Integration "${integrationName}" failed during "${operation}"`; super(ErrorCode.INTEGRATION_ERROR, message, { integrationName, operation, cause, }); this.name = "IntegrationError"; this.integrationName = integrationName; this.operation = operation; } } /** * Error thrown when user code fails during execution. */ export class ExecutionError extends SdkError { constructor(message: string, cause?: unknown) { super(ErrorCode.EXECUTION_ERROR, message, { cause }); this.name = "ExecutionError"; } }