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

♻️ Attach firstElement and messageArray to assert errors #32887

Merged
merged 3 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export class UserError extends Error {

/**
* Throws a provided error if the second argument isn't trueish.
*
* Supports argument substitution into the message via %s placeholders.
*
* Throws an error object that has two extra properties:
* - associatedElement: This is the first element provided in the var args.
* It can be used for improved display of error messages.
* - messageArray: The elements of the substituted message as non-stringified
* elements in an array. When e.g. passed to console.error this yields
* native displays of things like HTML elements.
* @param {Object} errorCls
* @param {T} shouldBeTruthy
* @param {string} opt_message
Expand All @@ -65,16 +74,25 @@ function assertion(errorCls, shouldBeTruthy, opt_message, var_args) {
return shouldBeTruthy;
}

// Skip the first 3 arguments to isolate format params
const messageArgs = Array.prototype.slice.call(arguments, 3);
// Substitute provided values into format string in message
const message = Array.prototype.slice
// Skip the first 3 arguments to isolate format params
.call(arguments, 3)
.reduce(
(msg, subValue) => msg.replace('%s', subValue),
opt_message || 'Assertion failed'
);
const message = messageArgs.reduce(
(msg, subValue) => msg.replace('%s', subValue),
opt_message || 'Assertion failed'
);
const error = new errorCls(message);
error.messageArray = messageArgs;

// If an element is provided, add it to the error object
for (let i = 0; i < messageArgs.length; ++i) {
if (messageArgs[i]?.tagName) {
rcebulko marked this conversation as resolved.
Show resolved Hide resolved
error.associatedElement = messageArgs[i];
break;
}
}

throw new errorCls(message);
throw error;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/unit/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ describes.sandboxed('assertions', {}, () => {
`1 a 2 b 3${USER_ERROR_SENTINEL}`
);
});

it('should add element and message info', () => {
const div = document.createElement('div');
let error;
try {
devAssert(false, '%s a %s b %s', div, 2, 3);
} catch (e) {
error = e;
}

expect(error.associatedElement).to.equal(div);
expect(error.messageArray).to.equal([div, 2, 3]);
});
});