UNPKG

@plust/datasleuth

Version:

Build LLM-powered research pipelines and output structured data.

217 lines 6.01 kB
/** * Base implementation for all specialized research error classes */ export class BaseResearchError extends Error { constructor(options) { super(options.message); this.name = 'ResearchError'; this.code = options.code; this.step = options.step; this.details = options.details; this.retry = options.retry ?? false; this.suggestions = options.suggestions ?? []; // Maintain proper stack traces for where our error was thrown if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } } /** * Creates a formatted error message with details for logging */ getFormattedMessage() { let message = `[${this.code}] ${this.message}`; if (this.step) { message = `[Step: ${this.step}] ${message}`; } if (this.suggestions && this.suggestions.length > 0) { message += `\nSuggestions:\n${this.suggestions.map((s) => `- ${s}`).join('\n')}`; } return message; } } /** * Error thrown when there are issues with the research configuration */ export class ConfigurationError extends BaseResearchError { constructor(options) { super({ ...options, code: 'configuration_error' }); this.name = 'ConfigurationError'; } } /** * Error thrown when input or output validation fails */ export class ValidationError extends BaseResearchError { constructor(options) { super({ ...options, code: 'validation_error' }); this.name = 'ValidationError'; } } /** * Error thrown when network operations fail */ export class NetworkError extends BaseResearchError { constructor(options) { super({ ...options, code: 'network_error', retry: options.retry ?? true }); this.name = 'NetworkError'; } } /** * Error thrown when external API calls fail */ export class ApiError extends BaseResearchError { constructor(options) { super({ ...options, code: 'api_error', retry: options.retry ?? false, details: { ...options.details, statusCode: options.statusCode, }, }); this.name = 'ApiError'; } } /** * Error thrown when LLM operations fail */ export class LLMError extends BaseResearchError { constructor(options) { super({ ...options, code: 'llm_error', retry: options.retry ?? true }); this.name = 'LLMError'; } } /** * Error thrown when search operations fail */ export class SearchError extends BaseResearchError { constructor(options) { super({ ...options, code: 'search_error', retry: options.retry ?? true }); this.name = 'SearchError'; } } /** * Error thrown when content extraction fails */ export class ExtractionError extends BaseResearchError { constructor(options) { super({ ...options, code: 'extraction_error' }); this.name = 'ExtractionError'; } } /** * Error thrown when pipeline execution fails */ export class PipelineError extends BaseResearchError { constructor(options) { super({ ...options, code: 'pipeline_error' }); this.name = 'PipelineError'; } } /** * Error thrown when processing operations fail */ export class ProcessingError extends BaseResearchError { constructor(options) { super({ ...options, code: 'processing_error' }); this.name = 'ProcessingError'; } } /** * Error thrown when an operation times out */ export class TimeoutError extends BaseResearchError { constructor(options) { super({ ...options, code: 'timeout_error' }); this.name = 'TimeoutError'; } } /** * Error thrown when maximum iterations are reached */ export class MaxIterationsError extends BaseResearchError { constructor(options) { super({ ...options, code: 'max_iterations_error' }); this.name = 'MaxIterationsError'; } } /** * Type guard to check if an error is a ResearchError */ export function isResearchError(error) { return error instanceof Error && 'code' in error && typeof error.code === 'string'; } /** * Type guard to check if an error is a NetworkError */ export function isNetworkError(error) { return error instanceof NetworkError; } /** * Type guard to check if an error is an ApiError */ export function isApiError(error) { return error instanceof ApiError; } /** * Type guard to check if an error is an LLMError */ export function isLLMError(error) { return error instanceof LLMError; } /** * Type guard to check if an error is a SearchError */ export function isSearchError(error) { return error instanceof SearchError; } /** * Type guard to check if an error is a ValidationError */ export function isValidationError(error) { return error instanceof ValidationError; } /** * Type guard to check if an error is a ConfigurationError */ export function isConfigurationError(error) { return error instanceof ConfigurationError; } /** * Type guard to check if an error is a ProcessingError */ export function isProcessingError(error) { return error instanceof ProcessingError; } /** * Type guard to check if an error is a TimeoutError */ export function isTimeoutError(error) { return error instanceof TimeoutError; } /** * Type guard to check if an error is a MaxIterationsError */ export function isMaxIterationsError(error) { return error instanceof MaxIterationsError; } /** * Type guard to check if an error is an ExtractionError */ export function isExtractionError(error) { return error instanceof ExtractionError; } /** * Type guard to check if an error is a PipelineError */ export function isPipelineError(error) { return error instanceof PipelineError; } /** * Type guard to check if an error is retryable */ export function isRetryableError(error) { return isResearchError(error) && error.retry === true; } //# sourceMappingURL=errors.js.map