UNPKG

@pod-protocol/cli

Version:

Command-line interface for PoD Protocol (Prompt or Die) AI Agent Communication Protocol

120 lines (119 loc) 4.42 kB
import chalk from "chalk"; /** * Error codes for different types of CLI errors */ export var ErrorCode; (function (ErrorCode) { ErrorCode[ErrorCode["VALIDATION_ERROR"] = 1] = "VALIDATION_ERROR"; ErrorCode[ErrorCode["NETWORK_ERROR"] = 2] = "NETWORK_ERROR"; ErrorCode[ErrorCode["WALLET_ERROR"] = 3] = "WALLET_ERROR"; ErrorCode[ErrorCode["SDK_ERROR"] = 4] = "SDK_ERROR"; ErrorCode[ErrorCode["FILE_ERROR"] = 5] = "FILE_ERROR"; ErrorCode[ErrorCode["CONFIG_ERROR"] = 6] = "CONFIG_ERROR"; ErrorCode[ErrorCode["UNKNOWN_ERROR"] = 99] = "UNKNOWN_ERROR"; })(ErrorCode || (ErrorCode = {})); /** * Standard CLI error class with error codes */ export class CliError extends Error { constructor(message, code = ErrorCode.UNKNOWN_ERROR, cause) { super(message); this.code = code; this.cause = cause; this.name = "CliError"; } } /** * Error handler for CLI operations */ export class ErrorHandler { /** * Handle errors with spinner and user-friendly messages */ static handleError(error, spinner, context) { const errorMessage = this.getErrorMessage(error, context); const suggestions = this.getErrorSuggestions(error); if (spinner) { spinner.fail(errorMessage); } else { console.error(chalk.red("Error:"), errorMessage); } if (suggestions.length > 0) { console.log(chalk.yellow("\nSuggestions:")); suggestions.forEach((suggestion) => { console.log(chalk.yellow(`• ${suggestion}`)); }); } const exitCode = error instanceof CliError ? error.code : ErrorCode.UNKNOWN_ERROR; process.exit(exitCode); } /** * Get user-friendly error message */ static getErrorMessage(error, context) { const baseMessage = context ? `Failed to ${context}` : "Operation failed"; if (error.message.includes("Non-base58 character")) { return "Invalid address format - must be a valid Solana public key"; } if (error.message.includes("Network request failed")) { return "Network connection failed - check your internet connection"; } if (error.message.includes("Insufficient funds")) { return "Insufficient SOL balance - add funds to your wallet"; } if (error.message.includes("not yet implemented")) { return "This feature is not yet implemented"; } return `${baseMessage}: ${error.message}`; } /** * Get helpful suggestions based on error type */ static getErrorSuggestions(error) { const suggestions = []; if (error.message.includes("Non-base58")) { suggestions.push("Use 'pod agent list' to see valid agent addresses"); suggestions.push("Ensure the address is 32-44 characters long"); } if (error.message.includes("Network")) { suggestions.push("Check your internet connection"); suggestions.push("Try switching networks with 'pod config set-network <network>'"); } if (error.message.includes("Insufficient funds")) { suggestions.push("Get devnet SOL with 'pod config airdrop'"); suggestions.push("Check your balance with 'pod config show'"); } if (error.message.includes("Keypair")) { suggestions.push("Generate a new keypair with 'pod config generate-keypair'"); suggestions.push("Check your keypair path with 'pod config show'"); } return suggestions; } /** * Create specific error types */ static validationError(message) { return new CliError(message, ErrorCode.VALIDATION_ERROR); } static networkError(message, cause) { return new CliError(message, ErrorCode.NETWORK_ERROR, cause); } static walletError(message, cause) { return new CliError(message, ErrorCode.WALLET_ERROR, cause); } static configError(message, cause) { return new CliError(message, ErrorCode.CONFIG_ERROR, cause); } } /** * Utility for safely executing async operations with error handling */ export async function safeExecute(operation, errorContext, spinner) { try { return await operation(); } catch (error) { ErrorHandler.handleError(error, spinner, errorContext); } }