Skip to content

Commit 1699a4f

Browse files
authored
Merge pull request #14554 from nathanhammond/improve-link-to-error
[BUGFIX beta] Improve error for improper invocations of link-to.
2 parents 6c73eb3 + 14164a2 commit 1699a4f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/ember-glimmer/lib/components/link-to.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ import {
313313
deprecate,
314314
get,
315315
computed,
316-
flaggedInstrument
316+
flaggedInstrument,
317+
runInDebug
317318
} from 'ember-metal';
318319
import {
319320
deprecatingAlias,
@@ -711,6 +712,26 @@ const LinkComponent = EmberComponent.extend({
711712

712713
let routing = get(this, '_routing');
713714
let queryParams = get(this, 'queryParams.values');
715+
716+
runInDebug(() => {
717+
/*
718+
* Unfortunately, to get decent error messages, we need to do this.
719+
* In some future state we should be able to use a "feature flag"
720+
* which allows us to strip this without needing to call it twice.
721+
*
722+
* if (isDebugBuild()) {
723+
* // Do the useful debug thing, probably including try/catch.
724+
* } else {
725+
* // Do the performant thing.
726+
* }
727+
*/
728+
try {
729+
routing.generateURL(qualifiedRouteName, models, queryParams);
730+
} catch (e) {
731+
assert('You attempted to define a `{{link-to "' + qualifiedRouteName + '"}}` but did not pass the parameters required for generating its dynamic segments. ' + e.message);
732+
}
733+
});
734+
714735
return routing.generateURL(qualifiedRouteName, models, queryParams);
715736
}),
716737

packages/ember/tests/helpers/link_to_test.js

+14
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,20 @@ QUnit.test('the {{link-to}} helper does not call preventDefault if `preventDefau
12491249
equal(event.isDefaultPrevented(), false, 'should not preventDefault');
12501250
});
12511251

1252+
QUnit.test('the {{link-to}} helper throws a useful error if you invoke it wrong', function() {
1253+
expect(1);
1254+
1255+
setTemplate('application', compile("{{#link-to 'post'}}Post{{/link-to}}"));
1256+
1257+
Router.map(function() {
1258+
this.route('post', { path: 'post/:post_id' });
1259+
});
1260+
1261+
QUnit.throws(function() {
1262+
bootApplication();
1263+
}, /(You attempted to define a `\{\{link-to "post"\}\}` but did not pass the parameters required for generating its dynamic segments.|You must provide param `post_id` to `generate`)/);
1264+
});
1265+
12521266
QUnit.test('the {{link-to}} helper does not throw an error if its route has exited', function() {
12531267
expect(0);
12541268

0 commit comments

Comments
 (0)