Skip to content

Commit b468185

Browse files
committed
[BUGFIX] Regression [Ember 5.12+] Non helpful error when the function for on modifier was forget to add #21045 #20783
1 parent 0531e62 commit b468185

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

packages/@glimmer/runtime/lib/modifiers/on.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,13 @@ export class OnModifierState {
7979
}
8080
) as EventListener;
8181

82-
localAssert(
83-
typeof userProvidedCallback === 'function',
84-
`You must pass a function as the second argument to the \`on\` modifier; you passed ${
85-
userProvidedCallback === null ? 'null' : typeof userProvidedCallback
86-
}. While rendering:\n\n${args.positional[1]?.debugLabel ?? `{unlabeled value}`}`
87-
);
82+
if (DEBUG && typeof userProvidedCallback !== 'function') {
83+
throw new Error(
84+
`You must pass a function as the second argument to the \`on\` modifier; you passed ${
85+
userProvidedCallback === null ? 'null' : typeof userProvidedCallback
86+
}. While rendering:\n\n${args.positional[1]?.debugLabel ?? `{unlabeled value}`}`
87+
);
88+
}
8889

8990
if (DEBUG && args.positional.length !== 2) {
9091
throw new Error(

smoke-tests/scenarios/basic-test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,44 @@ function basicTest(scenarios: Scenarios, appName: string) {
269269
let allNodes = flattenTree(tree);
270270
let names = allNodes.filter(n => n.type === 'component').map(n => n.name);
271271
assert.true(names.includes('HelloWorld'), 'HelloWorld component name is preserved in the render tree (found: ' + names.join(', ') + ')');
272+
});
273+
});
274+
`,
275+
'on-modifier-error-test.gjs': `
276+
import { module, test } from 'qunit';
277+
import { render, setupOnerror, resetOnerror } from '@ember/test-helpers';
278+
import { setupRenderingTest } from 'ember-qunit';
279+
import { on } from '@ember/modifier';
280+
281+
module('on modifier | error handling', function (hooks) {
282+
setupRenderingTest(hooks);
283+
284+
hooks.afterEach(function () {
285+
resetOnerror();
286+
});
287+
288+
test('throws helpful error when callback is undefined', async function (assert) {
289+
assert.expect(1);
290+
const noop = undefined;
291+
setupOnerror((error) => {
292+
assert.true(
293+
/You must pass a function as the second argument to the \`on\` modifier/.test(error.message),
294+
'Expected helpful error message, got: ' + error.message
295+
);
296+
});
297+
await render(<template><div {{on "click" noop}}>Click</div></template>);
298+
});
299+
300+
test('throws helpful error when callback is null', async function (assert) {
301+
assert.expect(1);
302+
const noop = null;
303+
setupOnerror((error) => {
304+
assert.true(
305+
/You must pass a function as the second argument to the \`on\` modifier/.test(error.message),
306+
'Expected helpful error message, got: ' + error.message
307+
);
308+
});
309+
await render(<template><div {{on "click" noop}}>Click</div></template>);
272310
});
273311
});
274312
`,

0 commit comments

Comments
 (0)