Skip to content

Commit cc20ad9

Browse files
committed
Add OperationError error type and use for errors to be printed to the output panel
1 parent a8aa1bc commit cc20ad9

10 files changed

Lines changed: 82 additions & 69 deletions

File tree

.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@
1010
"modules": false,
1111
"useBuiltIns": true
1212
}]
13+
],
14+
"plugins": [
15+
["babel-plugin-transform-builtin-extend", {
16+
"globals": ["Error"]
17+
}]
1318
]
1419
}

package-lock.json

Lines changed: 18 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
},
6969
"dependencies": {
7070
"babel-polyfill": "^6.26.0",
71+
"babel-plugin-transform-builtin-extend": "1.1.2",
7172
"bcryptjs": "^2.4.3",
7273
"bignumber.js": "^6.0.0",
7374
"bootstrap": "^3.3.7",

src/core/Recipe.mjs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import OpModules from "./config/modules/OpModules";
99
import OperationConfig from "./config/OperationConfig.json";
1010
import log from "loglevel";
11+
import OperationError from "./errors/OperationError";
1112

1213
/**
1314
* The Recipe controls a list of Operations and the Dish they operate on.
@@ -175,18 +176,24 @@ class Recipe {
175176
dish.set(output, op.outputType);
176177
}
177178
} catch (err) {
178-
const e = typeof err == "string" ? { message: err } : err;
179-
180-
e.progress = i;
181-
if (e.fileName) {
182-
e.displayStr = op.name + " - " + e.name + " in " +
183-
e.fileName + " on line " + e.lineNumber +
184-
".<br><br>Message: " + (e.displayStr || e.message);
179+
// print expected errors in output pane
180+
if (err instanceof OperationError) {
181+
dish.set(err.message, "string");
182+
return i;
185183
} else {
186-
e.displayStr = op.name + " - " + (e.displayStr || e.message);
184+
const e = typeof err == "string" ? { message: err } : err;
185+
186+
e.progress = i;
187+
if (e.fileName) {
188+
e.displayStr = op.name + " - " + e.name + " in " +
189+
e.fileName + " on line " + e.lineNumber +
190+
".<br><br>Message: " + (e.displayStr || e.message);
191+
} else {
192+
e.displayStr = op.name + " - " + (e.displayStr || e.message);
193+
}
194+
195+
throw e;
187196
}
188-
189-
throw e;
190197
}
191198
}
192199

src/core/errors/OperationError.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Create custom error type for handling operation input errors.
3+
* i.e. where the operation can handle the error and print a
4+
* message to the screen.
5+
*/
6+
class OperationError extends Error {
7+
/**
8+
* Standard error constructor. Adds no new behaviour.
9+
* @param args standard error args
10+
*/
11+
constructor(...args) {
12+
super(...args);
13+
14+
if (Error.captureStackTrace) {
15+
Error.captureStackTrace(this, OperationError);
16+
}
17+
}
18+
}
19+
20+
export default OperationError;

src/core/operations/CartesianProduct.mjs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import Operation from "../Operation";
8+
import OperationError from "../errors/OperationError";
89

910
/**
1011
* Set cartesian product operation
@@ -44,7 +45,8 @@ class CartesianProduct extends Operation {
4445
*/
4546
validateSampleNumbers(sets) {
4647
if (!sets || sets.length < 2) {
47-
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
48+
throw new OperationError("Incorrect number of sets, perhaps you" +
49+
" need to modify the sample delimiter or add more samples?");
4850
}
4951
}
5052

@@ -54,16 +56,13 @@ class CartesianProduct extends Operation {
5456
* @param {string} input
5557
* @param {Object[]} args
5658
* @returns {string}
59+
* @throws {OperationError}
5760
*/
5861
run(input, args) {
5962
[this.sampleDelim, this.itemDelimiter] = args;
6063
const sets = input.split(this.sampleDelim);
6164

62-
try {
63-
this.validateSampleNumbers(sets);
64-
} catch (e) {
65-
return e;
66-
}
65+
this.validateSampleNumbers(sets);
6766

6867
return this.runCartesianProduct(...sets.map(s => s.split(this.itemDelimiter)));
6968
}

src/core/operations/SetDifference.mjs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import Operation from "../Operation";
8+
import OperationError from "../errors/OperationError";
89

910
/**
1011
* Set Difference operation
@@ -44,7 +45,7 @@ class SetDifference extends Operation {
4445
*/
4546
validateSampleNumbers(sets) {
4647
if (!sets || (sets.length !== 2)) {
47-
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
48+
throw new OperationError("Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?");
4849
}
4950
}
5051

@@ -54,16 +55,13 @@ class SetDifference extends Operation {
5455
* @param {string} input
5556
* @param {Object[]} args
5657
* @returns {string}
58+
* @throws {OperationError}
5759
*/
5860
run(input, args) {
5961
[this.sampleDelim, this.itemDelimiter] = args;
6062
const sets = input.split(this.sampleDelim);
6163

62-
try {
63-
this.validateSampleNumbers(sets);
64-
} catch (e) {
65-
return e;
66-
}
64+
this.validateSampleNumbers(sets);
6765

6866
return this.runSetDifference(...sets.map(s => s.split(this.itemDelimiter)));
6967
}

src/core/operations/SetIntersection.mjs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import Operation from "../Operation";
8+
import OperationError from "../errors/OperationError";
89

910
/**
1011
* Set Intersection operation
@@ -44,7 +45,7 @@ class SetIntersection extends Operation {
4445
*/
4546
validateSampleNumbers(sets) {
4647
if (!sets || (sets.length !== 2)) {
47-
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
48+
throw new OperationError("Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?");
4849
}
4950
}
5051

@@ -54,16 +55,13 @@ class SetIntersection extends Operation {
5455
* @param {string} input
5556
* @param {Object[]} args
5657
* @returns {string}
58+
* @throws {OperationError}
5759
*/
5860
run(input, args) {
5961
[this.sampleDelim, this.itemDelimiter] = args;
6062
const sets = input.split(this.sampleDelim);
6163

62-
try {
63-
this.validateSampleNumbers(sets);
64-
} catch (e) {
65-
return e;
66-
}
64+
this.validateSampleNumbers(sets);
6765

6866
return this.runIntersect(...sets.map(s => s.split(this.itemDelimiter)));
6967
}

0 commit comments

Comments
 (0)