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

fix: throw a descriptive error when an undefined attribute is used for a... #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion hsp/rt/cptcomponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ exports.$CptComponent = {
for (var i = 0, sz = this.atts.length; sz > i; i++) {
att = atts[i];
nm = att.name;
if (this.ctlAttributes[nm].type!=="template") {
if (!this.ctlAttributes || !this.ctlAttributes[nm]) {
throw new Error('The attribute "' + nm + '" was used but the component doesn\'t define this attribute.');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The impacted component name is in this.info, so to be in line with other error messages, this one could be changed to:
throw new Error(this.info + ' The attribute "' + etc

} else if (this.ctlAttributes[nm].type!=="template") {
attributes[nm]=att.getValue(eh, pvs, null);
}
}
Expand Down
51 changes: 51 additions & 0 deletions test/rt/cpterrors.spec.hsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var klass=require("hsp/klass"),
ht=require("hsp/utils/hashtester");

var FooNoAttrsCtrl=klass({
});

var FooEmptyAttrsCtrl=klass({
attributes: {
}
});

{template fooNoAttrs using ctrl:FooNoAttrsCtrl}
<div>foo</div>
{/template}

{template fooEmptyAttrs using ctrl:FooEmptyAttrsCtrl}
<div>foo</div>
{/template}

{template test1}
<#fooNoAttrs foo="bar"></#fooNoAttrs>
{/template}

{template test2}
<#fooEmptyAttrs foo="bar"></#fooEmptyAttrs>
{/template}

describe('error reporting for custom components', function() {

var h;
beforeEach(function() {
h=ht.newTestContext();
});

it('attribute used for a component without attributes', function() {
expect(function() {
test1().render(h.container);
}).to.throwException(/The attribute "foo" was used but the component doesn't define this attribute./);
});

it('attribute used for a component without attributes', function() {
expect(function() {
test2().render(h.container);
} ).to.throwException(/The attribute "foo" was used but the component doesn't define this attribute./);
});

afterEach(function(){
h.$dispose();
});

});