quallaa-cli
Version:
Sets up core infrastructure services for AI-assisted development
181 lines ⢠8.03 kB
JavaScript
;
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