|
|
@ -12,7 +12,6 @@ const kCode = Symbol('code'); |
|
|
|
const messages = new Map(); |
|
|
|
|
|
|
|
// Lazily loaded
|
|
|
|
var assert = null; |
|
|
|
var util = null; |
|
|
|
|
|
|
|
function makeNodeError(Base) { |
|
|
@ -55,11 +54,22 @@ class AssertionError extends Error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// This is defined here instead of using the assert module to avoid a
|
|
|
|
// circular dependency. The effect is largely the same.
|
|
|
|
function internalAssert(condition, message) { |
|
|
|
if (!condition) { |
|
|
|
throw new AssertionError({ |
|
|
|
message, |
|
|
|
actual: false, |
|
|
|
expected: true, |
|
|
|
operator: '==' |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function message(key, args) { |
|
|
|
if (assert === null) assert = require('assert'); |
|
|
|
assert.strictEqual(typeof key, 'string'); |
|
|
|
const msg = messages.get(key); |
|
|
|
assert(msg, `An invalid error message key was used: ${key}.`); |
|
|
|
internalAssert(msg, `An invalid error message key was used: ${key}.`); |
|
|
|
let fmt; |
|
|
|
if (typeof msg === 'function') { |
|
|
|
fmt = msg; |
|
|
@ -188,7 +198,7 @@ E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range'); |
|
|
|
E('ERR_INVALID_ARG_TYPE', invalidArgType); |
|
|
|
E('ERR_INVALID_ARRAY_LENGTH', |
|
|
|
(name, len, actual) => { |
|
|
|
assert.strictEqual(typeof actual, 'number'); |
|
|
|
internalAssert(typeof actual === 'number', 'actual must be a number'); |
|
|
|
return `The array "${name}" (length ${actual}) must be of length ${len}.`; |
|
|
|
}); |
|
|
|
E('ERR_INVALID_ASYNC_ID', (type, id) => `Invalid ${type} value: ${id}`); |
|
|
@ -280,7 +290,7 @@ E('ERR_VALID_PERFORMANCE_ENTRY_TYPE', |
|
|
|
E('ERR_VALUE_OUT_OF_RANGE', 'The value of "%s" must be %s. Received "%s"'); |
|
|
|
|
|
|
|
function invalidArgType(name, expected, actual) { |
|
|
|
assert(name, 'name is required'); |
|
|
|
internalAssert(name, 'name is required'); |
|
|
|
|
|
|
|
// determiner: 'must be' or 'must not be'
|
|
|
|
let determiner; |
|
|
@ -311,7 +321,7 @@ function invalidArgType(name, expected, actual) { |
|
|
|
} |
|
|
|
|
|
|
|
function missingArgs(...args) { |
|
|
|
assert(args.length > 0, 'At least one arg needs to be specified'); |
|
|
|
internalAssert(args.length > 0, 'At least one arg needs to be specified'); |
|
|
|
let msg = 'The '; |
|
|
|
const len = args.length; |
|
|
|
args = args.map((a) => `"${a}"`); |
|
|
@ -331,11 +341,12 @@ function missingArgs(...args) { |
|
|
|
} |
|
|
|
|
|
|
|
function oneOf(expected, thing) { |
|
|
|
assert(expected, 'expected is required'); |
|
|
|
assert(typeof thing === 'string', 'thing is required'); |
|
|
|
internalAssert(expected, 'expected is required'); |
|
|
|
internalAssert(typeof thing === 'string', 'thing is required'); |
|
|
|
if (Array.isArray(expected)) { |
|
|
|
const len = expected.length; |
|
|
|
assert(len > 0, 'At least one expected value needs to be specified'); |
|
|
|
internalAssert(len > 0, |
|
|
|
'At least one expected value needs to be specified'); |
|
|
|
expected = expected.map((i) => String(i)); |
|
|
|
if (len > 2) { |
|
|
|
return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or ` + |
|
|
|