@stoar/cli
Version:
CLI tool for STOAR - decentralized file storage on Arweave
59 lines • 1.94 kB
JavaScript
import chalk from 'chalk';
export class CLIError extends Error {
code;
constructor(message, code) {
super(message);
this.code = code;
this.name = 'CLIError';
}
}
export function handleError(error, options) {
const verbose = options?.verbose;
if (error instanceof CLIError) {
console.error(chalk.red('Error:'), error.message);
if (error.code) {
console.error(chalk.gray(`Code: ${error.code}`));
}
}
else if (error.code === 'INSUFFICIENT_BALANCE') {
console.error(chalk.red('Error: Not enough AR tokens'));
if (error.required && error.current) {
console.error(chalk.gray(`Current balance: ${error.current}`));
console.error(chalk.gray(`Required: ${error.required}`));
}
}
else if (error.code === 'ENOENT') {
console.error(chalk.red('Error:'), `File not found: ${error.path}`);
}
else if (error.code === 'EACCES') {
console.error(chalk.red('Error:'), `Permission denied: ${error.path}`);
}
else if (error.response?.status === 404) {
console.error(chalk.red('Error:'), 'Resource not found');
}
else if (error.response?.status === 400) {
console.error(chalk.red('Error:'), 'Invalid request');
if (error.response?.data?.error) {
console.error(chalk.gray(error.response.data.error));
}
}
else {
console.error(chalk.red('Error:'), error.message || 'An unknown error occurred');
}
if (verbose && error.stack) {
console.error(chalk.gray('\nStack trace:'));
console.error(chalk.gray(error.stack));
}
process.exit(1);
}
export function wrapCommand(fn) {
return async (...args) => {
try {
await fn(...args);
}
catch (error) {
handleError(error, args[args.length - 2]);
}
};
}
//# sourceMappingURL=error.js.map