UNPKG

npmplus-mcp-server

Version:

Production-ready MCP server for intelligent JavaScript package management. Works with Claude, Windsurf, Cursor, VS Code, and any MCP-compatible AI editor.

127 lines 4.57 kB
"use strict"; /** * Common error handling utilities */ Object.defineProperty(exports, "__esModule", { value: true }); exports.PackageManagerError = void 0; exports.withErrorHandling = withErrorHandling; exports.extractErrorMessage = extractErrorMessage; exports.getErrorSuggestion = getErrorSuggestion; exports.formatError = formatError; exports.safeExecute = safeExecute; /** * Enhanced error class with context information */ class PackageManagerError extends Error { operation; context; originalError; constructor(message, operation, context, originalError) { super(message); this.name = 'PackageManagerError'; this.operation = operation; this.context = context; this.originalError = originalError; } } exports.PackageManagerError = PackageManagerError; /** * Wraps async operations with consistent error handling */ async function withErrorHandling(operation, context) { try { return await operation(); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new PackageManagerError(`Failed to ${context.operation}: ${errorMessage}`, context.operation, context, error instanceof Error ? error : undefined); } } /** * Extracts meaningful error messages from various error types */ function extractErrorMessage(error) { if (error instanceof PackageManagerError) { return error.message; } if (error instanceof Error) { // Handle specific npm/yarn error patterns if (error.message.includes('ENOTFOUND')) { return 'Network error: Unable to connect to npm registry'; } if (error.message.includes('EACCES')) { return 'Permission denied: Try running with sudo or check file permissions'; } if (error.message.includes('ENOENT')) { return 'File or directory not found'; } if (error.message.includes('Invalid package name')) { return 'Invalid package name format'; } return error.message; } return String(error); } /** * Determines if an error is recoverable and suggests fixes */ function getErrorSuggestion(error) { // Check original error message first before extractErrorMessage transforms it const originalMessage = error instanceof Error ? error.message : String(error); const extractedMessage = extractErrorMessage(error); if (originalMessage.includes('ENOTFOUND') || extractedMessage.includes('Unable to connect to npm registry')) { return 'Check your internet connection and npm registry configuration'; } if (originalMessage.includes('EACCES') || extractedMessage.includes('Permission denied')) { return 'Try running the command with appropriate permissions or use a package manager like nvm'; } if (originalMessage.includes('Invalid package name') || extractedMessage.includes('Invalid package name')) { return 'Ensure package name follows npm naming conventions (lowercase, no spaces)'; } // Check for version-specific errors first (more specific) if (extractedMessage.includes('version')) { return 'Check if the specified version exists and use valid semver format'; } // Check for general not found errors after version checks if (extractedMessage.includes('404') || extractedMessage.includes('not found')) { return 'Verify the package name is correct and exists in the npm registry'; } return null; } /** * Formats error with context and suggestions */ function formatError(error, context) { const message = extractErrorMessage(error); const suggestion = getErrorSuggestion(error); let output = message; if (context) { const contextParts = []; if (context.package) contextParts.push(`Package: ${context.package}`); if (context.cwd) contextParts.push(`Directory: ${context.cwd}`); if (context.command) contextParts.push(`Command: ${context.command}`); if (contextParts.length > 0) { output += `\n\nContext:\n${contextParts.join('\n')}`; } } if (suggestion) { output += `\n\n💡 Suggestion: ${suggestion}`; } return output; } /** * Safely execute a function with error recovery */ async function safeExecute(operation, fallback, onError) { try { return await operation(); } catch (error) { onError?.(error); return fallback; } } //# sourceMappingURL=error-handling.js.map