Skip to content

Commit

Permalink
[BUGFIX beta] Improve the error message for improper invocations of l…
Browse files Browse the repository at this point in the history
…ink-to.
  • Loading branch information
nathanhammond committed Oct 30, 2016
1 parent 86f0a30 commit 14164a2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/ember-glimmer/lib/components/link-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ import {
deprecate,
get,
computed,
flaggedInstrument
flaggedInstrument,
runInDebug
} from 'ember-metal';
import {
deprecatingAlias,
Expand Down Expand Up @@ -711,6 +712,26 @@ const LinkComponent = EmberComponent.extend({

let routing = get(this, '_routing');
let queryParams = get(this, 'queryParams.values');

runInDebug(() => {
/*
* Unfortunately, to get decent error messages, we need to do this.
* In some future state we should be able to use a "feature flag"
* which allows us to strip this without needing to call it twice.
*
* if (isDebugBuild()) {
* // Do the useful debug thing, probably including try/catch.
* } else {
* // Do the performant thing.
* }
*/
try {
routing.generateURL(qualifiedRouteName, models, queryParams);
} catch (e) {
assert('You attempted to define a `{{link-to "' + qualifiedRouteName + '"}}` but did not pass the parameters required for generating its dynamic segments. ' + e.message);
}
});

return routing.generateURL(qualifiedRouteName, models, queryParams);
}),

Expand Down
14 changes: 14 additions & 0 deletions packages/ember/tests/helpers/link_to_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,20 @@ QUnit.test('the {{link-to}} helper does not call preventDefault if `preventDefau
equal(event.isDefaultPrevented(), false, 'should not preventDefault');
});

QUnit.test('the {{link-to}} helper throws a useful error if you invoke it wrong', function() {
expect(1);

setTemplate('application', compile("{{#link-to 'post'}}Post{{/link-to}}"));

Router.map(function() {
this.route('post', { path: 'post/:post_id' });
});

QUnit.throws(function() {
bootApplication();
}, /(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`)/);
});

QUnit.test('the {{link-to}} helper does not throw an error if its route has exited', function() {
expect(0);

Expand Down

0 comments on commit 14164a2

Please sign in to comment.