Merge branch 'esm' of github.com:gchq/CyberChef into esm

This commit is contained in:
n1474335 2018-05-07 11:35:35 +01:00
commit 0ca0e7427d
10 changed files with 85 additions and 69 deletions

View file

@ -7,6 +7,7 @@
// import Operation from "./Operation.js";
import OpModules from "./config/modules/OpModules";
import OperationConfig from "./config/OperationConfig.json";
import OperationError from "./errors/OperationError";
import log from "loglevel";
/**
@ -175,18 +176,23 @@ 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);
// Return expected errors as output
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;
throw e;
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;
}
}
}

View file

@ -0,0 +1,24 @@
/**
* Custom error type for handling operation input errors.
* i.e. where the operation can handle the error and print a message to the screen.
*
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
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;

View file

@ -5,6 +5,7 @@
*/
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
/**
* Set cartesian product operation
@ -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?");
}
}
@ -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)));
}

View file

@ -5,6 +5,7 @@
*/
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
/**
* Set Difference operation
@ -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?");
}
}
@ -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)));
}

View file

@ -5,6 +5,7 @@
*/
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
/**
* Set Intersection operation
@ -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?");
}
}
@ -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)));
}

View file

@ -5,6 +5,7 @@
*/
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
/**
* Set Union operation
@ -44,7 +45,7 @@ class SetUnion 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?");
}
}
@ -54,16 +55,13 @@ class SetUnion 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.runUnion(...sets.map(s => s.split(this.itemDelimiter)));
}

View file

@ -6,6 +6,7 @@
import Utils from "../Utils";
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
/**
* Set Symmetric Difference operation
@ -45,7 +46,7 @@ class SymmetricDifference 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?");
}
}
@ -55,16 +56,13 @@ class SymmetricDifference 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.runSymmetricDifference(...sets.map(s => s.split(this.itemDelimiter)));
}