mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 15:41:06 +00:00

If lines gets skipped, they are marked with three dots. This makes sure they are better visualized to distinguish them from everything else. PR-URL: https://github.com/nodejs/node/pull/20315 Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
1141 lines
39 KiB
JavaScript
1141 lines
39 KiB
JavaScript
/* eslint node-core/documented-errors: "error" */
|
|
/* eslint node-core/alphabetize-errors: "error" */
|
|
/* eslint node-core/prefer-util-format-errors: "error" */
|
|
|
|
'use strict';
|
|
|
|
// The whole point behind this internal module is to allow Node.js to no
|
|
// longer be forced to treat every error message change as a semver-major
|
|
// change. The NodeError classes here all expose a `code` property whose
|
|
// value statically and permanently identifies the error. While the error
|
|
// message may change, the code should not.
|
|
|
|
const kCode = Symbol('code');
|
|
const kInfo = Symbol('info');
|
|
const messages = new Map();
|
|
const codes = {};
|
|
|
|
let blue = '';
|
|
let green = '';
|
|
let red = '';
|
|
let white = '';
|
|
|
|
const READABLE_OPERATOR = {
|
|
deepStrictEqual: 'Input A expected to strictly deep-equal input B',
|
|
notDeepStrictEqual: 'Input A expected to strictly not deep-equal input B',
|
|
strictEqual: 'Input A expected to strictly equal input B',
|
|
notStrictEqual: 'Input A expected to strictly not equal input B'
|
|
};
|
|
|
|
const {
|
|
errmap,
|
|
UV_EAI_MEMORY,
|
|
UV_EAI_NODATA,
|
|
UV_EAI_NONAME
|
|
} = process.binding('uv');
|
|
const { kMaxLength } = process.binding('buffer');
|
|
const { defineProperty } = Object;
|
|
|
|
// Lazily loaded
|
|
var util;
|
|
var buffer;
|
|
|
|
var internalUtil = null;
|
|
function lazyInternalUtil() {
|
|
if (!internalUtil) {
|
|
internalUtil = require('internal/util');
|
|
}
|
|
return internalUtil;
|
|
}
|
|
|
|
function copyError(source) {
|
|
const keys = Object.keys(source);
|
|
const target = Object.create(Object.getPrototypeOf(source));
|
|
for (const key of keys) {
|
|
target[key] = source[key];
|
|
}
|
|
Object.defineProperty(target, 'message', { value: source.message });
|
|
return target;
|
|
}
|
|
|
|
function inspectValue(val) {
|
|
// The util.inspect default values could be changed. This makes sure the
|
|
// error messages contain the necessary information nevertheless.
|
|
return util.inspect(
|
|
val,
|
|
{
|
|
compact: false,
|
|
customInspect: false,
|
|
depth: 1000,
|
|
maxArrayLength: Infinity,
|
|
// Assert compares only enumerable properties (with a few exceptions).
|
|
showHidden: false,
|
|
// Having a long line as error is better than wrapping the line for
|
|
// comparison.
|
|
breakLength: Infinity,
|
|
// Assert does not detect proxies currently.
|
|
showProxy: false
|
|
}
|
|
).split('\n');
|
|
}
|
|
|
|
function sysErrorMessage(prefix, ctx) {
|
|
let message = `${prefix}: ${ctx.syscall} returned ` +
|
|
`${ctx.code} (${ctx.message})`;
|
|
if (ctx.path !== undefined)
|
|
message += ` ${ctx.path}`;
|
|
if (ctx.dest !== undefined)
|
|
message += ` => ${ctx.dest}`;
|
|
return message;
|
|
}
|
|
|
|
// A specialized Error that includes an additional info property with
|
|
// additional information about the error condition.
|
|
// It has the properties present in a UVException but with a custom error
|
|
// message followed by the uv error code and uv error message.
|
|
// It also has its own error code with the original uv error context put into
|
|
// `err.info`.
|
|
// The context passed into this error must have .code, .syscall and .message,
|
|
// and may have .path and .dest.
|
|
class SystemError extends Error {
|
|
constructor(key, context = {}) {
|
|
context = context || {};
|
|
super(sysErrorMessage(message(key), context));
|
|
Object.defineProperty(this, kInfo, {
|
|
configurable: false,
|
|
enumerable: false,
|
|
value: context
|
|
});
|
|
Object.defineProperty(this, kCode, {
|
|
configurable: true,
|
|
enumerable: false,
|
|
value: key,
|
|
writable: true
|
|
});
|
|
}
|
|
|
|
get name() {
|
|
return `SystemError [${this[kCode]}]`;
|
|
}
|
|
|
|
set name(value) {
|
|
defineProperty(this, 'name', {
|
|
configurable: true,
|
|
enumerable: true,
|
|
value,
|
|
writable: true
|
|
});
|
|
}
|
|
|
|
get code() {
|
|
return this[kCode];
|
|
}
|
|
|
|
set code(value) {
|
|
defineProperty(this, 'code', {
|
|
configurable: true,
|
|
enumerable: true,
|
|
value,
|
|
writable: true
|
|
});
|
|
}
|
|
|
|
get info() {
|
|
return this[kInfo];
|
|
}
|
|
|
|
get errno() {
|
|
return this[kInfo].errno;
|
|
}
|
|
|
|
set errno(val) {
|
|
this[kInfo].errno = val;
|
|
}
|
|
|
|
get syscall() {
|
|
return this[kInfo].syscall;
|
|
}
|
|
|
|
set syscall(val) {
|
|
this[kInfo].syscall = val;
|
|
}
|
|
|
|
get path() {
|
|
return this[kInfo].path !== undefined ?
|
|
this[kInfo].path.toString() : undefined;
|
|
}
|
|
|
|
set path(val) {
|
|
this[kInfo].path = val ?
|
|
lazyBuffer().from(val.toString()) : undefined;
|
|
}
|
|
|
|
get dest() {
|
|
return this[kInfo].path !== undefined ?
|
|
this[kInfo].dest.toString() : undefined;
|
|
}
|
|
|
|
set dest(val) {
|
|
this[kInfo].dest = val ?
|
|
lazyBuffer().from(val.toString()) : undefined;
|
|
}
|
|
}
|
|
|
|
function makeSystemErrorWithCode(key) {
|
|
return class NodeError extends SystemError {
|
|
constructor(...args) {
|
|
super(key, ...args);
|
|
}
|
|
};
|
|
}
|
|
|
|
function makeNodeErrorWithCode(Base, key) {
|
|
return class NodeError extends Base {
|
|
constructor(...args) {
|
|
super(message(key, args));
|
|
}
|
|
|
|
get name() {
|
|
return `${super.name} [${key}]`;
|
|
}
|
|
|
|
set name(value) {
|
|
defineProperty(this, 'name', {
|
|
configurable: true,
|
|
enumerable: true,
|
|
value,
|
|
writable: true
|
|
});
|
|
}
|
|
|
|
get code() {
|
|
return key;
|
|
}
|
|
|
|
set code(value) {
|
|
defineProperty(this, 'code', {
|
|
configurable: true,
|
|
enumerable: true,
|
|
value,
|
|
writable: true
|
|
});
|
|
}
|
|
};
|
|
}
|
|
|
|
// Utility function for registering the error codes. Only used here. Exported
|
|
// *only* to allow for testing.
|
|
function E(sym, val, def, ...otherClasses) {
|
|
// Special case for SystemError that formats the error message differently
|
|
// The SystemErrors only have SystemError as their base classes.
|
|
messages.set(sym, val);
|
|
if (def === SystemError) {
|
|
def = makeSystemErrorWithCode(sym);
|
|
} else {
|
|
def = makeNodeErrorWithCode(def, sym);
|
|
}
|
|
|
|
if (otherClasses.length !== 0) {
|
|
otherClasses.forEach((clazz) => {
|
|
def[clazz.name] = makeNodeErrorWithCode(clazz, sym);
|
|
});
|
|
}
|
|
codes[sym] = def;
|
|
}
|
|
|
|
function lazyBuffer() {
|
|
if (buffer === undefined)
|
|
buffer = require('buffer').Buffer;
|
|
return buffer;
|
|
}
|
|
|
|
function createErrDiff(actual, expected, operator) {
|
|
var other = '';
|
|
var res = '';
|
|
var lastPos = 0;
|
|
var end = '';
|
|
var skipped = false;
|
|
if (util === undefined) util = require('util');
|
|
const actualLines = inspectValue(actual);
|
|
const expectedLines = inspectValue(expected);
|
|
const msg = READABLE_OPERATOR[operator] +
|
|
`:\n${green}+ expected${white} ${red}- actual${white}`;
|
|
const skippedMsg = ` ${blue}...${white} Lines skipped`;
|
|
|
|
// Remove all ending lines that match (this optimizes the output for
|
|
// readability by reducing the number of total changed lines).
|
|
var a = actualLines[actualLines.length - 1];
|
|
var b = expectedLines[expectedLines.length - 1];
|
|
var i = 0;
|
|
while (a === b) {
|
|
if (i++ < 2) {
|
|
end = `\n ${a}${end}`;
|
|
} else {
|
|
other = a;
|
|
}
|
|
actualLines.pop();
|
|
expectedLines.pop();
|
|
if (actualLines.length === 0 || expectedLines.length === 0)
|
|
break;
|
|
a = actualLines[actualLines.length - 1];
|
|
b = expectedLines[expectedLines.length - 1];
|
|
}
|
|
if (i > 3) {
|
|
end = `\n${blue}...${white}${end}`;
|
|
skipped = true;
|
|
}
|
|
if (other !== '') {
|
|
end = `\n ${other}${end}`;
|
|
other = '';
|
|
}
|
|
|
|
const maxLines = Math.max(actualLines.length, expectedLines.length);
|
|
var printedLines = 0;
|
|
var identical = 0;
|
|
for (i = 0; i < maxLines; i++) {
|
|
// Only extra expected lines exist
|
|
const cur = i - lastPos;
|
|
if (actualLines.length < i + 1) {
|
|
if (cur > 1 && i > 2) {
|
|
if (cur > 4) {
|
|
res += `\n${blue}...${white}`;
|
|
skipped = true;
|
|
} else if (cur > 3) {
|
|
res += `\n ${expectedLines[i - 2]}`;
|
|
printedLines++;
|
|
}
|
|
res += `\n ${expectedLines[i - 1]}`;
|
|
printedLines++;
|
|
}
|
|
lastPos = i;
|
|
other += `\n${green}+${white} ${expectedLines[i]}`;
|
|
printedLines++;
|
|
// Only extra actual lines exist
|
|
} else if (expectedLines.length < i + 1) {
|
|
if (cur > 1 && i > 2) {
|
|
if (cur > 4) {
|
|
res += `\n${blue}...${white}`;
|
|
skipped = true;
|
|
} else if (cur > 3) {
|
|
res += `\n ${actualLines[i - 2]}`;
|
|
printedLines++;
|
|
}
|
|
res += `\n ${actualLines[i - 1]}`;
|
|
printedLines++;
|
|
}
|
|
lastPos = i;
|
|
res += `\n${red}-${white} ${actualLines[i]}`;
|
|
printedLines++;
|
|
// Lines diverge
|
|
} else if (actualLines[i] !== expectedLines[i]) {
|
|
if (cur > 1 && i > 2) {
|
|
if (cur > 4) {
|
|
res += `\n${blue}...${white}`;
|
|
skipped = true;
|
|
} else if (cur > 3) {
|
|
res += `\n ${actualLines[i - 2]}`;
|
|
printedLines++;
|
|
}
|
|
res += `\n ${actualLines[i - 1]}`;
|
|
printedLines++;
|
|
}
|
|
lastPos = i;
|
|
res += `\n${red}-${white} ${actualLines[i]}`;
|
|
other += `\n${green}+${white} ${expectedLines[i]}`;
|
|
printedLines += 2;
|
|
// Lines are identical
|
|
} else {
|
|
res += other;
|
|
other = '';
|
|
if (cur === 1 || i === 0) {
|
|
res += `\n ${actualLines[i]}`;
|
|
printedLines++;
|
|
}
|
|
identical++;
|
|
}
|
|
// Inspected object to big (Show ~20 rows max)
|
|
if (printedLines > 20 && i < maxLines - 2) {
|
|
return `${msg}${skippedMsg}\n${res}\n${blue}...${white}${other}\n` +
|
|
`${blue}...${white}`;
|
|
}
|
|
}
|
|
|
|
// Strict equal with identical objects that are not identical by reference.
|
|
if (identical === maxLines) {
|
|
// E.g., assert.deepStrictEqual(Symbol(), Symbol())
|
|
const base = operator === 'strictEqual' ?
|
|
'Input objects identical but not reference equal:' :
|
|
'Input objects not identical:';
|
|
|
|
// We have to get the result again. The lines were all removed before.
|
|
const actualLines = inspectValue(actual);
|
|
|
|
// Only remove lines in case it makes sense to collapse those.
|
|
// TODO: Accept env to always show the full error.
|
|
if (actualLines.length > 30) {
|
|
actualLines[26] = `${blue}...${white}`;
|
|
while (actualLines.length > 27) {
|
|
actualLines.pop();
|
|
}
|
|
}
|
|
|
|
return `${base}\n\n${actualLines.join('\n')}\n`;
|
|
}
|
|
return `${msg}${skipped ? skippedMsg : ''}\n${res}${other}${end}`;
|
|
}
|
|
|
|
class AssertionError extends Error {
|
|
constructor(options) {
|
|
if (typeof options !== 'object' || options === null) {
|
|
throw new codes.ERR_INVALID_ARG_TYPE('options', 'Object', options);
|
|
}
|
|
var {
|
|
actual,
|
|
expected,
|
|
message,
|
|
operator,
|
|
stackStartFn
|
|
} = options;
|
|
|
|
if (message != null) {
|
|
super(message);
|
|
} else {
|
|
if (process.stdout.isTTY) {
|
|
// Reset on each call to make sure we handle dynamically set environment
|
|
// variables correct.
|
|
if (process.stdout.getColorDepth() !== 1) {
|
|
blue = '\u001b[34m';
|
|
green = '\u001b[32m';
|
|
white = '\u001b[39m';
|
|
red = '\u001b[31m';
|
|
} else {
|
|
blue = '';
|
|
green = '';
|
|
white = '';
|
|
red = '';
|
|
}
|
|
}
|
|
if (util === undefined) util = require('util');
|
|
// Prevent the error stack from being visible by duplicating the error
|
|
// in a very close way to the original in case both sides are actually
|
|
// instances of Error.
|
|
if (typeof actual === 'object' && actual !== null &&
|
|
typeof expected === 'object' && expected !== null &&
|
|
'stack' in actual && actual instanceof Error &&
|
|
'stack' in expected && expected instanceof Error) {
|
|
actual = copyError(actual);
|
|
expected = copyError(expected);
|
|
}
|
|
|
|
if (operator === 'deepStrictEqual' || operator === 'strictEqual') {
|
|
super(createErrDiff(actual, expected, operator));
|
|
} else if (operator === 'notDeepStrictEqual' ||
|
|
operator === 'notStrictEqual') {
|
|
// In case the objects are equal but the operator requires unequal, show
|
|
// the first object and say A equals B
|
|
const res = inspectValue(actual);
|
|
const base = `Identical input passed to ${operator}:`;
|
|
|
|
// Only remove lines in case it makes sense to collapse those.
|
|
// TODO: Accept env to always show the full error.
|
|
if (res.length > 30) {
|
|
res[26] = `${blue}...${white}`;
|
|
while (res.length > 27) {
|
|
res.pop();
|
|
}
|
|
}
|
|
|
|
// Only print a single input.
|
|
if (res.length === 1) {
|
|
super(`${base} ${res[0]}`);
|
|
} else {
|
|
super(`${base}\n\n${res.join('\n')}\n`);
|
|
}
|
|
} else {
|
|
let res = util.inspect(actual);
|
|
let other = util.inspect(expected);
|
|
if (res.length > 128)
|
|
res = `${res.slice(0, 125)}...`;
|
|
if (other.length > 128)
|
|
other = `${other.slice(0, 125)}...`;
|
|
super(`${res} ${operator} ${other}`);
|
|
}
|
|
}
|
|
|
|
this.generatedMessage = !message;
|
|
this.name = 'AssertionError [ERR_ASSERTION]';
|
|
this.code = 'ERR_ASSERTION';
|
|
this.actual = actual;
|
|
this.expected = expected;
|
|
this.operator = operator;
|
|
Error.captureStackTrace(this, stackStartFn);
|
|
}
|
|
}
|
|
|
|
// 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 = []) {
|
|
const msg = messages.get(key);
|
|
if (util === undefined) util = require('util');
|
|
|
|
if (typeof msg === 'function') {
|
|
internalAssert(
|
|
msg.length <= args.length, // Default options do not count.
|
|
`Code: ${key}; The provided arguments length (${args.length}) does not ` +
|
|
`match the required ones (${msg.length}).`
|
|
);
|
|
return msg.apply(null, args);
|
|
}
|
|
|
|
const expectedLength = (msg.match(/%[dfijoOs]/g) || []).length;
|
|
internalAssert(
|
|
expectedLength === args.length,
|
|
`Code: ${key}; The provided arguments length (${args.length}) does not ` +
|
|
`match the required ones (${expectedLength}).`
|
|
);
|
|
if (args.length === 0)
|
|
return msg;
|
|
|
|
args.unshift(msg);
|
|
return util.format.apply(null, args);
|
|
}
|
|
|
|
/**
|
|
* This creates an error compatible with errors produced in the C++
|
|
* function UVException using a context object with data assembled in C++.
|
|
* The goal is to migrate them to ERR_* errors later when compatibility is
|
|
* not a concern.
|
|
*
|
|
* @param {Object} ctx
|
|
* @returns {Error}
|
|
*/
|
|
function uvException(ctx) {
|
|
const [ code, uvmsg ] = errmap.get(ctx.errno);
|
|
let message = `${code}: ${uvmsg}, ${ctx.syscall}`;
|
|
|
|
let path;
|
|
let dest;
|
|
if (ctx.path) {
|
|
path = ctx.path.toString();
|
|
message += ` '${path}'`;
|
|
}
|
|
if (ctx.dest) {
|
|
dest = ctx.dest.toString();
|
|
message += ` -> '${dest}'`;
|
|
}
|
|
|
|
// Pass the message to the constructor instead of setting it on the object
|
|
// to make sure it is the same as the one created in C++
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
const err = new Error(message);
|
|
|
|
for (const prop of Object.keys(ctx)) {
|
|
if (prop === 'message' || prop === 'path' || prop === 'dest') {
|
|
continue;
|
|
}
|
|
err[prop] = ctx[prop];
|
|
}
|
|
|
|
err.code = code;
|
|
if (path) {
|
|
err.path = path;
|
|
}
|
|
if (dest) {
|
|
err.dest = dest;
|
|
}
|
|
|
|
Error.captureStackTrace(err, uvException);
|
|
return err;
|
|
}
|
|
|
|
/**
|
|
* This used to be util._errnoException().
|
|
*
|
|
* @param {number} err - A libuv error number
|
|
* @param {string} syscall
|
|
* @param {string} [original]
|
|
* @returns {Error}
|
|
*/
|
|
function errnoException(err, syscall, original) {
|
|
// TODO(joyeecheung): We have to use the type-checked
|
|
// getSystemErrorName(err) to guard against invalid arguments from users.
|
|
// This can be replaced with [ code ] = errmap.get(err) when this method
|
|
// is no longer exposed to user land.
|
|
if (util === undefined) util = require('util');
|
|
const code = util.getSystemErrorName(err);
|
|
const message = original ?
|
|
`${syscall} ${code} ${original}` : `${syscall} ${code}`;
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
const ex = new Error(message);
|
|
// TODO(joyeecheung): errno is supposed to err, like in uvException
|
|
ex.code = ex.errno = code;
|
|
ex.syscall = syscall;
|
|
|
|
Error.captureStackTrace(ex, errnoException);
|
|
return ex;
|
|
}
|
|
|
|
/**
|
|
* This used to be util._exceptionWithHostPort().
|
|
*
|
|
* @param {number} err - A libuv error number
|
|
* @param {string} syscall
|
|
* @param {string} address
|
|
* @param {number} [port]
|
|
* @param {string} [additional]
|
|
* @returns {Error}
|
|
*/
|
|
function exceptionWithHostPort(err, syscall, address, port, additional) {
|
|
// TODO(joyeecheung): We have to use the type-checked
|
|
// getSystemErrorName(err) to guard against invalid arguments from users.
|
|
// This can be replaced with [ code ] = errmap.get(err) when this method
|
|
// is no longer exposed to user land.
|
|
if (util === undefined) util = require('util');
|
|
const code = util.getSystemErrorName(err);
|
|
let details = '';
|
|
if (port && port > 0) {
|
|
details = ` ${address}:${port}`;
|
|
} else if (address) {
|
|
details = ` ${address}`;
|
|
}
|
|
if (additional) {
|
|
details += ` - Local (${additional})`;
|
|
}
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
const ex = new Error(`${syscall} ${code}${details}`);
|
|
// TODO(joyeecheung): errno is supposed to err, like in uvException
|
|
ex.code = ex.errno = code;
|
|
ex.syscall = syscall;
|
|
ex.address = address;
|
|
if (port) {
|
|
ex.port = port;
|
|
}
|
|
|
|
Error.captureStackTrace(ex, exceptionWithHostPort);
|
|
return ex;
|
|
}
|
|
|
|
/**
|
|
* @param {number|string} code - A libuv error number or a c-ares error code
|
|
* @param {string} syscall
|
|
* @param {string} [hostname]
|
|
* @returns {Error}
|
|
*/
|
|
function dnsException(code, syscall, hostname) {
|
|
// If `code` is of type number, it is a libuv error number, else it is a
|
|
// c-ares error code.
|
|
if (typeof code === 'number') {
|
|
// FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass
|
|
// the true error to the user. ENOTFOUND is not even a proper POSIX error!
|
|
if (code === UV_EAI_MEMORY ||
|
|
code === UV_EAI_NODATA ||
|
|
code === UV_EAI_NONAME) {
|
|
code = 'ENOTFOUND'; // Fabricated error name.
|
|
} else {
|
|
code = lazyInternalUtil().getSystemErrorName(code);
|
|
}
|
|
}
|
|
const message = `${syscall} ${code}${hostname ? ` ${hostname}` : ''}`;
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
const ex = new Error(message);
|
|
// TODO(joyeecheung): errno is supposed to be a number / err, like in
|
|
// uvException.
|
|
ex.errno = code;
|
|
ex.code = code;
|
|
ex.syscall = syscall;
|
|
if (hostname) {
|
|
ex.hostname = hostname;
|
|
}
|
|
Error.captureStackTrace(ex, dnsException);
|
|
return ex;
|
|
}
|
|
|
|
let maxStack_ErrorName;
|
|
let maxStack_ErrorMessage;
|
|
/**
|
|
* Returns true if `err.name` and `err.message` are equal to engine-specific
|
|
* values indicating max call stack size has been exceeded.
|
|
* "Maximum call stack size exceeded" in V8.
|
|
*
|
|
* @param {Error} err
|
|
* @returns {boolean}
|
|
*/
|
|
function isStackOverflowError(err) {
|
|
if (maxStack_ErrorMessage === undefined) {
|
|
try {
|
|
function overflowStack() { overflowStack(); }
|
|
overflowStack();
|
|
} catch (err) {
|
|
maxStack_ErrorMessage = err.message;
|
|
maxStack_ErrorName = err.name;
|
|
}
|
|
}
|
|
|
|
return err.name === maxStack_ErrorName &&
|
|
err.message === maxStack_ErrorMessage;
|
|
}
|
|
|
|
module.exports = {
|
|
dnsException,
|
|
errnoException,
|
|
exceptionWithHostPort,
|
|
uvException,
|
|
isStackOverflowError,
|
|
message,
|
|
AssertionError,
|
|
SystemError,
|
|
codes,
|
|
E, // This is exported only to facilitate testing.
|
|
errorCache: new Map() // This is in here only to facilitate testing.
|
|
};
|
|
|
|
// To declare an error message, use the E(sym, val, def) function above. The sym
|
|
// must be an upper case string. The val can be either a function or a string.
|
|
// The def must be an error class.
|
|
// The return value of the function must be a string.
|
|
// Examples:
|
|
// E('EXAMPLE_KEY1', 'This is the error value', Error);
|
|
// E('EXAMPLE_KEY2', (a, b) => return `${a} ${b}`, RangeError);
|
|
//
|
|
// Once an error code has been assigned, the code itself MUST NOT change and
|
|
// any given error code must never be reused to identify a different error.
|
|
//
|
|
// Any error code added here should also be added to the documentation
|
|
//
|
|
// Note: Please try to keep these in alphabetical order
|
|
//
|
|
// Note: Node.js specific errors must begin with the prefix ERR_
|
|
|
|
E('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError);
|
|
E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError);
|
|
E('ERR_ASSERTION', '%s', Error);
|
|
E('ERR_ASYNC_CALLBACK', '%s must be a function', TypeError);
|
|
E('ERR_ASYNC_TYPE', 'Invalid name for async "type": %s', TypeError);
|
|
E('ERR_BUFFER_OUT_OF_BOUNDS', bufferOutOfBounds, RangeError);
|
|
E('ERR_BUFFER_TOO_LARGE',
|
|
`Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes`,
|
|
RangeError);
|
|
E('ERR_CANNOT_WATCH_SIGINT', 'Cannot watch for SIGINT signals', Error);
|
|
E('ERR_CHILD_CLOSED_BEFORE_REPLY',
|
|
'Child closed before reply received', Error);
|
|
E('ERR_CHILD_PROCESS_IPC_REQUIRED',
|
|
"Forked processes must have an IPC channel, missing value 'ipc' in %s",
|
|
Error);
|
|
E('ERR_CHILD_PROCESS_STDIO_MAXBUFFER', '%s maxBuffer length exceeded',
|
|
RangeError);
|
|
E('ERR_CONSOLE_WRITABLE_STREAM',
|
|
'Console expects a writable stream instance for %s', TypeError);
|
|
E('ERR_CPU_USAGE', 'Unable to obtain cpu usage %s', Error);
|
|
E('ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED',
|
|
'Custom engines not supported by this OpenSSL', Error);
|
|
E('ERR_CRYPTO_ECDH_INVALID_FORMAT', 'Invalid ECDH format: %s', TypeError);
|
|
E('ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY',
|
|
'Public key is not valid for specified curve', Error);
|
|
E('ERR_CRYPTO_ENGINE_UNKNOWN', 'Engine "%s" was not found', Error);
|
|
E('ERR_CRYPTO_FIPS_FORCED',
|
|
'Cannot set FIPS mode, it was forced with --force-fips at startup.', Error);
|
|
E('ERR_CRYPTO_FIPS_UNAVAILABLE', 'Cannot set FIPS mode in a non-FIPS build.',
|
|
Error);
|
|
E('ERR_CRYPTO_HASH_DIGEST_NO_UTF16', 'hash.digest() does not support UTF-16',
|
|
Error);
|
|
E('ERR_CRYPTO_HASH_FINALIZED', 'Digest already called', Error);
|
|
E('ERR_CRYPTO_HASH_UPDATE_FAILED', 'Hash update failed', Error);
|
|
E('ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: %s', TypeError);
|
|
E('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation %s', Error);
|
|
|
|
// Switch to TypeError. The current implementation does not seem right.
|
|
E('ERR_CRYPTO_SIGN_KEY_REQUIRED', 'No key provided to sign', Error);
|
|
E('ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH',
|
|
'Input buffers must have the same length', RangeError);
|
|
E('ERR_DNS_SET_SERVERS_FAILED', 'c-ares failed to set servers: "%s" [%s]',
|
|
Error);
|
|
E('ERR_DOMAIN_CALLBACK_NOT_AVAILABLE',
|
|
'A callback was registered through ' +
|
|
'process.setUncaughtExceptionCaptureCallback(), which is mutually ' +
|
|
'exclusive with using the `domain` module',
|
|
Error);
|
|
E('ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE',
|
|
'The `domain` module is in use, which is mutually exclusive with calling ' +
|
|
'process.setUncaughtExceptionCaptureCallback()',
|
|
Error);
|
|
E('ERR_ENCODING_INVALID_ENCODED_DATA',
|
|
'The encoded data was not valid for encoding %s', TypeError);
|
|
E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported',
|
|
RangeError);
|
|
E('ERR_FALSY_VALUE_REJECTION', 'Promise was rejected with falsy value', Error);
|
|
E('ERR_FS_FILE_TOO_LARGE', 'File size (%s) is greater than possible Buffer: ' +
|
|
`${kMaxLength} bytes`,
|
|
RangeError);
|
|
E('ERR_FS_INVALID_SYMLINK_TYPE',
|
|
'Symlink type must be one of "dir", "file", or "junction". Received "%s"',
|
|
Error); // Switch to TypeError. The current implementation does not seem right
|
|
E('ERR_HTTP2_ALTSVC_INVALID_ORIGIN',
|
|
'HTTP/2 ALTSVC frames require a valid origin', TypeError);
|
|
E('ERR_HTTP2_ALTSVC_LENGTH',
|
|
'HTTP/2 ALTSVC frames are limited to 16382 bytes', TypeError);
|
|
E('ERR_HTTP2_CONNECT_AUTHORITY',
|
|
':authority header is required for CONNECT requests', Error);
|
|
E('ERR_HTTP2_CONNECT_PATH',
|
|
'The :path header is forbidden for CONNECT requests', Error);
|
|
E('ERR_HTTP2_CONNECT_SCHEME',
|
|
'The :scheme header is forbidden for CONNECT requests', Error);
|
|
E('ERR_HTTP2_GOAWAY_SESSION',
|
|
'New streams cannot be created after receiving a GOAWAY', Error);
|
|
E('ERR_HTTP2_HEADERS_AFTER_RESPOND',
|
|
'Cannot specify additional headers after response initiated', Error);
|
|
E('ERR_HTTP2_HEADERS_SENT', 'Response has already been initiated.', Error);
|
|
E('ERR_HTTP2_HEADER_SINGLE_VALUE',
|
|
'Header field "%s" must only have a single value', TypeError);
|
|
E('ERR_HTTP2_INFO_STATUS_NOT_ALLOWED',
|
|
'Informational status codes cannot be used', RangeError);
|
|
E('ERR_HTTP2_INVALID_CONNECTION_HEADERS',
|
|
'HTTP/1 Connection specific headers are forbidden: "%s"', TypeError);
|
|
E('ERR_HTTP2_INVALID_HEADER_VALUE',
|
|
'Invalid value "%s" for header "%s"', TypeError);
|
|
E('ERR_HTTP2_INVALID_INFO_STATUS',
|
|
'Invalid informational status code: %s', RangeError);
|
|
E('ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH',
|
|
'Packed settings length must be a multiple of six', RangeError);
|
|
E('ERR_HTTP2_INVALID_PSEUDOHEADER',
|
|
'"%s" is an invalid pseudoheader or is used incorrectly', TypeError);
|
|
E('ERR_HTTP2_INVALID_SESSION', 'The session has been destroyed', Error);
|
|
E('ERR_HTTP2_INVALID_SETTING_VALUE',
|
|
'Invalid value for setting "%s": %s', TypeError, RangeError);
|
|
E('ERR_HTTP2_INVALID_STREAM', 'The stream has been destroyed', Error);
|
|
E('ERR_HTTP2_MAX_PENDING_SETTINGS_ACK',
|
|
'Maximum number of pending settings acknowledgements', Error);
|
|
E('ERR_HTTP2_NO_SOCKET_MANIPULATION',
|
|
'HTTP/2 sockets should not be directly manipulated (e.g. read and written)',
|
|
Error);
|
|
E('ERR_HTTP2_OUT_OF_STREAMS',
|
|
'No stream ID is available because maximum stream ID has been reached',
|
|
Error);
|
|
E('ERR_HTTP2_PAYLOAD_FORBIDDEN',
|
|
'Responses with %s status must not have a payload', Error);
|
|
E('ERR_HTTP2_PING_CANCEL', 'HTTP2 ping cancelled', Error);
|
|
E('ERR_HTTP2_PING_LENGTH', 'HTTP2 ping payload must be 8 bytes', RangeError);
|
|
E('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED',
|
|
'Cannot set HTTP/2 pseudo-headers', TypeError);
|
|
E('ERR_HTTP2_PUSH_DISABLED', 'HTTP/2 client has disabled push streams', Error);
|
|
E('ERR_HTTP2_SEND_FILE', 'Directories cannot be sent', Error);
|
|
E('ERR_HTTP2_SEND_FILE_NOSEEK',
|
|
'Offset or length can only be specified for regular files', Error);
|
|
E('ERR_HTTP2_SESSION_ERROR', 'Session closed with error code %s', Error);
|
|
E('ERR_HTTP2_SOCKET_BOUND',
|
|
'The socket is already bound to an Http2Session', Error);
|
|
E('ERR_HTTP2_STATUS_101',
|
|
'HTTP status code 101 (Switching Protocols) is forbidden in HTTP/2', Error);
|
|
E('ERR_HTTP2_STATUS_INVALID', 'Invalid status code: %s', RangeError);
|
|
E('ERR_HTTP2_STREAM_CANCEL', 'The pending stream has been canceled', Error);
|
|
E('ERR_HTTP2_STREAM_ERROR', 'Stream closed with error code %s', Error);
|
|
E('ERR_HTTP2_STREAM_SELF_DEPENDENCY',
|
|
'A stream cannot depend on itself', Error);
|
|
E('ERR_HTTP2_TRAILERS_ALREADY_SENT',
|
|
'Trailing headers have already been sent', Error);
|
|
E('ERR_HTTP2_TRAILERS_NOT_READY',
|
|
'Trailing headers cannot be sent until after the wantTrailers event is ' +
|
|
'emitted', Error);
|
|
E('ERR_HTTP2_UNSUPPORTED_PROTOCOL', 'protocol "%s" is unsupported.', Error);
|
|
E('ERR_HTTP_HEADERS_SENT',
|
|
'Cannot %s headers after they are sent to the client', Error);
|
|
E('ERR_HTTP_INVALID_HEADER_VALUE',
|
|
'Invalid value "%s" for header "%s"', TypeError);
|
|
E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
|
|
E('ERR_HTTP_TRAILER_INVALID',
|
|
'Trailers are invalid with this transfer encoding', Error);
|
|
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range', RangeError);
|
|
E('ERR_INSPECTOR_ALREADY_CONNECTED', '%s is already connected', Error);
|
|
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
|
|
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
|
|
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
|
|
E('ERR_INVALID_ADDRESS_FAMILY', 'Invalid address family: %s', RangeError);
|
|
E('ERR_INVALID_ARG_TYPE', invalidArgType, TypeError);
|
|
E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
|
|
let inspected = util.inspect(value);
|
|
if (inspected.length > 128) {
|
|
inspected = `${inspected.slice(0, 128)}...`;
|
|
}
|
|
return `The argument '${name}' ${reason}. Received ${inspected}`;
|
|
}, TypeError, RangeError);
|
|
E('ERR_INVALID_ARRAY_LENGTH',
|
|
(name, len, actual) => {
|
|
internalAssert(typeof actual === 'number', 'actual must be of type number');
|
|
return `The array "${name}" (length ${actual}) must be of length ${len}.`;
|
|
}, TypeError);
|
|
E('ERR_INVALID_ASYNC_ID', 'Invalid %s value: %s', RangeError);
|
|
E('ERR_INVALID_BUFFER_SIZE',
|
|
'Buffer size must be a multiple of %s', RangeError);
|
|
E('ERR_INVALID_CALLBACK', 'Callback must be a function', TypeError);
|
|
E('ERR_INVALID_CHAR', invalidChar, TypeError);
|
|
E('ERR_INVALID_CURSOR_POS',
|
|
'Cannot set cursor row without setting its column', TypeError);
|
|
E('ERR_INVALID_FD',
|
|
'"fd" must be a positive integer: %s', RangeError);
|
|
E('ERR_INVALID_FD_TYPE', 'Unsupported fd type: %s', TypeError);
|
|
E('ERR_INVALID_FILE_URL_HOST',
|
|
'File URL host must be "localhost" or empty on %s', TypeError);
|
|
E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s', TypeError);
|
|
E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent', TypeError);
|
|
E('ERR_INVALID_HTTP_TOKEN', '%s must be a valid HTTP token ["%s"]', TypeError);
|
|
E('ERR_INVALID_IP_ADDRESS', 'Invalid IP address: %s', TypeError);
|
|
E('ERR_INVALID_OPT_VALUE', (name, value) =>
|
|
`The value "${String(value)}" is invalid for option "${name}"`,
|
|
TypeError,
|
|
RangeError);
|
|
E('ERR_INVALID_OPT_VALUE_ENCODING',
|
|
'The value "%s" is invalid for option "encoding"', TypeError);
|
|
E('ERR_INVALID_PERFORMANCE_MARK',
|
|
'The "%s" performance mark has not been set', Error);
|
|
E('ERR_INVALID_PROTOCOL',
|
|
'Protocol "%s" not supported. Expected "%s"',
|
|
TypeError);
|
|
E('ERR_INVALID_REPL_EVAL_CONFIG',
|
|
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL', TypeError);
|
|
E('ERR_INVALID_RETURN_VALUE', (input, name, value) => {
|
|
let type;
|
|
if (value && value.constructor && value.constructor.name) {
|
|
type = `instance of ${value.constructor.name}`;
|
|
} else {
|
|
type = `type ${typeof value}`;
|
|
}
|
|
return `Expected ${input} to be returned from the "${name}"` +
|
|
` function but got ${type}.`;
|
|
}, TypeError);
|
|
E('ERR_INVALID_SYNC_FORK_INPUT',
|
|
'Asynchronous forks do not support Buffer, Uint8Array or string input: %s',
|
|
TypeError);
|
|
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
|
|
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
|
|
E('ERR_INVALID_URI', 'URI malformed', URIError);
|
|
E('ERR_INVALID_URL', 'Invalid URL: %s', TypeError);
|
|
E('ERR_INVALID_URL_SCHEME',
|
|
(expected) => `The URL must be ${oneOf(expected, 'scheme')}`, TypeError);
|
|
E('ERR_IPC_CHANNEL_CLOSED', 'Channel closed', Error);
|
|
E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected', Error);
|
|
E('ERR_IPC_ONE_PIPE', 'Child process can have only one IPC pipe', Error);
|
|
E('ERR_IPC_SYNC_FORK', 'IPC cannot be used with synchronous forks', Error);
|
|
E('ERR_METHOD_NOT_IMPLEMENTED', 'The %s method is not implemented', Error);
|
|
E('ERR_MISSING_ARGS', missingArgs, TypeError);
|
|
E('ERR_MISSING_MODULE', 'Cannot find module %s', Error);
|
|
E('ERR_MODULE_RESOLUTION_LEGACY',
|
|
'%s not found by import in %s.' +
|
|
' Legacy behavior in require() would have found it at %s',
|
|
Error);
|
|
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error);
|
|
E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function', TypeError);
|
|
E('ERR_NAPI_INVALID_DATAVIEW_ARGS',
|
|
'byte_offset + byte_length should be less than or equal to the size in ' +
|
|
'bytes of the array passed in',
|
|
RangeError);
|
|
E('ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT',
|
|
'start offset of %s should be a multiple of %s', RangeError);
|
|
E('ERR_NAPI_INVALID_TYPEDARRAY_LENGTH',
|
|
'Invalid typed array length', RangeError);
|
|
E('ERR_NO_CRYPTO',
|
|
'Node.js is not compiled with OpenSSL crypto support', Error);
|
|
E('ERR_NO_ICU',
|
|
'%s is not supported on Node.js compiled without ICU', TypeError);
|
|
E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported', Error);
|
|
E('ERR_OUT_OF_RANGE', outOfRange, RangeError);
|
|
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);
|
|
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
|
|
'Script execution was interrupted by `SIGINT`', Error);
|
|
E('ERR_SERVER_ALREADY_LISTEN',
|
|
'Listen method has been called more than once without closing.', Error);
|
|
E('ERR_SERVER_NOT_RUNNING', 'Server is not running.', Error);
|
|
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound', Error);
|
|
E('ERR_SOCKET_BAD_BUFFER_SIZE',
|
|
'Buffer size must be a positive integer', TypeError);
|
|
E('ERR_SOCKET_BAD_PORT',
|
|
'Port should be > 0 and < 65536. Received %s.', RangeError);
|
|
E('ERR_SOCKET_BAD_TYPE',
|
|
'Bad socket type specified. Valid types are: udp4, udp6', TypeError);
|
|
E('ERR_SOCKET_BUFFER_SIZE',
|
|
'Could not get or set buffer size',
|
|
SystemError);
|
|
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data', Error);
|
|
E('ERR_SOCKET_CLOSED', 'Socket is closed', Error);
|
|
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running', Error);
|
|
E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed', Error);
|
|
E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed', Error);
|
|
E('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable', Error);
|
|
E('ERR_STREAM_DESTROYED', 'Cannot call %s after a stream was destroyed', Error);
|
|
E('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);
|
|
E('ERR_STREAM_PREMATURE_CLOSE', 'Premature close', Error);
|
|
E('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF', Error);
|
|
E('ERR_STREAM_UNSHIFT_AFTER_END_EVENT',
|
|
'stream.unshift() after end event', Error);
|
|
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode', Error);
|
|
E('ERR_STREAM_WRITE_AFTER_END', 'write after end', Error);
|
|
E('ERR_SYSTEM_ERROR', 'A system error occurred', SystemError);
|
|
E('ERR_TLS_CERT_ALTNAME_INVALID',
|
|
'Hostname/IP does not match certificate\'s altnames: %s', Error);
|
|
E('ERR_TLS_DH_PARAM_SIZE', 'DH parameter size %s is less than 2048', Error);
|
|
E('ERR_TLS_HANDSHAKE_TIMEOUT', 'TLS handshake timeout', Error);
|
|
E('ERR_TLS_RENEGOTIATION_DISABLED',
|
|
'TLS session renegotiation disabled for this socket', Error);
|
|
|
|
// This should probably be a `TypeError`.
|
|
E('ERR_TLS_REQUIRED_SERVER_NAME',
|
|
'"servername" is required parameter for Server.addContext', Error);
|
|
E('ERR_TLS_SESSION_ATTACK', 'TLS session renegotiation attack detected', Error);
|
|
E('ERR_TLS_SNI_FROM_SERVER',
|
|
'Cannot issue SNI from a TLS server-side socket', Error);
|
|
E('ERR_TRACE_EVENTS_CATEGORY_REQUIRED',
|
|
'At least one category is required', TypeError);
|
|
E('ERR_TRACE_EVENTS_UNAVAILABLE', 'Trace events are unavailable', Error);
|
|
E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
|
|
'Calling transform done when still transforming', Error);
|
|
|
|
// This should probably be a `RangeError`.
|
|
E('ERR_TRANSFORM_WITH_LENGTH_0',
|
|
'Calling transform done when writableState.length != 0', Error);
|
|
E('ERR_TTY_INIT_FAILED', 'TTY initialization failed', SystemError);
|
|
E('ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET',
|
|
'`process.setupUncaughtExceptionCapture()` was called while a capture ' +
|
|
'callback was already active',
|
|
Error);
|
|
E('ERR_UNESCAPED_CHARACTERS', '%s contains unescaped characters', TypeError);
|
|
E('ERR_UNHANDLED_ERROR',
|
|
// Using a default argument here is important so the argument is not counted
|
|
// towards `Function#length`.
|
|
(err = undefined) => {
|
|
const msg = 'Unhandled error.';
|
|
if (err === undefined) return msg;
|
|
return `${msg} (${err})`;
|
|
}, Error);
|
|
E('ERR_UNKNOWN_CREDENTIAL', '%s identifier does not exist: %s', Error);
|
|
E('ERR_UNKNOWN_ENCODING', 'Unknown encoding: %s', TypeError);
|
|
|
|
// This should probably be a `TypeError`.
|
|
E('ERR_UNKNOWN_FILE_EXTENSION', 'Unknown file extension: %s', Error);
|
|
E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
|
|
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
|
|
E('ERR_UNKNOWN_STDIN_TYPE', 'Unknown stdin file type', Error);
|
|
|
|
// This should probably be a `TypeError`.
|
|
E('ERR_UNKNOWN_STREAM_TYPE', 'Unknown stream file type', Error);
|
|
E('ERR_V8BREAKITERATOR',
|
|
'Full ICU data not installed. See https://github.com/nodejs/node/wiki/Intl',
|
|
Error);
|
|
|
|
// This should probably be a `TypeError`.
|
|
E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
|
|
'At least one valid performance entry type is required', Error);
|
|
E('ERR_VM_MODULE_ALREADY_LINKED', 'Module has already been linked', Error);
|
|
E('ERR_VM_MODULE_DIFFERENT_CONTEXT',
|
|
'Linked modules must use the same context', Error);
|
|
E('ERR_VM_MODULE_LINKING_ERRORED',
|
|
'Linking has already failed for the provided module', Error);
|
|
E('ERR_VM_MODULE_NOT_LINKED',
|
|
'Module must be linked before it can be instantiated', Error);
|
|
E('ERR_VM_MODULE_NOT_MODULE',
|
|
'Provided module is not an instance of Module', Error);
|
|
E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);
|
|
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed', Error);
|
|
|
|
function invalidArgType(name, expected, actual) {
|
|
internalAssert(typeof name === 'string', 'name must be a string');
|
|
|
|
// determiner: 'must be' or 'must not be'
|
|
let determiner;
|
|
if (typeof expected === 'string' && expected.startsWith('not ')) {
|
|
determiner = 'must not be';
|
|
expected = expected.replace(/^not /, '');
|
|
} else {
|
|
determiner = 'must be';
|
|
}
|
|
|
|
let msg;
|
|
if (name.endsWith(' argument')) {
|
|
// For cases like 'first argument'
|
|
msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
|
|
} else {
|
|
const type = name.includes('.') ? 'property' : 'argument';
|
|
msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
|
|
}
|
|
|
|
// TODO(BridgeAR): Improve the output by showing `null` and similar.
|
|
msg += `. Received type ${typeof actual}`;
|
|
return msg;
|
|
}
|
|
|
|
function missingArgs(...args) {
|
|
internalAssert(args.length > 0, 'At least one arg needs to be specified');
|
|
let msg = 'The ';
|
|
const len = args.length;
|
|
args = args.map((a) => `"${a}"`);
|
|
switch (len) {
|
|
case 1:
|
|
msg += `${args[0]} argument`;
|
|
break;
|
|
case 2:
|
|
msg += `${args[0]} and ${args[1]} arguments`;
|
|
break;
|
|
default:
|
|
msg += args.slice(0, len - 1).join(', ');
|
|
msg += `, and ${args[len - 1]} arguments`;
|
|
break;
|
|
}
|
|
return `${msg} must be specified`;
|
|
}
|
|
|
|
function oneOf(expected, thing) {
|
|
internalAssert(typeof thing === 'string', '`thing` has to be of type string');
|
|
if (Array.isArray(expected)) {
|
|
const len = expected.length;
|
|
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 ` +
|
|
expected[len - 1];
|
|
} else if (len === 2) {
|
|
return `one of ${thing} ${expected[0]} or ${expected[1]}`;
|
|
} else {
|
|
return `of ${thing} ${expected[0]}`;
|
|
}
|
|
} else {
|
|
return `of ${thing} ${String(expected)}`;
|
|
}
|
|
}
|
|
|
|
// Using a default argument here is important so the argument is not counted
|
|
// towards `Function#length`.
|
|
function bufferOutOfBounds(name = undefined) {
|
|
if (name) {
|
|
return `"${name}" is outside of buffer bounds`;
|
|
}
|
|
return 'Attempt to write outside buffer bounds';
|
|
}
|
|
|
|
// Using a default argument here is important so the argument is not counted
|
|
// towards `Function#length`.
|
|
function invalidChar(name, field = undefined) {
|
|
let msg = `Invalid character in ${name}`;
|
|
if (field !== undefined) {
|
|
msg += ` ["${field}"]`;
|
|
}
|
|
return msg;
|
|
}
|
|
|
|
function outOfRange(name, range, value) {
|
|
let msg = `The value of "${name}" is out of range.`;
|
|
if (range !== undefined) msg += ` It must be ${range}.`;
|
|
msg += ` Received ${value}`;
|
|
return msg;
|
|
}
|