UNPKG

@ordojs/cli

Version:

Command-line interface for OrdoJS framework

98 lines 3.09 kB
/** * @fileoverview OrdoJS CLI - Error handling utilities */ import chalk from 'chalk'; import { logger } from './logger.js'; /** * Error types */ export var ErrorType; (function (ErrorType) { ErrorType["VALIDATION"] = "validation"; ErrorType["COMPILATION"] = "compilation"; ErrorType["RUNTIME"] = "runtime"; ErrorType["SYSTEM"] = "system"; ErrorType["DEPLOYMENT"] = "deployment"; ErrorType["UNKNOWN"] = "unknown"; })(ErrorType || (ErrorType = {})); /** * CLI error class */ export class CLIError extends Error { type; code; suggestions; constructor(message, type = ErrorType.UNKNOWN, code, suggestions) { super(message); this.type = type; this.code = code; this.suggestions = suggestions; this.name = 'CLIError'; } } /** * Format an error message with line and column information */ export function formatErrorWithLocation(message, filePath, line, column) { const location = line !== undefined ? `${filePath}:${line}${column !== undefined ? `:${column}` : ''}` : filePath; return `${chalk.red('error')} ${chalk.gray(location)} ${message}`; } /** * Format a compilation error */ export function formatCompilationError(message, filePath, line, column, code, suggestions) { let output = formatErrorWithLocation(message, filePath, line, column); if (code) { output += `\n${chalk.gray(`Error code: ${code}`)}`; } if (suggestions && suggestions.length > 0) { output += '\n\n' + chalk.yellow('Suggestions:'); suggestions.forEach(suggestion => { output += `\n - ${suggestion}`; }); } return output; } /** * Handle and report errors */ export function handleError(error) { if (error instanceof CLIError) { switch (error.type) { case ErrorType.VALIDATION: logger.error(`Validation error: ${error.message}`); break; case ErrorType.COMPILATION: logger.error(`Compilation error: ${error.message}`); break; case ErrorType.RUNTIME: logger.error(`Runtime error: ${error.message}`); break; case ErrorType.SYSTEM: logger.error(`System error: ${error.message}`); break; case ErrorType.DEPLOYMENT: logger.error(`Deployment error: ${error.message}`); break; default: logger.error(`Error: ${error.message}`); } if (error.suggestions && error.suggestions.length > 0) { logger.info('Suggestions:'); error.suggestions.forEach(suggestion => { logger.info(` - ${suggestion}`); }); } } else if (error instanceof Error) { logger.error(`Error: ${error.message}`); if (error.stack && process.env.DEBUG) { logger.debug(error.stack); } } else { logger.error(`Unknown error: ${String(error)}`); } process.exit(1); } //# sourceMappingURL=error.js.map