From 896e8dbbebbdee440762fba9323bd320165ef7bf Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 20 Apr 2017 15:23:05 -0700 Subject: [PATCH] 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 --- lib/internal/errors.js | 11 +++++++++-- test/parallel/test-internal-errors.js | 6 ------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index c2d3bc71e9fd4e..fbaa06170d34c8 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -6,7 +6,6 @@ // value statically and permanently identifies the error. While the error // message may change, the code should not. -const assert = require('assert'); const kCode = Symbol('code'); const messages = new Map(); @@ -17,6 +16,13 @@ function lazyUtil() { return util; } +var assert; +function lazyAssert() { + if (!assert) + assert = require('assert'); + return assert; +} + function makeNodeError(Base) { return class NodeError extends Base { constructor(key, ...args) { @@ -36,6 +42,7 @@ function makeNodeError(Base) { } function message(key, args) { + const assert = lazyAssert(); assert.strictEqual(typeof key, 'string'); const util = lazyUtil(); const msg = messages.get(key); @@ -54,7 +61,6 @@ function message(key, args) { // Utility function for registering the error codes. Only used here. Exported // *only* to allow for testing. function E(sym, val) { - assert(messages.has(sym) === false, `Error symbol: ${sym} was already used.`); 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... function invalidArgType(name, expected, actual) { + const assert = lazyAssert(); assert(name, 'name is required'); var msg = `The "${name}" argument must be ${oneOf(expected, 'type')}`; if (arguments.length >= 3) { diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index dd4f02c1f758bf..4157ca0008ebe8 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -125,12 +125,6 @@ assert.throws(() => { 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\.$/ -); - // // Test ERR_INVALID_ARG_TYPE assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b']), 'The "a" argument must be of type b');