Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions lib/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,9 @@ class Component extends WebComponent {
// Ensure attr type is valid
const attrType = attrSchema.type;
if (!ATTR_TYPE_DEFAULTS.hasOwnProperty(attrType)) {
this._logError(`attrsSchema definition error in`, this);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure _logError would still be useful in this situation since it will always throw on initialization

Copy link
Member

Choose a reason for hiding this comment

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

👍

throw new Error(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new Error(
this._logError(`attrsSchema error in`, this);
throw new Error(

`Invalid type: ${attrType} for attr: ${attr} in attrsSchema. ` +
`Invalid type: ${attrType} for attr: ${attr} in ${this.constructor.name} attrsSchema. ` +
`Only (${Object.keys(ATTR_TYPE_DEFAULTS)
.map((v) => `'${v}'`)
.join(` | `)}) is valid.`,
Expand Down Expand Up @@ -628,8 +629,9 @@ class Component extends WebComponent {
const enumSet = attrSchema.enumSet;

if (enumSet && !enumSet.has(attrValue)) {
this._logError(`attrsSchema value error in`, this);
throw new Error(
`Invalid value: '${attrValue}' for attr: ${attr}. ` +
`Invalid value: '${attrValue}' for attr: ${attr} in element ${this.tagName}. ` +
`Only (${Array.from(enumSet)
.map((v) => `'${v}'`)
.join(` | `)}) is valid.`,
Expand Down
7 changes: 4 additions & 3 deletions test/server/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,17 @@ describe(`Server-side component renderer`, function () {
});

it(`throws error for invalid value in an enum attr`, function () {
const el = new AttrsReflectionApp();
const el = document.createElement(`attrs-reflection-app`);
el.connectedCallback();

expect(() => el.setAttribute(`str-attr`, `boo!`)).to.throw(
`Invalid value: 'boo!' for attr: str-attr. Only ('hello' | 'world' | '💩🤒🤢☠️ -> 👻🎉💐🎊😱😍') is valid.`,
`Invalid value: 'boo!' for attr: str-attr in element attrs-reflection-app. Only ('hello' | 'world' | '💩🤒🤢☠️ -> 👻🎉💐🎊😱😍') is valid.`,
);
});

it(`throws error if there is a malformed attrsSchema type`, function () {
expect(() => new BadAttrsSchemaApp()).to.throw(
`Invalid type: bool for attr: bad-attr in attrsSchema. Only ('string' | 'boolean' | 'number' | 'json') is valid.`,
`Invalid type: bool for attr: bad-attr in BadAttrsSchemaApp attrsSchema. Only ('string' | 'boolean' | 'number' | 'json') is valid.`,
);
});
});