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: https://github.com/nodejs/node/pull/11300 Ref: https://github.com/nodejs/node/issues/11273 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
parent
427125491f
commit
f0b702555a
@ -6,7 +6,6 @@
|
|||||||
// value statically and permanently identifies the error. While the error
|
// value statically and permanently identifies the error. While the error
|
||||||
// message may change, the code should not.
|
// message may change, the code should not.
|
||||||
|
|
||||||
const assert = require('assert');
|
|
||||||
const kCode = Symbol('code');
|
const kCode = Symbol('code');
|
||||||
const messages = new Map();
|
const messages = new Map();
|
||||||
|
|
||||||
@ -17,6 +16,13 @@ function lazyUtil() {
|
|||||||
return util;
|
return util;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var assert;
|
||||||
|
function lazyAssert() {
|
||||||
|
if (!assert)
|
||||||
|
assert = require('assert');
|
||||||
|
return assert;
|
||||||
|
}
|
||||||
|
|
||||||
function makeNodeError(Base) {
|
function makeNodeError(Base) {
|
||||||
return class NodeError extends Base {
|
return class NodeError extends Base {
|
||||||
constructor(key, ...args) {
|
constructor(key, ...args) {
|
||||||
@ -36,6 +42,7 @@ function makeNodeError(Base) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function message(key, args) {
|
function message(key, args) {
|
||||||
|
const assert = lazyAssert();
|
||||||
assert.strictEqual(typeof key, 'string');
|
assert.strictEqual(typeof key, 'string');
|
||||||
const util = lazyUtil();
|
const util = lazyUtil();
|
||||||
const msg = messages.get(key);
|
const msg = messages.get(key);
|
||||||
@ -54,7 +61,6 @@ function message(key, args) {
|
|||||||
// Utility function for registering the error codes. Only used here. Exported
|
// Utility function for registering the error codes. Only used here. Exported
|
||||||
// *only* to allow for testing.
|
// *only* to allow for testing.
|
||||||
function E(sym, val) {
|
function E(sym, val) {
|
||||||
assert(messages.has(sym) === false, `Error symbol: ${sym} was already used.`);
|
|
||||||
messages.set(sym, typeof val === 'function' ? val : String(val));
|
messages.set(sym, typeof val === 'function' ? val : String(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,6 +105,7 @@ E('ERR_UNKNOWN_BUILTIN_MODULE', (id) => `No such built-in module: ${id}`);
|
|||||||
// Add new errors from here...
|
// Add new errors from here...
|
||||||
|
|
||||||
function invalidArgType(name, expected, actual) {
|
function invalidArgType(name, expected, actual) {
|
||||||
|
const assert = lazyAssert();
|
||||||
assert(name, 'name is required');
|
assert(name, 'name is required');
|
||||||
var msg = `The "${name}" argument must be ${oneOf(expected, 'type')}`;
|
var msg = `The "${name}" argument must be ${oneOf(expected, 'type')}`;
|
||||||
if (arguments.length >= 3) {
|
if (arguments.length >= 3) {
|
||||||
|
@ -125,12 +125,6 @@ assert.throws(() => {
|
|||||||
message: /^Error for testing 2/ }));
|
message: /^Error for testing 2/ }));
|
||||||
}, /AssertionError: .+ does not match \S/);
|
}, /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\.$/
|
|
||||||
);
|
|
||||||
|
|
||||||
// // Test ERR_INVALID_ARG_TYPE
|
// // Test ERR_INVALID_ARG_TYPE
|
||||||
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b']),
|
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b']),
|
||||||
'The "a" argument must be of type b');
|
'The "a" argument must be of type b');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user