Skip to content

Commit 8c4a55b

Browse files
Lamby777C85297
andauthored
Add more helpful error for when numerical ingredient is left empty (#1540)
Co-authored-by: GCHQ Developer C85297 <95289555+C85297@users.noreply.github.com>
1 parent a1f9208 commit 8c4a55b

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/core/Chef.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,15 @@ class Chef {
5555
progress = await recipe.execute(this.dish, progress);
5656
} catch (err) {
5757
log.error(err);
58+
59+
let displayStr;
60+
if ("displayStr" in err) {
61+
displayStr = err.displayStr;
62+
} else {
63+
displayStr = err.toString();
64+
}
5865
error = {
59-
displayStr: err.displayStr,
66+
displayStr: displayStr,
6067
};
6168
progress = err.progress;
6269
}

src/core/Ingredient.mjs

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

77
import Utils from "./Utils.mjs";
8-
import {fromHex} from "./lib/Hex.mjs";
8+
import { fromHex } from "./lib/Hex.mjs";
9+
import OperationError from "./errors/OperationError.mjs";
910

1011
/**
1112
* The arguments to operations.
@@ -119,7 +120,9 @@ class Ingredient {
119120
number = parseFloat(data);
120121
if (isNaN(number)) {
121122
const sample = Utils.truncate(data.toString(), 10);
122-
throw "Invalid ingredient value. Not a number: " + sample;
123+
throw new OperationError(
124+
"Invalid ingredient value. Not a number: " + sample,
125+
);
123126
}
124127
return number;
125128
default:

src/core/Operation.mjs

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

77
import Dish from "./Dish.mjs";
8+
import OperationError from "./errors/OperationError.mjs";
89
import Ingredient from "./Ingredient.mjs";
910

1011
/**
@@ -223,7 +224,11 @@ class Operation {
223224
*/
224225
set ingValues(ingValues) {
225226
ingValues.forEach((val, i) => {
226-
this._ingList[i].value = val;
227+
try {
228+
this._ingList[i].value = val;
229+
} catch (err) {
230+
throw new OperationError(`Failed to set value of ingredient '${this._ingList[i].name}': ${err}`);
231+
}
227232
});
228233
}
229234

src/core/Recipe.mjs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ class Recipe {
7070
if (o instanceof Operation) {
7171
return o;
7272
} else {
73-
const op = new modules[o.module][o.name]();
74-
op.ingValues = o.ingValues;
75-
op.breakpoint = o.breakpoint;
76-
op.disabled = o.disabled;
77-
return op;
73+
try {
74+
const op = new modules[o.module][o.name]();
75+
op.ingValues = o.ingValues;
76+
op.breakpoint = o.breakpoint;
77+
op.disabled = o.disabled;
78+
return op;
79+
} catch (err) {
80+
throw new Error(`Failed to hydrate operation '${o.name}': ${err}`);
81+
}
7882
}
7983
});
8084
}

0 commit comments

Comments
 (0)