Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OperationError error type #296

Merged
merged 1 commit into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@
"modules": false,
"useBuiltIns": true
}]
],
"plugins": [
["babel-plugin-transform-builtin-extend", {
"globals": ["Error"]
}]
]
}
47 changes: 18 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
},
"dependencies": {
"babel-polyfill": "^6.26.0",
"babel-plugin-transform-builtin-extend": "1.1.2",
"bcryptjs": "^2.4.3",
"bignumber.js": "^6.0.0",
"bootstrap": "^3.3.7",
Expand Down
27 changes: 17 additions & 10 deletions src/core/Recipe.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import OpModules from "./config/modules/OpModules";
import OperationConfig from "./config/OperationConfig.json";
import log from "loglevel";
import OperationError from "./errors/OperationError";

/**
* The Recipe controls a list of Operations and the Dish they operate on.
Expand Down Expand Up @@ -175,18 +176,24 @@ class Recipe {
dish.set(output, op.outputType);
}
} catch (err) {
const e = typeof err == "string" ? { message: err } : err;

e.progress = i;
if (e.fileName) {
e.displayStr = op.name + " - " + e.name + " in " +
e.fileName + " on line " + e.lineNumber +
".<br><br>Message: " + (e.displayStr || e.message);
// print expected errors in output pane
if (err instanceof OperationError) {
dish.set(err.message, "string");
return i;
} else {
e.displayStr = op.name + " - " + (e.displayStr || e.message);
const e = typeof err == "string" ? { message: err } : err;

e.progress = i;
if (e.fileName) {
e.displayStr = op.name + " - " + e.name + " in " +
e.fileName + " on line " + e.lineNumber +
".<br><br>Message: " + (e.displayStr || e.message);
} else {
e.displayStr = op.name + " - " + (e.displayStr || e.message);
}

throw e;
}

throw e;
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/core/errors/OperationError.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Create custom error type for handling operation input errors.
* i.e. where the operation can handle the error and print a
* message to the screen.
*/
class OperationError extends Error {
/**
* Standard error constructor. Adds no new behaviour.
* @param args standard error args
*/
constructor(...args) {
super(...args);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, OperationError);
}
}
}

export default OperationError;
11 changes: 5 additions & 6 deletions src/core/operations/CartesianProduct.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import Operation from "../Operation";
import OperationError from "../errors/OperationError";

/**
* Set cartesian product operation
Expand Down Expand Up @@ -44,7 +45,8 @@ class CartesianProduct extends Operation {
*/
validateSampleNumbers(sets) {
if (!sets || sets.length < 2) {
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
throw new OperationError("Incorrect number of sets, perhaps you" +
" need to modify the sample delimiter or add more samples?");
}
}

Expand All @@ -54,16 +56,13 @@ class CartesianProduct extends Operation {
* @param {string} input
* @param {Object[]} args
* @returns {string}
* @throws {OperationError}
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
const sets = input.split(this.sampleDelim);

try {
this.validateSampleNumbers(sets);
} catch (e) {
return e;
}
this.validateSampleNumbers(sets);

return this.runCartesianProduct(...sets.map(s => s.split(this.itemDelimiter)));
}
Expand Down
10 changes: 4 additions & 6 deletions src/core/operations/SetDifference.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import Operation from "../Operation";
import OperationError from "../errors/OperationError";

/**
* Set Difference operation
Expand Down Expand Up @@ -44,7 +45,7 @@ class SetDifference extends Operation {
*/
validateSampleNumbers(sets) {
if (!sets || (sets.length !== 2)) {
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
throw new OperationError("Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?");
}
}

Expand All @@ -54,16 +55,13 @@ class SetDifference extends Operation {
* @param {string} input
* @param {Object[]} args
* @returns {string}
* @throws {OperationError}
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
const sets = input.split(this.sampleDelim);

try {
this.validateSampleNumbers(sets);
} catch (e) {
return e;
}
this.validateSampleNumbers(sets);

return this.runSetDifference(...sets.map(s => s.split(this.itemDelimiter)));
}
Expand Down
10 changes: 4 additions & 6 deletions src/core/operations/SetIntersection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import Operation from "../Operation";
import OperationError from "../errors/OperationError";

/**
* Set Intersection operation
Expand Down Expand Up @@ -44,7 +45,7 @@ class SetIntersection extends Operation {
*/
validateSampleNumbers(sets) {
if (!sets || (sets.length !== 2)) {
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
throw new OperationError("Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?");
}
}

Expand All @@ -54,16 +55,13 @@ class SetIntersection extends Operation {
* @param {string} input
* @param {Object[]} args
* @returns {string}
* @throws {OperationError}
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
const sets = input.split(this.sampleDelim);

try {
this.validateSampleNumbers(sets);
} catch (e) {
return e;
}
this.validateSampleNumbers(sets);

return this.runIntersect(...sets.map(s => s.split(this.itemDelimiter)));
}
Expand Down
Loading