@dharshansr/gitgenius
Version:
AI-powered commit message generator with enhanced features
102 lines • 3.79 kB
JavaScript
/**
* Professional error handler with detailed error information
*/
import chalk from 'chalk';
export class GitGeniusError extends Error {
constructor(message, code, category, suggestions = []) {
super(message);
this.code = code;
this.category = category;
this.suggestions = suggestions;
this.name = 'GitGeniusError';
}
}
export class ErrorHandler {
static handle(error) {
if (error instanceof GitGeniusError) {
this.handleGitGeniusError(error);
}
else if (error instanceof Error) {
this.handleGenericError(error);
}
else {
this.handleUnknownError(error);
}
}
static handleGitGeniusError(error) {
console.error(chalk.red(`[${error.category.toUpperCase()}] ${error.message}`));
if (error.suggestions.length > 0) {
console.log(chalk.yellow('\n💡 Suggestions:'));
error.suggestions.forEach(suggestion => {
console.log(chalk.yellow(` • ${suggestion}`));
});
}
this.showCommonSolutions(error.category);
process.exit(1);
}
static handleGenericError(error) {
console.error(chalk.red(`[ERROR] ${error.message}`));
if (process.env.DEBUG) {
console.error(chalk.gray(error.stack));
}
process.exit(1);
}
static handleUnknownError(error) {
console.error(chalk.red('[ERROR] An unexpected error occurred'));
console.error(chalk.gray(String(error)));
process.exit(1);
}
static showCommonSolutions(category) {
const solutions = {
git: [
'Ensure you are in a git repository',
'Check if you have staged changes: git status',
'Verify git is properly configured'
],
ai: [
'Check your API key configuration: gitgenius config',
'Verify your internet connection',
'Try a different AI provider: gitgenius config provider'
],
config: [
'Reset configuration: gitgenius config --reset',
'Check file permissions in ~/.config/gitgenius/',
'Reconfigure your settings: gitgenius config'
],
network: [
'Check your internet connection',
'Verify firewall settings',
'Try again in a few moments'
],
user: [
'Check the command syntax: gitgenius --help',
'Refer to documentation: https://docs.gitgenius.dev',
'Join our community: https://discord.gg/gitgenius'
]
};
const categorySolutions = solutions[category];
if (categorySolutions) {
console.log(chalk.blue('\n🔧 Common solutions:'));
categorySolutions.forEach(solution => {
console.log(chalk.blue(` • ${solution}`));
});
}
}
// Specific error creators
static gitError(message, suggestions = []) {
return new GitGeniusError(message, 'GIT_ERROR', 'git', suggestions);
}
static aiError(message, suggestions = []) {
return new GitGeniusError(message, 'AI_ERROR', 'ai', suggestions);
}
static configError(message, suggestions = []) {
return new GitGeniusError(message, 'CONFIG_ERROR', 'config', suggestions);
}
static networkError(message, suggestions = []) {
return new GitGeniusError(message, 'NETWORK_ERROR', 'network', suggestions);
}
static userError(message, suggestions = []) {
return new GitGeniusError(message, 'USER_ERROR', 'user', suggestions);
}
}
//# sourceMappingURL=ErrorHandler.js.map