Files
ccs/bin/utils/error-manager.js
T
Kai (Tam Nhu) TranandGitHub f40e9647ec feat(cli): comprehensive UX improvements for v3.5.0 (#7)
Implements 6-phase CLI UX improvement plan with comprehensive error handling, 
interactive features, and cross-platform consistency.

### Added
- Shell auto-completion (bash, zsh, PowerShell, Fish)
- Error codes (E101-E901) with documentation URLs
- Fuzzy matching "Did you mean?" suggestions (Levenshtein distance)
- Progress indicators (doctor [n/9] counter, GLMT proxy spinner)
- Interactive confirmation prompts with --yes/-y automation flag
- JSON output format (--json) for auth commands
- Impact display (session count, paths) before destructive operations
- Comprehensive test suite (15 tests, 100% pass rate)
- Complete error documentation in docs/errors/
- Cross-platform `--shell-completion` command

### Changed
- Error boxes: Unicode (╔═╗) → ASCII (===) for compatibility
- JSON output uses CCS version instead of schema version
- Help text includes EXAMPLES section across platforms
- Test suite properly counts test cases (not assertions)

### Fixed
- --yes flag bug (returned false instead of true)
- Help text consistency (added Uninstall section to bash)
- Test pass rate calculation (excludes skipped tests)
- Help section comparison (locale-specific sort)

### Testing
- 13/13 tests passing (2 legitimately skipped)
- Cross-platform verified (Node.js, bash, PowerShell)
- All error codes documented and tested
2025-11-15 01:26:50 -05:00

165 lines
5.7 KiB
JavaScript

'use strict';
const { colored } = require('./helpers');
const { ERROR_CODES, getErrorDocUrl } = require('./error-codes');
/**
* Error types with structured messages (Legacy - kept for compatibility)
*/
const ErrorTypes = {
NO_CLAUDE_CLI: 'NO_CLAUDE_CLI',
MISSING_SETTINGS: 'MISSING_SETTINGS',
INVALID_CONFIG: 'INVALID_CONFIG',
UNKNOWN_PROFILE: 'UNKNOWN_PROFILE',
PERMISSION_DENIED: 'PERMISSION_DENIED',
GENERIC: 'GENERIC'
};
/**
* Enhanced error manager with context-aware messages
*/
class ErrorManager {
/**
* Show error code and documentation URL
* @param {string} errorCode - Error code (e.g., E301)
*/
static showErrorCode(errorCode) {
console.error(colored(`Error: ${errorCode}`, 'yellow'));
console.error(colored(getErrorDocUrl(errorCode), 'yellow'));
console.error('');
}
/**
* Show Claude CLI not found error
*/
static showClaudeNotFound() {
console.error('');
console.error(colored('[X] Claude CLI not found', 'red'));
console.error('');
console.error('CCS requires Claude CLI to be installed and available in PATH.');
console.error('');
console.error(colored('Solutions:', 'yellow'));
console.error(' 1. Install Claude CLI:');
console.error(' https://docs.claude.com/en/docs/claude-code/installation');
console.error('');
console.error(' 2. Verify installation:');
console.error(' command -v claude (Unix)');
console.error(' Get-Command claude (Windows)');
console.error('');
console.error(' 3. Custom path (if installed elsewhere):');
console.error(' export CCS_CLAUDE_PATH="/path/to/claude"');
console.error('');
this.showErrorCode(ERROR_CODES.CLAUDE_NOT_FOUND);
}
/**
* Show settings file not found error
* @param {string} settingsPath - Path to missing settings file
*/
static showSettingsNotFound(settingsPath) {
const isClaudeSettings = settingsPath.includes('.claude') && settingsPath.endsWith('settings.json');
console.error('');
console.error(colored('[X] Settings file not found', 'red'));
console.error('');
console.error(`File: ${settingsPath}`);
console.error('');
if (isClaudeSettings) {
console.error('This file is auto-created when you login to Claude CLI.');
console.error('');
console.error(colored('Solutions:', 'yellow'));
console.error(` echo '{}' > ${settingsPath}`);
console.error(' claude /login');
console.error('');
console.error('Why: Newer Claude CLI versions require explicit login.');
} else {
console.error(colored('Solutions:', 'yellow'));
console.error(' npm install -g @kaitranntt/ccs --force');
console.error('');
console.error('This will recreate missing profile settings.');
}
console.error('');
this.showErrorCode(ERROR_CODES.CONFIG_INVALID_PROFILE);
}
/**
* Show invalid configuration error
* @param {string} configPath - Path to invalid config
* @param {string} errorDetail - JSON parse error detail
*/
static showInvalidConfig(configPath, errorDetail) {
console.error('');
console.error(colored('[X] Configuration invalid', 'red'));
console.error('');
console.error(`File: ${configPath}`);
console.error(`Issue: ${errorDetail}`);
console.error('');
console.error(colored('Solutions:', 'yellow'));
console.error(' # Backup corrupted file');
console.error(` mv ${configPath} ${configPath}.backup`);
console.error('');
console.error(' # Reinstall CCS');
console.error(' npm install -g @kaitranntt/ccs --force');
console.error('');
console.error('Your profile settings will be preserved.');
console.error('');
this.showErrorCode(ERROR_CODES.CONFIG_INVALID_JSON);
}
/**
* Show profile not found error
* @param {string} profileName - Requested profile name
* @param {string[]} availableProfiles - List of available profiles
* @param {string[]} suggestions - Suggested profile names (fuzzy match)
*/
static showProfileNotFound(profileName, availableProfiles, suggestions = []) {
console.error('');
console.error(colored(`[X] Profile '${profileName}' not found`, 'red'));
console.error('');
if (suggestions && suggestions.length > 0) {
console.error(colored('Did you mean:', 'yellow'));
suggestions.forEach(s => console.error(` ${s}`));
console.error('');
}
console.error(colored('Available profiles:', 'cyan'));
availableProfiles.forEach(line => console.error(` ${line}`));
console.error('');
console.error(colored('Solutions:', 'yellow'));
console.error(' # Use existing profile');
console.error(' ccs <profile> "your prompt"');
console.error('');
console.error(' # Create new account profile');
console.error(' ccs auth create <name>');
console.error('');
this.showErrorCode(ERROR_CODES.PROFILE_NOT_FOUND);
}
/**
* Show permission denied error
* @param {string} path - Path with permission issue
*/
static showPermissionDenied(path) {
console.error('');
console.error(colored('[X] Permission denied', 'red'));
console.error('');
console.error(`Cannot write to: ${path}`);
console.error('');
console.error(colored('Solutions:', 'yellow'));
console.error(' # Fix ownership');
console.error(' sudo chown -R $USER ~/.ccs ~/.claude');
console.error('');
console.error(' # Fix permissions');
console.error(' chmod 755 ~/.ccs ~/.claude');
console.error('');
console.error(' # Retry installation');
console.error(' npm install -g @kaitranntt/ccs --force');
console.error('');
this.showErrorCode(ERROR_CODES.FS_CANNOT_WRITE_FILE);
}
}
module.exports = { ErrorManager, ErrorTypes };