Permalink
Browse files

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 <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information...
jasnell committed Apr 20, 2017
1 parent 4271254 commit f0b702555aacc814da28a018277d56e5f79f10d1
Showing with 9 additions and 8 deletions.
  1. +9 −2 lib/internal/errors.js
  2. +0 −6 test/parallel/test-internal-errors.js
View
@@ -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) {
@@ -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');

0 comments on commit f0b7025

Please sign in to comment.