Skip to content
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
25 changes: 20 additions & 5 deletions packages/blaze/materializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,24 @@ const materializeDOMInner = function (htmljs, intoArray, parentView, workStack)

const isPromiseLike = x => !!x && typeof x.then === 'function';

// Thrown in place of a rejection that `waitForAllAttributes` has reported, so
// that the end of the chain reports only the errors nobody reported yet.
const alreadyReported = new Error('attribute rejection already reported');

function reportRejection(error) {
Blaze._reportException(error);
throw alreadyReported;
}

function then(maybePromise, fn) {
if (isPromiseLike(maybePromise)) {
maybePromise.then(fn, Blaze._reportException)
maybePromise.then(fn, error => {
// An error thrown while reading the resolved attributes, for example
// by a getter, rejects the chain without having been reported.
if (error !== alreadyReported) {
Blaze._reportException(error);
}
});
} else {
fn(maybePromise);
}
Expand All @@ -117,7 +132,7 @@ function waitForAllAttributes(attrs) {

// Singular async attributes, e.g., `<img {{x}}>`.
if (isPromiseLike(attrs)) {
return attrs.then(waitForAllAttributes, Blaze._reportExceptionAndThrow);
return attrs.then(waitForAllAttributes, reportRejection);
}

// Singular sync attributes, with potentially async properties.
Expand All @@ -126,21 +141,21 @@ function waitForAllAttributes(attrs) {
if (isPromiseLike(value)) {
promises.push(value.then(value => {
attrs[key] = value;
}, Blaze._reportExceptionAndThrow));
}, reportRejection));
} else if (Array.isArray(value)) {
value.forEach((element, index) => {
if (isPromiseLike(element)) {
promises.push(element.then(element => {
value[index] = element;
}, Blaze._reportExceptionAndThrow));
}, reportRejection));
}
});
}
}

// If any of the properties were async, lift the `Promise`.
return promises.length
? Promise.all(promises).then(() => attrs, Blaze._reportExceptionAndThrow)
? Promise.all(promises).then(() => attrs)
: attrs;
}

Expand Down
29 changes: 29 additions & 0 deletions packages/spacebars-tests/async_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,32 @@ asyncSuite('state2nested', [
['resolved rejected', { x: resolved, y: rejected }, '1 a1 b1 ab1', '2 a3 b2 ab2 ab3'],
['resolved resolved', { x: resolved, y: resolved }, '1 a1 b1 ab1', '3 a3 b3 ab3'],
]);

// A promise that rejects in an attribute is reported once, where it rejects,
// and not again by each enclosing layer of the attribute promise chain.
for (const [templateName, testName, makeHelpers, expectedMessages] of [
['attribute', 'rejected value reported once',
() => ({ x: Promise.reject(new Error('class')) }), ['class']],
['attributes', 'rejected attributes object reported once',
() => ({ x: Promise.reject(new Error('attrs')) }), ['attrs']],
['attributes', 'rejected nested value reported once',
() => ({ x: value({ class: Promise.reject(new Error('class')) }) }), ['class']],
['attributes_double', 'two rejected values reported once each',
() => ({ x: value({ class: Promise.reject(new Error('x')) }), y: value({ id: Promise.reject(new Error('y')) }) }), ['x', 'y']],
['attributes', 'error thrown while reading the resolved attributes reported once',
() => ({ x: Promise.resolve({ get class() { throw new Error('getter'); } }) }), ['getter']],
]) {
asyncTest(templateName, testName, async (test, template, render) => {
const reportedMessages = [];
const originalReportException = Blaze._reportException;
Blaze._reportException = (error) => reportedMessages.push(error.message);
try {
template.helpers(makeHelpers());
render();
await new Promise((resolve) => setTimeout(resolve, 50));
} finally {
Blaze._reportException = originalReportException;
}
test.equal(reportedMessages.sort(), expectedMessages);
});
}
Loading