Skip to content

Commit f0b7025

Browse files
committed
errors: use lazy assert to avoid issues on startup
Use of assert must be lazy to allow errors to be used early before the process is completely set up PR-URL: #11300 Ref: #11273 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 4271254 commit f0b7025

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

lib/internal/errors.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// value statically and permanently identifies the error. While the error
77
// message may change, the code should not.
88

9-
const assert = require('assert');
109
const kCode = Symbol('code');
1110
const messages = new Map();
1211

@@ -17,6 +16,13 @@ function lazyUtil() {
1716
return util;
1817
}
1918

19+
var assert;
20+
function lazyAssert() {
21+
if (!assert)
22+
assert = require('assert');
23+
return assert;
24+
}
25+
2026
function makeNodeError(Base) {
2127
return class NodeError extends Base {
2228
constructor(key, ...args) {
@@ -36,6 +42,7 @@ function makeNodeError(Base) {
3642
}
3743

3844
function message(key, args) {
45+
const assert = lazyAssert();
3946
assert.strictEqual(typeof key, 'string');
4047
const util = lazyUtil();
4148
const msg = messages.get(key);
@@ -54,7 +61,6 @@ function message(key, args) {
5461
// Utility function for registering the error codes. Only used here. Exported
5562
// *only* to allow for testing.
5663
function E(sym, val) {
57-
assert(messages.has(sym) === false, `Error symbol: ${sym} was already used.`);
5864
messages.set(sym, typeof val === 'function' ? val : String(val));
5965
}
6066

@@ -99,6 +105,7 @@ E('ERR_UNKNOWN_BUILTIN_MODULE', (id) => `No such built-in module: ${id}`);
99105
// Add new errors from here...
100106

101107
function invalidArgType(name, expected, actual) {
108+
const assert = lazyAssert();
102109
assert(name, 'name is required');
103110
var msg = `The "${name}" argument must be ${oneOf(expected, 'type')}`;
104111
if (arguments.length >= 3) {

test/parallel/test-internal-errors.js

-6
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,6 @@ assert.throws(() => {
125125
message: /^Error for testing 2/ }));
126126
}, /AssertionError: .+ does not match \S/);
127127

128-
assert.doesNotThrow(() => errors.E('TEST_ERROR_USED_SYMBOL'));
129-
assert.throws(
130-
() => errors.E('TEST_ERROR_USED_SYMBOL'),
131-
/^AssertionError: Error symbol: TEST_ERROR_USED_SYMBOL was already used\.$/
132-
);
133-
134128
// // Test ERR_INVALID_ARG_TYPE
135129
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b']),
136130
'The "a" argument must be of type b');

0 commit comments

Comments
 (0)