Skip to content

Commit 2240254

Browse files
committed
errors: eliminate circular dependency on assert
1 parent ea2e636 commit 2240254

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/internal/errors.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const kCode = Symbol('code');
1212
const messages = new Map();
1313

1414
// Lazily loaded
15-
var assert = null;
1615
var util = null;
1716

1817
function makeNodeError(Base) {
@@ -55,9 +54,20 @@ class AssertionError extends Error {
5554
}
5655
}
5756

57+
// This is defined here instead of using the assert module to avoid a
58+
// circular dependency. The effect is largely the same.
59+
function assert(condition, message) {
60+
if (!condition) {
61+
throw new AssertionError({
62+
message,
63+
actual: false,
64+
expected: true,
65+
operator: '=='
66+
});
67+
}
68+
}
69+
5870
function message(key, args) {
59-
if (assert === null) assert = require('assert');
60-
assert.strictEqual(typeof key, 'string');
6171
const msg = messages.get(key);
6272
assert(msg, `An invalid error message key was used: ${key}.`);
6373
let fmt;
@@ -188,7 +198,7 @@ E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
188198
E('ERR_INVALID_ARG_TYPE', invalidArgType);
189199
E('ERR_INVALID_ARRAY_LENGTH',
190200
(name, len, actual) => {
191-
assert.strictEqual(typeof actual, 'number');
201+
assert(typeof actual === 'number', 'actual must be a number');
192202
return `The array "${name}" (length ${actual}) must be of length ${len}.`;
193203
});
194204
E('ERR_INVALID_ASYNC_ID', (type, id) => `Invalid ${type} value: ${id}`);

0 commit comments

Comments
 (0)