Skip to content

Commit 8c23676

Browse files
author
Zach Alam
committed
graceful cli error handling
1 parent 8f98a4e commit 8c23676

3 files changed

Lines changed: 45 additions & 21 deletions

File tree

cli/commands/stamp.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,40 @@ spinner.setSpinnerString(19);
77

88
module.exports = {
99
prompt: async (env) => {
10-
1110
helpers.killForNoConf();
12-
if (!env.file && !env.text) helpers.errorExit("Must supply either -f <file> or -t <text> param.");
13-
if (env.file && env.text) helpers.errorExit("Cannot supply both -f <file> and -t <text> params.");
11+
if (!env.file && !env.text)
12+
helpers.errorExit("Must supply either -f <file> or -t <text> param.");
13+
if (env.file && env.text)
14+
helpers.errorExit("Cannot supply both -f <file> and -t <text> params.");
1415
const bitfact = new BitFact(helpers.loadConfFile());
1516

1617
if (env.file) {
1718
// bitfact stamp file.
19+
1820
spinner.start();
19-
const stamped = await bitfact.stampFile(env.file, env.memo);
21+
22+
try {
23+
const stamped = await bitfact.stampFile(env.file, env.memo);
24+
helpers.stampDone(stamped.hash, stamped.txid);
25+
} catch (e) {
26+
helpers.errorExit(e);
27+
}
28+
2029
spinner.stop();
21-
helpers.stampDone(stamped.hash, stamped.txid);
2230
}
2331

2432
if (env.text) {
2533
// bitfact stamp text
2634
spinner.start();
27-
const stamped = await bitfact.stampText(env.text, env.memo);
35+
36+
try {
37+
const stamped = await bitfact.stampText(env.text, env.memo);
38+
helpers.stampDone(stamped.hash, stamped.txid);
39+
} catch (e) {
40+
helpers.errorExit(e);
41+
}
42+
2843
spinner.stop();
29-
helpers.stampDone(stamped.hash, stamped.txid);
3044
}
31-
3245
},
3346
};

cli/commands/verify.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,38 @@ spinner.setSpinnerString(19);
77

88
module.exports = {
99
prompt: async (env) => {
10-
1110
helpers.killForNoConf();
12-
if (!env.file && !env.text) helpers.errorExit("Must supply either -f <file> or -t <text> param.");
13-
if (env.file && env.text) helpers.errorExit("Cannot supply both -f <file> and -t <text> params.");
11+
if (!env.file && !env.text)
12+
helpers.errorExit("Must supply either -f <file> or -t <text> param.");
13+
if (env.file && env.text)
14+
helpers.errorExit("Cannot supply both -f <file> and -t <text> params.");
1415
const bitfact = new BitFact(helpers.loadConfFile());
15-
16+
1617
if (env.file) {
17-
// bitfact stamp file.
18+
// bitfact verify file.
1819
spinner.start();
19-
const isStamped = await bitfact.verifyFile(env.file, env.tx);
20+
21+
try {
22+
const isStamped = await bitfact.verifyFile(env.file, env.tx);
23+
helpers.verifyDone(isStamped);
24+
} catch (e) {
25+
helpers.errorExit(e);
26+
}
27+
2028
spinner.stop();
21-
helpers.verifyDone(isStamped);
2229
}
2330

2431
if (env.text) {
25-
// bitfact stamp text
32+
// bitfact verify text
2633
spinner.start();
27-
const isStamped = await bitfact.verifyText(env.text, env.tx);
34+
35+
try {
36+
const isStamped = await bitfact.verifyText(env.text, env.tx);
37+
helpers.verifyDone(isStamped);
38+
} catch (e) {
39+
helpers.errorExit(e);
40+
}
2841
spinner.stop();
29-
helpers.verifyDone(isStamped);
3042
}
31-
3243
},
3344
};

cli/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ const killForNoConf = () => {
2828
const stampDone = (hash, txid) => {
2929
if (!hash || !txid) errorExit("Could not write to Blockchain.");
3030
console.log("");
31-
console.log(chalk.green(chalk.bold("Success! Stamped on Blockchain.")));
31+
console.log(chalk.green(chalk.bold("🛡️ Success! Stamped on Blockchain.")));
3232
console.log("Hash: " + chalk.gray(chalk.bold(hash)));
3333
console.log("Txid: " + chalk.gray(chalk.bold(txid)));
3434
};
3535

3636
const verifyDone = (status) => {
3737
if (status === undefined) errorExit("Could not read from Blockchain.");
3838
console.log("");
39-
console.log(chalk.gray(chalk.bold("Response received from Blockchain.")));
39+
console.log(chalk.gray(chalk.bold("🛡️ Response received from Blockchain.")));
4040
console.log(
4141
"Stamped: " + chalk.bold(status ? chalk.green("YES") : chalk.red("NO"))
4242
);

0 commit comments

Comments
 (0)