Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX beta] Improve error for improper invocations of link-to. #14554

Merged
merged 1 commit into from
Oct 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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