Skip to content

Commit 3f31071

Browse files
authored
refactor: replace primitive string throws with Error objects (#6602)
Standardized error handling in math utilities, pitch actions, and program blocks by replacing string throws with standard Error objects. Updated NumberBlocks catch sites and unit tests to handle the new Error object format. Reference: Linked to issue created by user.
1 parent e123a40 commit 3f31071

5 files changed

Lines changed: 27 additions & 27 deletions

File tree

js/blocks/NumberBlocks.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ function setupNumberBlocks(activity) {
307307
return MathUtility.doSqrt(a);
308308
} catch (e) {
309309
logo.stopTurtle = true;
310-
if (e === "NanError") {
310+
if (e.message === "NanError") {
311311
activity.errorMsg(NANERRORMSG, blk);
312-
} else if (e === "NoSqrtError") {
312+
} else if (e.message === "NoSqrtError") {
313313
activity.errorMsg(NOSQRTERRORMSG, blk);
314314
return MathUtility.doSqrt(-a);
315315
}
@@ -464,9 +464,9 @@ function setupNumberBlocks(activity) {
464464
return MathUtility.doDivide(a, b);
465465
} catch (e) {
466466
logo.stopTurtle = true;
467-
if (e === "NanError") {
467+
if (e.message === "NanError") {
468468
activity.errorMsg(NANERRORMSG, blk);
469-
} else if (e === "DivByZeroError") {
469+
} else if (e.message === "DivByZeroError") {
470470
activity.errorMsg(ZERODIVIDEERRORMSG, blk);
471471
}
472472
return 0;
@@ -785,9 +785,9 @@ function setupNumberBlocks(activity) {
785785
return MathUtility.doPlus(a, b);
786786
} catch (e) {
787787
activity.errorMsg(NOINPUTERRORMSG, blk);
788-
// eslint-disable-next-line no-console
788+
789789
console.debug(a + " " + b);
790-
// eslint-disable-next-line no-console
790+
791791
console.debug(e);
792792
if (!isNaN(a)) {
793793
return a;

js/blocks/ProgramBlocks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function setupProgramBlocks(activity) {
253253
activity.blocks.blockList[c].value[1]
254254
);
255255
if (!Array.isArray(logo.turtleHeaps[turtle])) {
256-
throw "is not array";
256+
throw new Error("is not array");
257257
}
258258
} catch (e) {
259259
logo.turtleHeaps[turtle] = oldHeap;
@@ -324,7 +324,7 @@ function setupProgramBlocks(activity) {
324324
try {
325325
logo.turtleHeaps[turtle] = JSON.parse(activity.blocks.blockList[c].value);
326326
if (!Array.isArray(logo.turtleHeaps[turtle])) {
327-
throw "is not array";
327+
throw new Error("is not array");
328328
}
329329
} catch (e) {
330330
logo.turtleHeaps[turtle] = oldHeap;

js/blocks/__tests__/NumberBlocks.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ global.MathUtility = {
131131
doMod: (a, b) => Number(a) % Number(b),
132132
doPower: (a, b) => Math.pow(Number(a), Number(b)),
133133
doSqrt: a => {
134-
if (Number(a) < 0) throw "NoSqrtError";
134+
if (Number(a) < 0) throw new Error("NoSqrtError");
135135
return Math.sqrt(Number(a));
136136
},
137137
doAbs: a => Math.abs(Number(a)),
138138
doCalculateDistance: (x1, y1, x2, y2) => Math.hypot(x2 - x1, y2 - y1),
139139
doDivide: (a, b) => {
140-
if (Number(b) === 0) throw "DivByZeroError";
140+
if (Number(b) === 0) throw new Error("DivByZeroError");
141141
return Number(a) / Number(b);
142142
},
143143
doMultiply: (a, b) => Number(a) * Number(b),
@@ -320,7 +320,7 @@ describe("setupNumberBlocks", () => {
320320
activity.blocks.blockList[130] = { connections: [null, "c1"] };
321321
logo.parseArg = jest.fn(() => -9);
322322
global.MathUtility.doSqrt = a => {
323-
if (Number(a) < 0) throw "NoSqrtError";
323+
if (Number(a) < 0) throw new Error("NoSqrtError");
324324
return Math.sqrt(Number(a));
325325
};
326326
const sqrtBlock = createdBlocks["sqrt"];
@@ -332,7 +332,7 @@ describe("setupNumberBlocks", () => {
332332
activity.blocks.blockList[130] = { connections: [null, "c1"] };
333333
logo.parseArg = jest.fn(() => "bad");
334334
global.MathUtility.doSqrt = () => {
335-
throw "NanError";
335+
throw new Error("NanError");
336336
};
337337
const sqrtBlock = createdBlocks["sqrt"];
338338
const result = sqrtBlock.arg(logo, 0, 130, null);
@@ -396,14 +396,14 @@ describe("setupNumberBlocks", () => {
396396
activity.blocks.blockList[160] = { connections: [null, "c1", "c2"] };
397397
logo.parseArg = jest.fn(() => 5);
398398
global.MathUtility.doDivide = () => {
399-
throw "NanError";
399+
throw new Error("NanError");
400400
};
401401
const divideBlock = createdBlocks["divide"];
402402
const result = divideBlock.arg(logo, 0, 160, null);
403403
expect(activity.errorMsg).toHaveBeenCalledWith(global.NANERRORMSG, 160);
404404
expect(result).toEqual(0);
405405
global.MathUtility.doDivide = (a, b) => {
406-
if (Number(b) === 0) throw "DivByZeroError";
406+
if (Number(b) === 0) throw new Error("DivByZeroError");
407407
return Number(a) / Number(b);
408408
};
409409
});

js/turtleactions/PitchActions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ function setupPitchActions(activity) {
573573
return obj[1];
574574
}
575575
} else {
576-
throw "NoArgError";
576+
throw new Error("NoArgError");
577577
}
578578
}
579579

js/utils/mathutils.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class MathUtility {
102102
) {
103103
return GetRandomSolfege(a, b, c);
104104
} else {
105-
throw "NanError";
105+
throw new Error("NanError");
106106
}
107107
}
108108

@@ -149,12 +149,12 @@ class MathUtility {
149149
static doSqrt(a) {
150150
if (typeof a === "number") {
151151
if (a < 0) {
152-
throw "NoSqrtError";
152+
throw new Error("NoSqrtError");
153153
}
154154

155155
return Math.sqrt(Number(a));
156156
} else {
157-
throw "NanError";
157+
throw new Error("NanError");
158158
}
159159
}
160160

@@ -188,7 +188,7 @@ class MathUtility {
188188
*/
189189
static doMinus(a, b) {
190190
if (typeof a === "string" || typeof b === "string") {
191-
throw "NanError";
191+
throw new Error("NanError");
192192
}
193193

194194
return Number(a) - Number(b);
@@ -205,7 +205,7 @@ class MathUtility {
205205
*/
206206
static doMultiply(a, b) {
207207
if (typeof a === "string" || typeof b === "string") {
208-
throw "NanError";
208+
throw new Error("NanError");
209209
}
210210

211211
return Number(a) * Number(b);
@@ -223,12 +223,12 @@ class MathUtility {
223223
static doDivide(a, b) {
224224
if (typeof a === "number" && typeof b === "number") {
225225
if (Number(b) === 0) {
226-
throw "DivByZeroError";
226+
throw new Error("DivByZeroError");
227227
}
228228

229229
return Number(a) / Number(b);
230230
} else {
231-
throw "NanError";
231+
throw new Error("NanError");
232232
}
233233
}
234234

@@ -256,7 +256,7 @@ class MathUtility {
256256

257257
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
258258
} else {
259-
throw "NanError";
259+
throw new Error("NanError");
260260
}
261261
}
262262

@@ -273,7 +273,7 @@ class MathUtility {
273273
if (typeof a === "number" && typeof b === "number") {
274274
return Math.pow(a, b);
275275
} else {
276-
throw "NanError";
276+
throw new Error("NanError");
277277
}
278278
}
279279

@@ -289,7 +289,7 @@ class MathUtility {
289289
if (typeof a === "number") {
290290
return Math.abs(a);
291291
} else {
292-
throw "NanError";
292+
throw new Error("NanError");
293293
}
294294
}
295295

@@ -308,7 +308,7 @@ class MathUtility {
308308
const obj = a.split("");
309309
return obj.reverse().join("");
310310
} else {
311-
throw "NoNegError";
311+
throw new Error("NoNegError");
312312
}
313313
}
314314

@@ -324,7 +324,7 @@ class MathUtility {
324324
try {
325325
return Math.floor(Number(a) + 0.5);
326326
} catch (e) {
327-
throw "NanError";
327+
throw new Error("NanError");
328328
}
329329
}
330330
}

0 commit comments

Comments
 (0)