gitlify
Version:
A powerful CLI tool to analyze uncommitted git changes with detailed reports, function detection, and beautiful terminal output
109 lines (100 loc) • 2.87 kB
JavaScript
class GitDiffersError extends Error {
constructor(message, code, details = null) {
super(message);
this.name = 'GitDiffersError';
this.code = code;
this.details = details;
this.timestamp = new Date().toISOString();
}
}
class SecurityError extends GitDiffersError {
constructor(message, details = null) {
super(message, 'SECURITY_ERROR', details);
this.name = 'SecurityError';
}
}
class ValidationError extends GitDiffersError {
constructor(message, details = null) {
super(message, 'VALIDATION_ERROR', details);
this.name = 'ValidationError';
}
}
class GitError extends GitDiffersError {
constructor(message, details = null) {
super(message, 'GIT_ERROR', details);
this.name = 'GitError';
}
}
class FileSystemError extends GitDiffersError {
constructor(message, details = null) {
super(message, 'FILESYSTEM_ERROR', details);
this.name = 'FileSystemError';
}
}
// Error handler utility
class ErrorHandler {
static handle(error, options = {}) {
const isDevelopment = process.env.NODE_ENV === 'development';
if (error instanceof GitDiffersError) {
// Custom error handling
if (isDevelopment) {
console.error(`❌ ${error.name}: ${error.message}`);
if (error.details) {
console.error('Details:', error.details);
}
} else {
// Production: Generic error messages
switch (error.code) {
case 'SECURITY_ERROR':
console.error('❌ Security violation detected');
break;
case 'VALIDATION_ERROR':
console.error('❌ Invalid input provided');
break;
case 'GIT_ERROR':
console.error('❌ Git operation failed');
break;
case 'FILESYSTEM_ERROR':
console.error('❌ File system operation failed');
break;
default:
console.error('❌ An error occurred');
}
}
} else {
// Generic error handling
if (isDevelopment) {
console.error('❌ Error:', error.message);
console.error('Stack:', error.stack);
} else {
console.error('❌ An unexpected error occurred');
}
}
// Log error for debugging
if (options.logger) {
options.logger.error(error.message, {
code: error.code,
details: error.details,
stack: error.stack
});
}
}
static wrapAsync(fn) {
return async (...args) => {
try {
return await fn(...args);
} catch (error) {
this.handle(error);
throw error;
}
};
}
}
module.exports = {
GitDiffersError,
SecurityError,
ValidationError,
GitError,
FileSystemError,
ErrorHandler
};