UNPKG

doclyft

Version:

CLI for DocLyft - Interactive documentation generator with hosted documentation support

132 lines (131 loc) 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MissingUserInfoError = exports.InvalidTokenError = exports.SessionExpiredError = exports.GitHubTokenError = exports.ValidationError = exports.AuthenticationError = exports.NetworkError = exports.CLIError = void 0; exports.handleCLIError = handleCLIError; class CLIError extends Error { constructor(message, code = 'CLI_ERROR', statusCode = 1, suggestions) { super(message); this.code = code; this.statusCode = statusCode; this.suggestions = suggestions; this.name = 'CLIError'; } } exports.CLIError = CLIError; class NetworkError extends CLIError { constructor(message, originalError) { super(message, 'NETWORK_ERROR', 1, [ 'Check your internet connection', 'Verify the API endpoint is accessible', 'Try again in a few moments' ]); this.originalError = originalError; } } exports.NetworkError = NetworkError; class AuthenticationError extends CLIError { constructor(message) { super(message, 'AUTH_ERROR', 1, [ 'Run `doclyft login` to authenticate', 'Check if your API key is valid', 'Verify your account has the necessary permissions' ]); } } exports.AuthenticationError = AuthenticationError; class ValidationError extends CLIError { constructor(message, field) { super(message, 'VALIDATION_ERROR', 1, [ 'Check the command syntax and try again', 'Use --help to see available options' ]); this.field = field; } } exports.ValidationError = ValidationError; class GitHubTokenError extends CLIError { constructor(message) { super(message, 'GITHUB_TOKEN_ERROR', 1, [ 'Generate a new GitHub personal access token', 'Ensure the token has the required permissions (repo scope)', 'Check if the token has expired' ]); } } exports.GitHubTokenError = GitHubTokenError; class SessionExpiredError extends AuthenticationError { constructor(message = 'Session has expired') { super(message); this.code = 'SESSION_EXPIRED'; this.suggestions = [ 'Run `doclyft login` to re-authenticate', 'Check if your account is still active', 'Verify your internet connection' ]; } } exports.SessionExpiredError = SessionExpiredError; class InvalidTokenError extends AuthenticationError { constructor(message = 'Authentication token is invalid') { super(message); this.code = 'INVALID_TOKEN'; this.suggestions = [ 'Run `doclyft login` with a valid API key', 'Generate a new API key from the DocLyft dashboard', 'Check if the token was copied correctly' ]; } } exports.InvalidTokenError = InvalidTokenError; class MissingUserInfoError extends AuthenticationError { constructor(message = 'User information not found') { super(message); this.code = 'MISSING_USER_INFO'; this.suggestions = [ 'Run `doclyft login` to restore user information', 'Check if your account settings are correct', 'Contact support if the issue persists' ]; } } exports.MissingUserInfoError = MissingUserInfoError; function handleCLIError(error) { if (error instanceof CLIError) { console.error(`\n❌ ${error.message}`); if (error.suggestions) { console.error('\n💡 Suggestions:'); error.suggestions.forEach(suggestion => { console.error(` • ${suggestion}`); }); } process.exit(error.statusCode); } if (error instanceof Error) { if (error.message.includes('ENOTFOUND') || error.message.includes('ECONNREFUSED')) { throw new NetworkError('Network connection failed. Please check your internet connection.'); } if (error.message.includes('401') || error.message.includes('Unauthorized')) { throw new AuthenticationError('Authentication failed. Please check your credentials.'); } if (error.message.includes('403') || error.message.includes('Forbidden')) { throw new AuthenticationError('Access denied. Your API key may not have sufficient permissions.'); } if (error.message.includes('404') || error.message.includes('Not Found')) { throw new CLIError('Resource not found. Please check the repository name or try analyzing it first.'); } if (error.message.includes('429') || error.message.includes('Rate limit')) { throw new CLIError('Rate limit exceeded. Please wait a moment and try again.'); } // Generic error console.error(`\n❌ An unexpected error occurred: ${error.message}`); console.error('\n💡 If this persists, please report this issue with the following details:'); console.error(` Error: ${error.name}: ${error.message}`); if (error.stack) { console.error(` Stack: ${error.stack.split('\n').slice(0, 3).join('\n')}`); } process.exit(1); } // Unknown error type console.error('\n❌ An unexpected error occurred:', error); process.exit(1); }