Skip to content

Commit

Permalink
More user friendly errors in layout.js (#26448)
Browse files Browse the repository at this point in the history
* More user friendly errors in layout.js.

* Include element in a few more asserts.

* Fix tests.
  • Loading branch information
William Chou committed Jan 24, 2020
1 parent 0432d1d commit a91a37c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
40 changes: 28 additions & 12 deletions src/layout.js
Expand Up @@ -375,15 +375,30 @@ export function applyStaticLayout(element) {

// Input layout attributes.
const inputLayout = layoutAttr ? parseLayout(layoutAttr) : null;
userAssert(inputLayout !== undefined, 'Unknown layout: %s', layoutAttr);
userAssert(
inputLayout !== undefined,
'Invalid "layout" value: %s, %s',
layoutAttr,
element
);
/** @const {string|null|undefined} */
const inputWidth =
widthAttr && widthAttr != 'auto' ? parseLength(widthAttr) : widthAttr;
userAssert(inputWidth !== undefined, 'Invalid width value: %s', widthAttr);
userAssert(
inputWidth !== undefined,
'Invalid "width" value: %s, %s',
widthAttr,
element
);
/** @const {string|null|undefined} */
const inputHeight =
heightAttr && heightAttr != 'fluid' ? parseLength(heightAttr) : heightAttr;
userAssert(inputHeight !== undefined, 'Invalid height value: %s', heightAttr);
userAssert(
inputHeight !== undefined,
'Invalid "height" value: %s, %s',
heightAttr,
element
);

// Effective layout attributes. These are effectively constants.
let width;
Expand Down Expand Up @@ -433,14 +448,13 @@ export function applyStaticLayout(element) {
layout == Layout.RESPONSIVE ||
layout == Layout.INTRINSIC
) {
userAssert(height, 'Expected height to be available: %s', heightAttr);
userAssert(height, 'The "height" attribute is missing: %s', element);
}
if (layout == Layout.FIXED_HEIGHT) {
userAssert(
!width || width == 'auto',
'Expected width to be either absent or equal "auto" ' +
'for fixed-height layout: %s',
widthAttr
'The "width" attribute must be missing or "auto": %s',
element
);
}
if (
Expand All @@ -450,22 +464,24 @@ export function applyStaticLayout(element) {
) {
userAssert(
width && width != 'auto',
'Expected width to be available and not equal to "auto": %s',
widthAttr
'The "width" attribute must be present and not "auto": %s',
element
);
}

if (layout == Layout.RESPONSIVE || layout == Layout.INTRINSIC) {
userAssert(
getLengthUnits(width) == getLengthUnits(height),
'Length units should be the same for width and height: %s, %s',
'Length units should be the same for "width" and "height": %s, %s, %s',
widthAttr,
heightAttr
heightAttr,
element
);
} else {
userAssert(
heightsAttr === null,
'Unexpected "heights" attribute for none-responsive layout'
'"heights" attribute must be missing: %s',
element
);
}

Expand Down
12 changes: 6 additions & 6 deletions test/unit/test-layout.js
Expand Up @@ -283,7 +283,7 @@ describe('Layout', () => {
div.setAttribute('layout', 'fixed');
allowConsoleError(() => {
expect(() => applyStaticLayout(div)).to.throw(
/Expected height to be available/
/The "height" attribute is missing/
);
});
});
Expand Down Expand Up @@ -318,7 +318,7 @@ describe('Layout', () => {
allowConsoleError(() => {
expect(function() {
applyStaticLayout(div);
}).to.throw(/Expected width to be either absent or equal "auto"/);
}).to.throw(/The "width" attribute must be missing or "auto"/);
});
});

Expand All @@ -341,7 +341,7 @@ describe('Layout', () => {
div.setAttribute('layout', 'fixed-height');
allowConsoleError(() => {
expect(() => applyStaticLayout(div)).to.throw(
/Expected height to be available/
/The "height" attribute is missing/
);
});
});
Expand Down Expand Up @@ -466,7 +466,7 @@ describe('Layout', () => {
allowConsoleError(() => {
expect(function() {
applyStaticLayout(div);
}).to.throw(/Unknown layout: foo/);
}).to.throw(/Invalid "layout" value: foo/);
});
});

Expand Down Expand Up @@ -535,7 +535,7 @@ describe('Layout', () => {
allowConsoleError(() => {
expect(() => {
applyStaticLayout(pixel);
}).to.throw(/Invalid width value/);
}).to.throw(/Invalid "width" value: X/);
});
});

Expand All @@ -547,7 +547,7 @@ describe('Layout', () => {
allowConsoleError(() => {
expect(() => {
applyStaticLayout(pixel);
}).to.throw(/Invalid height value/);
}).to.throw(/Invalid "height" value: X/);
});
});

Expand Down

0 comments on commit a91a37c

Please sign in to comment.