Added 'DishError' and refined test results.
This commit is contained in:
parent
d2325306db
commit
b29bb6fdd7
@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import Utils from "./Utils";
|
import Utils from "./Utils";
|
||||||
|
import DishError from "./errors/DishError";
|
||||||
import BigNumber from "bignumber.js";
|
import BigNumber from "bignumber.js";
|
||||||
import log from "loglevel";
|
import log from "loglevel";
|
||||||
|
|
||||||
@ -61,7 +62,7 @@ class Dish {
|
|||||||
case "list<file>":
|
case "list<file>":
|
||||||
return Dish.LIST_FILE;
|
return Dish.LIST_FILE;
|
||||||
default:
|
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:
|
case Dish.LIST_FILE:
|
||||||
return "List<File>";
|
return "List<File>";
|
||||||
default:
|
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()) {
|
if (!this.valid()) {
|
||||||
const sample = Utils.truncate(JSON.stringify(this.value), 13);
|
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,77 +152,85 @@ class Dish {
|
|||||||
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
|
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
|
||||||
|
|
||||||
// Convert data to intermediate byteArray type
|
// Convert data to intermediate byteArray type
|
||||||
switch (this.type) {
|
try {
|
||||||
case Dish.STRING:
|
switch (this.type) {
|
||||||
this.value = this.value ? Utils.strToByteArray(this.value) : [];
|
case Dish.STRING:
|
||||||
break;
|
this.value = this.value ? Utils.strToByteArray(this.value) : [];
|
||||||
case Dish.NUMBER:
|
break;
|
||||||
this.value = typeof this.value === "number" ? Utils.strToByteArray(this.value.toString()) : [];
|
case Dish.NUMBER:
|
||||||
break;
|
this.value = typeof this.value === "number" ? Utils.strToByteArray(this.value.toString()) : [];
|
||||||
case Dish.HTML:
|
break;
|
||||||
this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : [];
|
case Dish.HTML:
|
||||||
break;
|
this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : [];
|
||||||
case Dish.ARRAY_BUFFER:
|
break;
|
||||||
// Array.from() would be nicer here, but it's slightly slower
|
case Dish.ARRAY_BUFFER:
|
||||||
this.value = Array.prototype.slice.call(new Uint8Array(this.value));
|
// Array.from() would be nicer here, but it's slightly slower
|
||||||
break;
|
this.value = Array.prototype.slice.call(new Uint8Array(this.value));
|
||||||
case Dish.BIG_NUMBER:
|
break;
|
||||||
this.value = this.value instanceof BigNumber ? Utils.strToByteArray(this.value.toFixed()) : [];
|
case Dish.BIG_NUMBER:
|
||||||
break;
|
this.value = this.value instanceof BigNumber ? Utils.strToByteArray(this.value.toFixed()) : [];
|
||||||
case Dish.JSON:
|
break;
|
||||||
this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value, null, 4)) : [];
|
case Dish.JSON:
|
||||||
break;
|
this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value, null, 4)) : [];
|
||||||
case Dish.FILE:
|
break;
|
||||||
this.value = await Utils.readFile(this.value);
|
case Dish.FILE:
|
||||||
this.value = Array.prototype.slice.call(this.value);
|
this.value = await Utils.readFile(this.value);
|
||||||
break;
|
this.value = Array.prototype.slice.call(this.value);
|
||||||
case Dish.LIST_FILE:
|
break;
|
||||||
this.value = await Promise.all(this.value.map(async f => Utils.readFile(f)));
|
case Dish.LIST_FILE:
|
||||||
this.value = this.value.map(b => Array.prototype.slice.call(b));
|
this.value = await Promise.all(this.value.map(async f => Utils.readFile(f)));
|
||||||
this.value = [].concat.apply([], this.value);
|
this.value = this.value.map(b => Array.prototype.slice.call(b));
|
||||||
break;
|
this.value = [].concat.apply([], this.value);
|
||||||
default:
|
break;
|
||||||
break;
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw new DishError(`Error translating from ${Dish.enumLookup(this.type)} to byteArray: ${err}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.type = Dish.BYTE_ARRAY;
|
this.type = Dish.BYTE_ARRAY;
|
||||||
|
|
||||||
// Convert from byteArray to toType
|
// Convert from byteArray to toType
|
||||||
switch (toType) {
|
try {
|
||||||
case Dish.STRING:
|
switch (toType) {
|
||||||
case Dish.HTML:
|
case Dish.STRING:
|
||||||
this.value = this.value ? byteArrayToStr(this.value) : "";
|
case Dish.HTML:
|
||||||
this.type = Dish.STRING;
|
this.value = this.value ? byteArrayToStr(this.value) : "";
|
||||||
break;
|
this.type = Dish.STRING;
|
||||||
case Dish.NUMBER:
|
break;
|
||||||
this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0;
|
case Dish.NUMBER:
|
||||||
this.type = Dish.NUMBER;
|
this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0;
|
||||||
break;
|
this.type = Dish.NUMBER;
|
||||||
case Dish.ARRAY_BUFFER:
|
break;
|
||||||
this.value = new Uint8Array(this.value).buffer;
|
case Dish.ARRAY_BUFFER:
|
||||||
this.type = Dish.ARRAY_BUFFER;
|
this.value = new Uint8Array(this.value).buffer;
|
||||||
break;
|
this.type = Dish.ARRAY_BUFFER;
|
||||||
case Dish.BIG_NUMBER:
|
break;
|
||||||
try {
|
case Dish.BIG_NUMBER:
|
||||||
this.value = new BigNumber(byteArrayToStr(this.value));
|
try {
|
||||||
} catch (err) {
|
this.value = new BigNumber(byteArrayToStr(this.value));
|
||||||
this.value = new BigNumber(NaN);
|
} catch (err) {
|
||||||
}
|
this.value = new BigNumber(NaN);
|
||||||
this.type = Dish.BIG_NUMBER;
|
}
|
||||||
break;
|
this.type = Dish.BIG_NUMBER;
|
||||||
case Dish.JSON:
|
break;
|
||||||
this.value = JSON.parse(byteArrayToStr(this.value));
|
case Dish.JSON:
|
||||||
this.type = Dish.JSON;
|
this.value = JSON.parse(byteArrayToStr(this.value));
|
||||||
break;
|
this.type = Dish.JSON;
|
||||||
case Dish.FILE:
|
break;
|
||||||
this.value = new File(this.value, "unknown");
|
case Dish.FILE:
|
||||||
break;
|
this.value = new File(this.value, "unknown");
|
||||||
case Dish.LIST_FILE:
|
break;
|
||||||
this.value = [new File(this.value, "unknown")];
|
case Dish.LIST_FILE:
|
||||||
this.type = Dish.LIST_FILE;
|
this.value = [new File(this.value, "unknown")];
|
||||||
break;
|
this.type = Dish.LIST_FILE;
|
||||||
default:
|
break;
|
||||||
break;
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,7 +366,7 @@ class Dish {
|
|||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error("Cannot clone Dish, unknown type");
|
throw new DishError("Cannot clone Dish, unknown type");
|
||||||
}
|
}
|
||||||
|
|
||||||
return newDish;
|
return newDish;
|
||||||
|
@ -4,10 +4,10 @@
|
|||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// import Operation from "./Operation.js";
|
|
||||||
import OpModules from "./config/modules/OpModules";
|
import OpModules from "./config/modules/OpModules";
|
||||||
import OperationConfig from "./config/OperationConfig.json";
|
import OperationConfig from "./config/OperationConfig.json";
|
||||||
import OperationError from "./errors/OperationError";
|
import OperationError from "./errors/OperationError";
|
||||||
|
import DishError from "./errors/DishError";
|
||||||
import log from "loglevel";
|
import log from "loglevel";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -183,6 +183,10 @@ class Recipe {
|
|||||||
// native types is not fully supported yet.
|
// native types is not fully supported yet.
|
||||||
dish.set(err.message, "string");
|
dish.set(err.message, "string");
|
||||||
return i;
|
return i;
|
||||||
|
} else if (err instanceof DishError ||
|
||||||
|
(err.type && err.type === "DishError")) {
|
||||||
|
dish.set(err.message, "string");
|
||||||
|
return i;
|
||||||
} else {
|
} else {
|
||||||
const e = typeof err == "string" ? { message: err } : err;
|
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 Operation from "../Operation";
|
||||||
import OperationError from "../errors/OperationError.mjs";
|
import OperationError from "../errors/OperationError";
|
||||||
import notepack from "notepack.io";
|
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.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Utils from "../Utils.mjs";
|
import Utils from "../Utils";
|
||||||
|
|
||||||
// Param jpeg should be a binaryArray
|
// Param jpeg should be a binaryArray
|
||||||
export function removeEXIF(jpeg) {
|
export function removeEXIF(jpeg) {
|
||||||
|
@ -77,7 +77,7 @@ import "./tests/operations/SetUnion";
|
|||||||
import "./tests/operations/StrUtils";
|
import "./tests/operations/StrUtils";
|
||||||
import "./tests/operations/SymmetricDifference";
|
import "./tests/operations/SymmetricDifference";
|
||||||
import "./tests/operations/TextEncodingBruteForce";
|
import "./tests/operations/TextEncodingBruteForce";
|
||||||
import "./tests/operations/ToGeohash.mjs";
|
import "./tests/operations/ToGeohash";
|
||||||
import "./tests/operations/TranslateDateTimeFormat";
|
import "./tests/operations/TranslateDateTimeFormat";
|
||||||
import "./tests/operations/Magic";
|
import "./tests/operations/Magic";
|
||||||
import "./tests/operations/ParseTLV";
|
import "./tests/operations/ParseTLV";
|
||||||
@ -155,7 +155,8 @@ TestRegister.runTests()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!allTestsPassing) {
|
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);
|
process.exit(allTestsPassing ? 0 : 1);
|
||||||
|
@ -334,8 +334,8 @@ TestRegister.addTests([
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "To MessagePack: no content",
|
name: "To MessagePack: no content",
|
||||||
input: "",
|
input: "{}",
|
||||||
expectedError: true,
|
expectedMatch: /Unexpected end of JSON input/,
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
"op": "To MessagePack",
|
"op": "To MessagePack",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user