Added 'DishError' and refined test results.
This commit is contained in:
parent
d2325306db
commit
b29bb6fdd7
@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
import Utils from "./Utils";
|
||||
import DishError from "./errors/DishError";
|
||||
import BigNumber from "bignumber.js";
|
||||
import log from "loglevel";
|
||||
|
||||
@ -61,7 +62,7 @@ class Dish {
|
||||
case "list<file>":
|
||||
return Dish.LIST_FILE;
|
||||
default:
|
||||
throw "Invalid data type string. No matching enum.";
|
||||
throw new DishError("Invalid data type string. No matching enum.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,7 +94,7 @@ class Dish {
|
||||
case Dish.LIST_FILE:
|
||||
return "List<File>";
|
||||
default:
|
||||
throw "Invalid data type enum. No matching type.";
|
||||
throw new DishError("Invalid data type enum. No matching type.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,7 +118,7 @@ class Dish {
|
||||
|
||||
if (!this.valid()) {
|
||||
const sample = Utils.truncate(JSON.stringify(this.value), 13);
|
||||
throw "Data is not a valid " + Dish.enumLookup(type) + ": " + sample;
|
||||
throw new DishError(`Data is not a valid ${Dish.enumLookup(type)}: ${sample}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,6 +152,7 @@ class Dish {
|
||||
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
|
||||
|
||||
// Convert data to intermediate byteArray type
|
||||
try {
|
||||
switch (this.type) {
|
||||
case Dish.STRING:
|
||||
this.value = this.value ? Utils.strToByteArray(this.value) : [];
|
||||
@ -183,10 +185,14 @@ class Dish {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
throw new DishError(`Error translating from ${Dish.enumLookup(this.type)} to byteArray: ${err}`);
|
||||
}
|
||||
|
||||
this.type = Dish.BYTE_ARRAY;
|
||||
|
||||
// Convert from byteArray to toType
|
||||
try {
|
||||
switch (toType) {
|
||||
case Dish.STRING:
|
||||
case Dish.HTML:
|
||||
@ -223,6 +229,9 @@ class Dish {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -357,7 +366,7 @@ class Dish {
|
||||
);
|
||||
break;
|
||||
default:
|
||||
throw new Error("Cannot clone Dish, unknown type");
|
||||
throw new DishError("Cannot clone Dish, unknown type");
|
||||
}
|
||||
|
||||
return newDish;
|
||||
|
@ -4,10 +4,10 @@
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
// import Operation from "./Operation.js";
|
||||
import OpModules from "./config/modules/OpModules";
|
||||
import OperationConfig from "./config/OperationConfig.json";
|
||||
import OperationError from "./errors/OperationError";
|
||||
import DishError from "./errors/DishError";
|
||||
import log from "loglevel";
|
||||
|
||||
/**
|
||||
@ -183,6 +183,10 @@ class Recipe {
|
||||
// native types is not fully supported yet.
|
||||
dish.set(err.message, "string");
|
||||
return i;
|
||||
} else if (err instanceof DishError ||
|
||||
(err.type && err.type === "DishError")) {
|
||||
dish.set(err.message, "string");
|
||||
return i;
|
||||
} else {
|
||||
const e = typeof err == "string" ? { message: err } : err;
|
||||
|
||||
|
26
src/core/errors/DishError.mjs
Normal file
26
src/core/errors/DishError.mjs
Normal file
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Custom error type for handling Dish type errors.
|
||||
* i.e. where the Dish cannot be successfully translated between types
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
class DishError extends Error {
|
||||
/**
|
||||
* Standard error constructor. Adds no new behaviour.
|
||||
*
|
||||
* @param args - Standard error args
|
||||
*/
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
|
||||
this.type = "DishError";
|
||||
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this, DishError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default DishError;
|
@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import OperationError from "../errors/OperationError";
|
||||
import notepack from "notepack.io";
|
||||
|
||||
/**
|
||||
|
2
src/core/vendor/remove-exif.mjs
vendored
2
src/core/vendor/remove-exif.mjs
vendored
@ -18,7 +18,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
import Utils from "../Utils.mjs";
|
||||
import Utils from "../Utils";
|
||||
|
||||
// Param jpeg should be a binaryArray
|
||||
export function removeEXIF(jpeg) {
|
||||
|
@ -77,7 +77,7 @@ import "./tests/operations/SetUnion";
|
||||
import "./tests/operations/StrUtils";
|
||||
import "./tests/operations/SymmetricDifference";
|
||||
import "./tests/operations/TextEncodingBruteForce";
|
||||
import "./tests/operations/ToGeohash.mjs";
|
||||
import "./tests/operations/ToGeohash";
|
||||
import "./tests/operations/TranslateDateTimeFormat";
|
||||
import "./tests/operations/Magic";
|
||||
import "./tests/operations/ParseTLV";
|
||||
@ -155,7 +155,8 @@ TestRegister.runTests()
|
||||
}
|
||||
|
||||
if (!allTestsPassing) {
|
||||
console.log("\nNot all tests are passing");
|
||||
console.log("\nFailing tests:\n");
|
||||
results.filter(r => r.status !== "passing").forEach(handleTestResult);
|
||||
}
|
||||
|
||||
process.exit(allTestsPassing ? 0 : 1);
|
||||
|
@ -334,8 +334,8 @@ TestRegister.addTests([
|
||||
},
|
||||
{
|
||||
name: "To MessagePack: no content",
|
||||
input: "",
|
||||
expectedError: true,
|
||||
input: "{}",
|
||||
expectedMatch: /Unexpected end of JSON input/,
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "To MessagePack",
|
||||
|
Loading…
x
Reference in New Issue
Block a user