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

errors: add duplicate symbol checking in E() and increase coverage #11829

Closed
Closed
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
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function message(key, args) {
// Utility function for registering the error codes. Only used here. Exported
// *only* to allow for testing.
function E(sym, val) {
const assert = lazyAssert();
assert(messages.has(sym) === false, `Error symbol: ${sym} was already used.`);
messages.set(sym, typeof val === 'function' ? val : String(val));
}

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-internal-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const err1 = new errors.Error('TEST_ERROR_1', 'test');
const err2 = new errors.TypeError('TEST_ERROR_1', 'test');
const err3 = new errors.RangeError('TEST_ERROR_1', 'test');
const err4 = new errors.Error('TEST_ERROR_2', 'abc', 'xyz');
const err5 = new errors.Error('TEST_ERROR_1');

assert(err1 instanceof Error);
assert.strictEqual(err1.name, 'Error[TEST_ERROR_1]');
Expand All @@ -33,6 +34,11 @@ assert.strictEqual(err4.name, 'Error[TEST_ERROR_2]');
assert.strictEqual(err4.message, 'abc xyz');
assert.strictEqual(err4.code, 'TEST_ERROR_2');

assert(err5 instanceof Error);
assert.strictEqual(err5.name, 'Error[TEST_ERROR_1]');
assert.strictEqual(err5.message, 'Error for testing purposes: %s');
assert.strictEqual(err5.code, 'TEST_ERROR_1');

assert.throws(
() => new errors.Error('TEST_FOO_KEY'),
/^AssertionError: An invalid error message key was used: TEST_FOO_KEY.$/);
Expand Down Expand Up @@ -118,3 +124,9 @@ assert.throws(() => {
type: TypeError,
message: /^Error for testing 2/ }));
}, /AssertionError: .+ does not match \S/);

assert.doesNotThrow(() => errors.E('TEST_ERROR_USED_SYMBOL'));
assert.throws(
() => errors.E('TEST_ERROR_USED_SYMBOL'),
/^AssertionError: Error symbol: TEST_ERROR_USED_SYMBOL was already used\.$/
);