Skip to content

3D engine that AI agents drive

Report a runtime hook failure that is not an Error (#2615)

abu-tahir-0c7d1364e75e3
diff --git a/changes/2615.md b/changes/2615.md
new file mode 100644
index 00000000..d80ae694
--- /dev/null
+++ b/changes/2615.md
@@ -0,0 +1 @@
+- A runtime hook that throws a string, `null` or a frozen Error is reported with the system, the hook and the original text instead of a `Cannot create property 'message'` error (#2615)
diff --git a/src/runtime.js b/src/runtime.js
index 44dbe1c7..b3cf26d2 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -123,9 +123,23 @@ export function createRuntime(target, options = {}) {
try {
system[hook](...args, context);
} catch (error) {
- error.message = `Runtime system "${system.name}" ${hook} failed: ${error.message}`;
- throw error;
+ throw hookFailure(system, hook, error);
+ }
+ }
+
+ // Names the system and hook on the failure. An ordinary Error keeps its identity and gets the prefixed message. Anything else
+ // (a string, null, a frozen Error) cannot take the prefix, so it becomes the cause of a new Error that does.
+ function hookFailure(system, hook, thrown) {
+ const message = `Runtime system "${system.name}" ${hook} failed: ${thrown instanceof Error ? thrown.message : String(thrown)}`;
+ if (thrown instanceof Error) {
+ try {
+ thrown.message = message;
+ return thrown;
+ } catch {
+ // A frozen or read-only message cannot carry the prefix; the new Error below does.
+ }
}
+ return new Error(message, { cause: thrown });
}
function initialise(system) {
diff --git a/tests/runtime.test.js b/tests/runtime.test.js
index fd735203..122c3781 100644
--- a/tests/runtime.test.js
+++ b/tests/runtime.test.js
@@ -219,6 +219,33 @@ test('a throwing system stops the loop and names the system', () => {
assert.equal(harness.hasPendingFrame(), false);
});
+test('a hook that throws a non-Error names the system, the hook and the original text (#2615)', () => {
+ const frozen = Object.freeze(new Error('frozen boom'));
+ const cases = [
+ ['a string', 'boom', 'boom'],
+ ['null', null, 'null'],
+ ['a frozen Error', frozen, 'frozen boom'],
+ ];
+ for (const [label, thrown, text] of cases) {
+ const harness = createHarness({ mode: 'variable', systems: [{ name: 'bad', update() { throw thrown; } }] });
+ let caught;
+ try {
+ harness.runtime.step(0);
+ } catch (error) {
+ caught = error;
+ }
+ assert.ok(caught instanceof Error, label);
+ assert.equal(caught.message, `Runtime system "bad" update failed: ${text}`, label);
+ assert.equal(caught.cause, thrown, `${label} is kept as the cause`);
+ }
+});
+
+test('an ordinary Error keeps its identity and gets the prefixed message (#2615)', () => {
+ const failure = new Error('boom');
+ const harness = createHarness({ mode: 'variable', systems: [{ name: 'bad', update() { throw failure; } }] });
+ assert.throws(() => harness.runtime.step(0), error => error === failure && error.message === 'Runtime system "bad" update failed: boom');
+});
+
test('step runs one frame without a scheduler', () => {
const log = [];
const harness = createHarness({ mode: 'fixed', fixedStepSeconds: 0.01, systems: [recorder('a', log)] });