Skip to content

Commit

Permalink
feat: don't print stack traces for cli errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jul 12, 2022
1 parent 36aaf9b commit e62be74
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 47 deletions.
8 changes: 4 additions & 4 deletions src/actions/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
*/

import { generateKeyPair, AE_AMOUNT_FORMATS } from '@aeternity/aepp-sdk';

import CliError from '../utils/CliError';
import { writeWallet } from '../utils/account';
import { initSdkByWalletFile, getAccountByWalletFile } from '../utils/cli';
import { print, printTransaction, printUnderscored } from '../utils/print';
import { readFile } from '../utils/helpers';
import { decode, readFile } from '../utils/helpers';
import { PROMPT_TYPE, prompt } from '../utils/prompt';

// ## `Sign message` function
Expand Down Expand Up @@ -74,7 +74,7 @@ export async function verifyMessage(walletPath, hexSignature, data = [], options
export async function sign(walletPath, tx, options) {
const { json } = options;
// Validate `tx` hash
if (tx.slice(0, 2) !== 'tx') { throw new Error('Invalid transaction hash'); }
decode(tx, 'tx');

const { account } = await getAccountByWalletFile(walletPath, options);

Expand Down Expand Up @@ -232,7 +232,7 @@ export async function createSecureWalletByPrivKey(walletPath, secretKey, {
// This function allow you to generate `keypair` from `private-key` and write it to secure `ethereum` like key-file
export async function generateKeyPairs(count = 1, { forcePrompt, json }) {
if (!Number.isInteger(+count)) {
throw new Error('Count must be an Number');
throw new CliError('Count must be an Number');
}
if (forcePrompt || await prompt(PROMPT_TYPE.confirm, { message: 'Are you sure you want print your secret key?' })) {
const accounts = Array.from(Array(parseInt(count))).map(() => generateKeyPair(false));
Expand Down
23 changes: 12 additions & 11 deletions src/actions/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { isAddressValid, getDefaultPointerKey } from '@aeternity/aepp-sdk';
import { initSdk, initSdkByWalletFile } from '../utils/cli';
import { print, printName, printTransaction } from '../utils/print';
import { isAvailable, updateNameStatus, validateName } from '../utils/helpers';
import CliError from '../utils/CliError';

// ## Claim `name` function
export async function preClaim(walletPath, domain, options) {
Expand All @@ -37,7 +38,7 @@ export async function preClaim(walletPath, domain, options) {
// Check if that `name' available
const name = await updateNameStatus(domain, sdk);
if (!isAvailable(name)) {
throw new Error('Domain not available');
throw new CliError('Domain not available');
}
// Create `pre-claim` transaction
const preClaimTx = await sdk.aensPreclaim(domain, {
Expand Down Expand Up @@ -66,7 +67,7 @@ export async function claim(walletPath, domain, salt, options) {
// Check if that `name' available
const name = await updateNameStatus(domain, sdk);
if (!isAvailable(name)) {
throw new Error('Domain not available');
throw new CliError('Domain not available');
}

// Wait for next block and create `claimName` transaction
Expand All @@ -91,15 +92,15 @@ export async function updateName(walletPath, domain, addresses, options) {

// Validate `address`
const invalidAddresses = addresses.filter((address) => !isAddressValid(address));
if (invalidAddresses.length) throw new Error(`Addresses "[${invalidAddresses}]" is not valid`);
if (invalidAddresses.length) throw new CliError(`Addresses "[${invalidAddresses}]" is not valid`);
// Validate `name`
validateName(domain);
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name` is unavailable and we can update it
const name = await updateNameStatus(domain, sdk);
if (isAvailable(name)) {
throw new Error(`Domain is ${name.status} and cannot be updated`);
throw new CliError(`Domain is ${name.status} and cannot be updated`);
}

// Create `updateName` transaction
Expand Down Expand Up @@ -133,7 +134,7 @@ export async function extendName(walletPath, domain, nameTtl, options) {
// Check if that `name` is unavailable and we can update it
const name = await updateNameStatus(domain, sdk);
if (isAvailable(name)) {
throw new Error(`Domain is ${name.status} and cannot be extended`);
throw new CliError(`Domain is ${name.status} and cannot be extended`);
}

// Create `updateName` transaction
Expand All @@ -157,15 +158,15 @@ export async function transferName(walletPath, domain, address, options) {
} = options;

// Validate `address`
if (!isAddressValid(address)) throw new Error(`Address "${address}" is not valid`);
if (!isAddressValid(address)) throw new CliError(`Address "${address}" is not valid`);
// Validate `name`
validateName(domain);
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name` is unavailable and we can transfer it
const name = await updateNameStatus(domain, sdk);
if (isAvailable(name)) {
throw new Error('Domain is available, nothing to transfer');
throw new CliError('Domain is available, nothing to transfer');
}

// Create `transferName` transaction
Expand Down Expand Up @@ -195,7 +196,7 @@ export async function revokeName(walletPath, domain, options) {
// Check if `name` is unavailable and we can revoke it
const name = await updateNameStatus(domain, sdk);
if (isAvailable(name)) {
throw new Error('Domain is available, nothing to revoke');
throw new CliError('Domain is available, nothing to revoke');
}

// Create `revokeName` transaction
Expand Down Expand Up @@ -224,7 +225,7 @@ export async function nameBid(walletPath, domain, nameFee, options) {
// Check if that `name' available
const name = await updateNameStatus(domain, sdk);
if (!isAvailable(name)) {
throw new Error('Auction do not start or already end');
throw new CliError('Auction do not start or already end');
}

// Wait for next block and create `claimName` transaction
Expand All @@ -246,14 +247,14 @@ export async function fullClaim(walletPath, domain, options) {
ttl, fee, nonce, nameFee, json, nameTtl, clientTtl,
} = options;
validateName(domain);
if (domain.split('.')[0] < 13) throw new Error('Full name claiming works only with name longer then 12 symbol (not trigger auction)');
if (domain.split('.')[0] < 13) throw new CliError('Full name claiming works only with name longer then 12 symbol (not trigger auction)');

const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name' available
const name = await updateNameStatus(domain, sdk);
if (!isAvailable(name)) {
throw new Error('Domain not available');
throw new CliError('Domain not available');
}

// Wait for next block and create `claimName` transaction
Expand Down
3 changes: 2 additions & 1 deletion src/actions/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
printBlock, print, printUnderscored, printTransaction, printValidation,
} from '../utils/print';
import { getBlock } from '../utils/helpers';
import CliError from '../utils/CliError';

// ## Retrieve `node` version
export async function version(options) {
Expand Down Expand Up @@ -119,7 +120,7 @@ export async function play(options) {
const topHeader = await sdk.api.getTopHeader();

if (height && height > parseInt(topHeader.height)) {
throw new Error('Height is bigger then height of top block');
throw new CliError('Height is bigger then height of top block');
}

printBlock(topHeader, json);
Expand Down
3 changes: 2 additions & 1 deletion src/actions/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import {
checkPref, getBlock, updateNameStatus, validateName,
} from '../utils/helpers';
import CliError from '../utils/CliError';

// ## Inspect helper function's
async function getBlockByHash(hash, options) {
Expand Down Expand Up @@ -157,7 +158,7 @@ async function getOracle(oracleId, options) {
// ## Inspect function
// That function get the param(`hash`, `height` or `name`) and show you info about it
export default async function inspect(hash, option) {
if (!hash) throw new Error('Hash required');
if (!hash) throw new CliError('Hash required');

// Get `block` by `height`
if (!isNaN(hash)) {
Expand Down
3 changes: 2 additions & 1 deletion src/actions/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { decode } from '../utils/helpers';
import {
print, printOracle, printQueries, printTransaction,
} from '../utils/print';
import CliError from '../utils/CliError';

// ## Create Oracle
export async function createOracle(walletPath, queryFormat, responseFormat, options) {
Expand Down Expand Up @@ -56,7 +57,7 @@ export async function extendOracle(walletPath, oracleId, oracleTtl, options) {
ttl, fee, nonce, waitMined, json,
} = options;

if (isNaN(+oracleTtl)) throw new Error('Oracle Ttl should be a number');
if (isNaN(+oracleTtl)) throw new CliError('Oracle Ttl should be a number');
decode(oracleId, 'ok');
const sdk = await initSdkByWalletFile(walletPath, options);
const oracle = await sdk.getOracleObject(oracleId);
Expand Down
5 changes: 3 additions & 2 deletions src/aecli-account.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/*
* ISC License (ISC)
* Copyright (c) 2021 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,5 +16,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import program from './commands/account';
import { runProgram } from './utils/CliError';

program.parseAsync();
await runProgram(program);
5 changes: 3 additions & 2 deletions src/aecli-chain.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/*
* ISC License (ISC)
* Copyright (c) 2021 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,5 +16,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import program from './commands/chain';
import { runProgram } from './utils/CliError';

program.parseAsync();
await runProgram(program);
5 changes: 3 additions & 2 deletions src/aecli-contract.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/*
* ISC License (ISC)
* Copyright (c) 2021 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,5 +16,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import program from './commands/contract';
import { runProgram } from './utils/CliError';

program.parseAsync();
await runProgram(program);
5 changes: 3 additions & 2 deletions src/aecli-crypto.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/*
* ISC License (ISC)
* Copyright (c) 2021 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,5 +16,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import program from './commands/crypto';
import { runProgram } from './utils/CliError';

program.parseAsync();
await runProgram(program);
5 changes: 3 additions & 2 deletions src/aecli-inspect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/*
* ISC License (ISC)
* Copyright (c) 2021 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,5 +16,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import program from './commands/inspect';
import { runProgram } from './utils/CliError';

program.parseAsync();
await runProgram(program);
5 changes: 3 additions & 2 deletions src/aecli-name.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/*
* ISC License (ISC)
* Copyright (c) 2021 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,5 +16,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import program from './commands/name';
import { runProgram } from './utils/CliError';

program.parseAsync();
await runProgram(program);
5 changes: 3 additions & 2 deletions src/aecli-oracle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/*
* ISC License (ISC)
* Copyright (c) 2021 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,5 +16,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import program from './commands/oracle';
import { runProgram } from './utils/CliError';

program.parseAsync();
await runProgram(program);
5 changes: 3 additions & 2 deletions src/aecli-tx.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
/*
* ISC License (ISC)
* Copyright (c) 2021 aeternity developers
* Copyright (c) 2022 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,5 +16,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import program from './commands/tx';
import { runProgram } from './utils/CliError';

program.parseAsync();
await runProgram(program);
3 changes: 2 additions & 1 deletion src/aecli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
import program from './commands/main';
import { runProgram } from './utils/CliError';

program.parseAsync();
await runProgram(program);
3 changes: 2 additions & 1 deletion src/commands/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import { Argument, Option, Command } from 'commander';
import { TX_TTL, MIN_GAS_PRICE } from '@aeternity/aepp-sdk';
import { COMPILER_URL } from '../utils/constant';
import { getCmdFromArguments } from '../utils/cli';
import CliError from '../utils/CliError';
import * as Contract from '../actions/contract';
import { nodeOption, jsonOption, gasOption } from '../arguments';

const callArgs = new Argument('[args]', 'JSON-encoded arguments array of contract call')
.argParser((argsText) => {
const args = JSON.parse(argsText);
if (!Array.isArray(args)) throw new Error(`Call arguments should be an array, got ${argsText} instead`);
if (!Array.isArray(args)) throw new CliError(`Call arguments should be an array, got ${argsText} instead`);
return args;
})
.default([]);
Expand Down
3 changes: 2 additions & 1 deletion src/commands/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
decryptKey, sign, buildTx, unpackTx, decode, TX_TYPE,
} from '@aeternity/aepp-sdk';
import { print } from '../utils/print';
import CliError from '../utils/CliError';

const program = new Command().name('aecli crypto');

Expand All @@ -47,7 +48,7 @@ program
const binaryKey = (() => {
if (file) return fs.readFileSync(file);
if (privKey) return Buffer.from(privKey, 'hex');
throw new Error('Must provide either [privkey] or [file]');
throw new CliError('Must provide either [privkey] or [file]');
})();
const decryptedKey = password ? decryptKey(password, binaryKey) : binaryKey;
const encodedTx = decode(tx, 'tx');
Expand Down
15 changes: 15 additions & 0 deletions src/utils/CliError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default class CliError extends Error {
constructor(message) {
super(message);
this.name = 'CliError';
}
}

export async function runProgram(program) {
try {
await program.parseAsync();
} catch (error) {
if (error instanceof CliError) program.error(error.message);
throw error;
}
}
3 changes: 2 additions & 1 deletion src/utils/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import {
} from '@aeternity/aepp-sdk';
import { readJSONFile } from './helpers';
import { PROMPT_TYPE, prompt } from './prompt';
import CliError from './CliError';

export async function writeWallet(name, secretKey, output, password, overwrite) {
const walletPath = path.resolve(process.cwd(), path.join(output, name));
if (!overwrite && fs.existsSync(walletPath) && !await prompt(PROMPT_TYPE.askOverwrite)) {
throw new Error(`Wallet already exist at ${walletPath}`);
throw new CliError(`Wallet already exist at ${walletPath}`);
}
password ||= await prompt(PROMPT_TYPE.askPassword);
fs.writeFileSync(walletPath, JSON.stringify(await dump(name, password, secretKey)));
Expand Down

0 comments on commit e62be74

Please sign in to comment.