Skip to content

Commit 63916af

Browse files
feat: add expected erro class and its handling
1 parent 2e41028 commit 63916af

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

bin/create-webextension

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env node
22
const chalk = require("chalk");
3+
const UsageError = require("../errors").UsageError;
4+
const onlyInstancesOf = require("../errors").onlyInstancesOf;
5+
36

47
const USAGE_MSG = `Usage: create-webextension project_dir_name`;
58

@@ -11,4 +14,7 @@ if (!process.argv[2]) {
1114

1215
require("..").main(process.argv[2])
1316
.then(console.log)
14-
.catch(err => console.log(err.message));
17+
.catch(onlyInstancesOf(UsageError, (error) => {
18+
console.log(`${chalk.red(error.message)}\n`);
19+
process.exit(1);
20+
}));

errors.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const ES6Error = require("es6-error");
2+
3+
exports.onlyInstancesOf = function(errorType, handler) {
4+
return(error) => {
5+
if (error instanceof errorType) {
6+
return handler(error);
7+
} else {
8+
console.log(error.stack);
9+
process.exit(1);
10+
}
11+
}
12+
}
13+
14+
exports.UsageError = class UsageError extends ES6Error {
15+
constructor(message) {
16+
super(message);
17+
}
18+
}

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require("path");
44
const chalk = require("chalk");
55
const fs = require("mz/fs");
66
const stripAnsi = require("strip-ansi");
7+
const UsageError = require("./errors").UsageError;
78

89
const README = `
910
This project contains a blank WebExtension addon, a "white canvas" for your new experiment of
@@ -110,7 +111,7 @@ exports.main = function main(dirPath) {
110111
}, error => {
111112
if (error.code === "EEXIST") {
112113
const msg = `Unable to create a new WebExtension: ${chalk.bold.underline(projectPath)} dir already exist.`;
113-
throw new Error(msg);
114+
throw new UsageError(msg);
114115
}
115116
}).catch((error) => {
116117
throw error;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"homepage": "https://github.com/rpl/create-webextension#readme",
3434
"dependencies": {
3535
"chalk": "^1.1.3",
36+
"es6-error": "^4.0.2",
3637
"mz": "^2.6.0",
3738
"strip-ansi": "^3.0.1"
3839
},

0 commit comments

Comments
 (0)