UNPKG

quallaa-cli

Version:

Sets up core infrastructure services for AI-assisted development

181 lines • 8.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorHandler = void 0; exports.setupGlobalErrorHandler = setupGlobalErrorHandler; exports.handleError = handleError; const chalk_1 = __importDefault(require("chalk")); const custom_errors_1 = require("./custom-errors"); class ErrorHandler { static instance; constructor() { } static getInstance() { if (!ErrorHandler.instance) { ErrorHandler.instance = new ErrorHandler(); } return ErrorHandler.instance; } handleError(error, context) { if (this.isCommanderHelperError(error)) { process.exit(0); } this.logError(error, context); this.displayUserError(error, context); const exitCode = this.getExitCode(error); process.exit(exitCode); } isCommanderHelperError(error) { const commanderErrors = [ '(outputHelp)', '(version)', 'commander.helpDisplayed', 'commander.version' ]; return commanderErrors.some(errType => error.message.includes(errType) || error.code === errType); } logError(error, context) { const logData = { error: error instanceof custom_errors_1.QuallaaError ? error.toJSON() : { name: error.name, message: error.message, stack: error.stack, }, context, timestamp: new Date().toISOString(), }; if (process.env.NODE_ENV === 'development' || process.env.DEBUG) { console.debug('Error details:', JSON.stringify(logData, null, 2)); } } displayUserError(error, context) { console.log(); if (error instanceof custom_errors_1.AuthenticationError) { this.displayAuthError(error, context); } else if (error instanceof custom_errors_1.ServiceError) { this.displayServiceError(error, context); } else if (error instanceof custom_errors_1.ValidationError) { this.displayValidationError(error, context); } else if (error instanceof custom_errors_1.ConfigurationError) { this.displayConfigError(error, context); } else if (error instanceof custom_errors_1.QuallaaError) { this.displayQuallaaError(error, context); } else { this.displayGenericError(error, context); } this.displaySupportInfo(error); } displayAuthError(error, context) { console.log(chalk_1.default.red('šŸ” Authentication Error')); console.log(chalk_1.default.gray('─'.repeat(50))); console.log(chalk_1.default.red(error.message)); console.log(); if (context?.service) { console.log(chalk_1.default.yellow('šŸ’” Try these solutions:')); console.log(chalk_1.default.gray(` 1. Run: ${chalk_1.default.cyan(`quallaa setup ${context.service}`)}`)); console.log(chalk_1.default.gray(' 2. Check your API keys/tokens are correct')); console.log(chalk_1.default.gray(' 3. Verify your account permissions')); } } displayServiceError(error, _context) { console.log(chalk_1.default.red(`🌐 ${error.service} Service Error`)); console.log(chalk_1.default.gray('─'.repeat(50))); console.log(chalk_1.default.red(error.message)); if (error.statusCode) { console.log(chalk_1.default.gray(`Status Code: ${error.statusCode}`)); } console.log(); console.log(chalk_1.default.yellow('šŸ’” Possible solutions:')); console.log(chalk_1.default.gray(' 1. Check your internet connection')); console.log(chalk_1.default.gray(' 2. Verify service is not experiencing downtime')); console.log(chalk_1.default.gray(' 3. Check rate limits for your account')); } displayValidationError(error, _context) { console.log(chalk_1.default.red('āœļø Input Validation Error')); console.log(chalk_1.default.gray('─'.repeat(50))); console.log(chalk_1.default.red(error.message)); if (error.field) { console.log(chalk_1.default.gray(`Field: ${error.field}`)); } console.log(); console.log(chalk_1.default.yellow('šŸ’” Please check your input and try again.')); } displayConfigError(error, _context) { console.log(chalk_1.default.red('āš™ļø Configuration Error')); console.log(chalk_1.default.gray('─'.repeat(50))); console.log(chalk_1.default.red(error.message)); console.log(); console.log(chalk_1.default.yellow('šŸ’” Try these solutions:')); console.log(chalk_1.default.gray(' 1. Run: ' + chalk_1.default.cyan('quallaa init') + ' to reinitialize')); console.log(chalk_1.default.gray(' 2. Check your .env.local file')); console.log(chalk_1.default.gray(' 3. Verify all required services are set up')); } displayQuallaaError(error, _context) { console.log(chalk_1.default.red('āŒ Quallaa CLI Error')); console.log(chalk_1.default.gray('─'.repeat(50))); console.log(chalk_1.default.red(error.message)); console.log(chalk_1.default.gray(`Error Code: ${error.code}`)); console.log(); } displayGenericError(error, _context) { console.log(chalk_1.default.red('šŸ’„ Unexpected Error')); console.log(chalk_1.default.gray('─'.repeat(50))); console.log(chalk_1.default.red(error.message)); console.log(); console.log(chalk_1.default.yellow('This appears to be an unexpected error.')); console.log(chalk_1.default.gray('Please report this issue with the details below.')); } displaySupportInfo(error) { console.log(); console.log(chalk_1.default.gray('─'.repeat(50))); console.log(chalk_1.default.cyan('šŸ†˜ Need help?')); if (error instanceof custom_errors_1.QuallaaError) { console.log(chalk_1.default.gray(`Version: ${error.version}`)); console.log(chalk_1.default.gray(`Error ID: ${error.code}-${error.timestamp.slice(-8)}`)); } console.log(chalk_1.default.gray('Documentation: https://docs.quallaa.com')); console.log(chalk_1.default.gray('Support: https://github.com/quallaa/quallaa-cli/issues')); console.log(chalk_1.default.gray('Email: support@quallaa.com')); console.log(); } getExitCode(error) { if (error instanceof custom_errors_1.AuthenticationError) return 1; if (error instanceof custom_errors_1.ServiceError) return 2; if (error instanceof custom_errors_1.ValidationError) return 3; if (error instanceof custom_errors_1.ConfigurationError) return 4; if (error instanceof custom_errors_1.QuallaaError) return 5; return 1; } } exports.ErrorHandler = ErrorHandler; function setupGlobalErrorHandler() { const errorHandler = ErrorHandler.getInstance(); process.on('uncaughtException', (error) => { console.log(chalk_1.default.red('\nšŸ’„ Uncaught Exception:')); errorHandler.handleError(error, { operation: 'uncaught-exception' }); }); process.on('unhandledRejection', (reason) => { const error = reason instanceof Error ? reason : new Error(`Unhandled promise rejection: ${String(reason)}`); console.log(chalk_1.default.red('\nšŸ’„ Unhandled Promise Rejection:')); errorHandler.handleError(error, { operation: 'unhandled-rejection' }); }); } function handleError(error, context) { const errorHandler = ErrorHandler.getInstance(); return errorHandler.handleError(error, context); } //# sourceMappingURL=error-handler.js.map