UNPKG

gitset

Version:

Enhanced git init with user configuration management

162 lines • 6.51 kB
import chalk from 'chalk'; import { ErrorType, GitStartError } from '../types/cli.js'; import { UI_CONFIG, APP_CONFIG } from '../config/constants.js'; import { readFileSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; // Get package.json for bugs URL const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const packageJsonPath = join(__dirname, '../../package.json'); const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); /** * Centralized error handling with consistent messaging and styling */ export class ErrorHandler { /** * Main error handling entry point */ static handle(error) { if (error instanceof GitStartError) { this.handleGitStartError(error); } else if (error instanceof Error) { this.handleGenericError(error); } else { this.handleUnknownError(error); } } /** * Handle application-specific errors */ static handleGitStartError(error) { const errorColor = chalk[UI_CONFIG.colors.error]; console.log(errorColor.bold('\nāŒ GitSet Error\n')); console.log(errorColor(`Error Type: ${error.type}`)); console.log(errorColor(`Message: ${error.message}`)); if (error.suggestion) { console.log(chalk[UI_CONFIG.colors.warning](`\nšŸ’” Suggestion: ${error.suggestion}`)); } this.showRecoverabilityMessage(error.recoverable); this.showErrorHelp(error.type); } /** * Handle generic JavaScript errors */ static handleGenericError(error) { const errorColor = chalk[UI_CONFIG.colors.error]; console.log(errorColor.bold('\nāŒ Unexpected Error\n')); console.log(errorColor(`Message: ${error.message}`)); if (error.stack && process.env.NODE_ENV !== 'production') { console.log(chalk[UI_CONFIG.colors.muted]('\nStack Trace:')); console.log(chalk[UI_CONFIG.colors.muted](error.stack)); } console.log(chalk[UI_CONFIG.colors.warning]('\nšŸ’” If this error persists, please report it as a bug.')); } /** * Handle unknown error types */ static handleUnknownError(error) { const errorColor = chalk[UI_CONFIG.colors.error]; console.log(errorColor.bold('\nāŒ Unknown Error\n')); console.log(errorColor(`An unknown error occurred: ${error}`)); console.log(chalk[UI_CONFIG.colors.warning]('\nšŸ’” Please report this issue with the error details above.')); } /** * Show recoverability message based on error type */ static showRecoverabilityMessage(recoverable) { if (recoverable) { console.log(chalk[UI_CONFIG.colors.muted]('\nšŸ”„ This error may be recoverable. Please try again.')); } else { console.log(chalk[UI_CONFIG.colors.error]('\n🚫 This error is not recoverable. Please check your configuration.')); } } /** * Show contextual help based on error type */ static showErrorHelp(errorType) { const helpMessages = this.getHelpMessages(); const messages = helpMessages[errorType]; if (messages && messages.length > 0) { console.log(chalk[UI_CONFIG.colors.info]('\nšŸ”§ Troubleshooting Steps:')); messages.forEach(message => { console.log(chalk[UI_CONFIG.colors.info](message)); }); } this.showGeneralHelp(); } /** * Get help messages for each error type */ static getHelpMessages() { return { [ErrorType.GIT_NOT_FOUND]: [ '• Ensure Git is installed on your system', `• Try running the command with automatic installation: ${APP_CONFIG.name}`, '• Manual installation: https://git-scm.com/downloads' ], [ErrorType.PERMISSION_DENIED]: [ '• Check directory permissions', '• Try running with appropriate user permissions', '• Consider using a different directory' ], [ErrorType.DIRECTORY_ERROR]: [ '• Verify the directory path exists', '• Check if you have write permissions', '• Try creating the directory manually first' ], [ErrorType.VALIDATION_ERROR]: [ '• Check your email format (user@domain.com)', '• Ensure username is between 2-100 characters', '• Verify directory path is valid' ], [ErrorType.INSTALLATION_FAILED]: [ '• Check your internet connection', '• Verify you have administrator privileges', '• Try a different installation method', '• Consider manual installation' ], [ErrorType.CONFIG_ERROR]: [ '• Check Git configuration syntax', '• Verify user.name and user.email are valid', '• Try resetting Git configuration' ], [ErrorType.NETWORK_ERROR]: [ '• Check your internet connection', '• Verify proxy settings if applicable', '• Try again later' ] }; } /** * Show general help information */ static showGeneralHelp() { console.log(chalk[UI_CONFIG.colors.muted](`\nšŸ“š For more help: ${APP_CONFIG.name} --help`)); console.log(chalk[UI_CONFIG.colors.muted](`šŸ› Report issues: ${packageJson.bugs?.url || 'https://github.com/bytesnack114/gitset/issues'}\n`)); } static createError(type, message, recoverable = true, suggestion) { return new GitStartError(type, message, recoverable, suggestion); } static validateAndThrow(condition, error) { if (!condition) { throw error; } } static async safeExecute(operation, errorType, errorMessage, suggestion) { try { return await operation(); } catch (error) { if (error instanceof GitStartError) { throw error; } const message = error instanceof Error ? error.message : String(error); throw this.createError(errorType, `${errorMessage}: ${message}`, true, suggestion); } } } //# sourceMappingURL=error-handler.js.map