UNPKG

@zhanghongping/json-sage-workflow-cli

Version:

An intelligent JSON processing workflow system with improved error handling and configuration

96 lines (83 loc) 3.44 kB
import { SchemaOptions } from '../types'; import { safeJsonParse, safeExecute, retryWithBackoff } from '../utils/error-handler'; const DEFAULT_CONFIG = { retry: { maxRetries: 3, initialDelay: 1000, maxDelay: 10000, shouldRetry: (error: any) => true }, errorMessages: { jsonParse: 'Error parsing JSON data. Please ensure the input is valid JSON.', commandError: 'Error executing command. Please check your input and try again.', fileNotFound: 'File not found. Please check the file path and try again.', invalidConfig: 'Invalid configuration. Please check your config file.', networkError: 'Network error occurred. Please check your connection and try again.', timeoutError: 'Operation timed out. Please try again.', validationError: 'Validation error occurred. Please check your input.', optimizationError: 'Error during optimization process.', unknownError: 'An unexpected error occurred. Please try again.' } }; export async function generateSchema(description: string, options?: SchemaOptions) { try { const result = await retryWithBackoff( async () => { const parseResult = safeJsonParse(description); if (!parseResult.success) { throw parseResult.error; } const executeResult = await safeExecute(async () => { // TODO: Implement schema generation logic return { schema: { type: "object", properties: {} } }; }); if (!executeResult.success || !executeResult.data) { throw executeResult.error || new Error('Failed to generate schema'); } return executeResult.data; }, DEFAULT_CONFIG.retry ); if (!result || !result.schema) { throw new Error('Invalid schema generated'); } return result.schema; } catch (error) { throw new Error(`Failed to generate schema: ${(error as Error).message}`); } } export async function saveSchema(schema: any, filePath: string, format: boolean = true) { try { const absolutePath = resolve(filePath); const content = JSON.stringify(schema, null, format ? 2 : 0); await safeExecute(async () => { writeFileSync(absolutePath, content, 'utf8'); }, DEFAULT_CONFIG.errorMessages.fileNotFound); } catch (error) { throw new Error(`Failed to save schema: ${(error as Error).message}`); } } export async function loadSchema(filePath: string) { try { const absolutePath = resolve(filePath); const content = await safeExecute( async () => readFileSync(absolutePath, 'utf8'), DEFAULT_CONFIG.errorMessages.fileNotFound ); if (!content.success || !content.data) { throw new Error(DEFAULT_CONFIG.errorMessages.fileNotFound); } const parseResult = safeJsonParse(content.data); if (!parseResult.success) { throw parseResult.error; } return parseResult.data; } catch (error) { throw new Error(`Failed to load schema: ${(error as Error).message}`); } }