Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
efbfeff
doc: fix incorrect heading level
700software Nov 4, 2020
6968b0f
doc: fix release-schedule link in backport guide
watilde Nov 2, 2020
1b277d9
src: remove ERR prefix in crypto status enums
danbev Oct 29, 2020
9b6512f
n-api: unlink reference during its destructor
Nov 2, 2020
4c6de85
benchmark: remove modules that require intl
richardlau Nov 4, 2020
acd3617
doc: option --prof documentation help added
Krank2me Aug 30, 2020
8bd364a
doc: add new wordings to the API description
PoojaDurgad Oct 10, 2020
23f0d0c
test: fix error in test/internet/test-dns.js
Trott Nov 4, 2020
6d56ba0
doc: update benchmark CI test indicator in README
Trott Nov 3, 2020
dc4936b
crypto: fix comment in ByteSource
tniessen Nov 4, 2020
9757b47
console: use more primordials
aduh95 Oct 21, 2020
b700900
lib: refactor to use more primordials
aduh95 Oct 29, 2020
d83e253
errors: refactor to use more primordials
aduh95 Nov 3, 2020
92bdfd1
fs: add support for AbortSignal in readFile
benjamingr Nov 1, 2020
76332a0
events: port some wpt tests
benjamingr May 28, 2020
a7d0c76
events: support emit on nodeeventtarget
benjamingr Oct 28, 2020
77d33c9
doc: update core-validate-commit link in guide
watilde Nov 3, 2020
01129a7
doc: revise v8.getHeapSnapshot()
Trott Oct 28, 2020
cb6f0d3
test: update old comment style test_util.cc
RaisinTen Oct 30, 2020
1dd744a
http2: fix error stream write followed by destroy
davedoesdev Nov 4, 2020
8d76db8
module: refactor to use iterable-weak-map
Nov 3, 2020
9d26c4d
domain: refactor to use more primordials
aduh95 Oct 30, 2020
da3c2ab
tools,doc: enable ecmaVersion 2021 in acorn parser
aduh95 Nov 6, 2020
9d9a044
benchmark: ignore build artifacts for napi addons
richardlau Nov 4, 2020
6011bfd
fs: remove unused assignment
Trott Oct 30, 2020
1f27214
tools: add new ESLint rule: prefer-primordials
Leko Sep 29, 2020
a7350b3
tools: don't print gold linker warning w/o flag
MylesBorins Nov 4, 2020
7a2edea
win, build: fix build time on Windows
bzoz Nov 2, 2020
33e2ee5
events: define event handler as enumerable
benjamingr Nov 2, 2020
ab0eb4f
events: support event handlers on prototypes
benjamingr Nov 2, 2020
ff59fcd
events: define abort on prototype
benjamingr Nov 2, 2020
9e67372
events: fire handlers in correct oder
benjamingr Nov 6, 2020
ccbe267
fs: remove unnecessary Function#bind() in fs/promises
bnoordhuis Sep 15, 2020
9c4b360
doc,crypto: added sign/verify method changes about dsaEncoding
panva Oct 12, 2020
06cc400
doc: fix crypto doc linter errors
aduh95 Nov 8, 2020
429113e
http2: move events to the JSStreamSocket
mmomtchev Oct 23, 2020
28ed7d0
http2: centralise socket event binding in Http2Session
mmomtchev Oct 24, 2020
b7aa5e2
stream: remove isPromise utility function
aduh95 Nov 2, 2020
567f8d8
events: getEventListeners static
benjamingr Nov 6, 2020
77e4f19
timers: cleanup abort listener on awaitable timers
jasnell Nov 6, 2020
af2a560
lib: add %TypedArray% abstract constructor to primordials
ExE-Boss Nov 7, 2020
7cb5c09
deps: cherry-pick 9a49b22 from V8 upstream
danbev Nov 3, 2020
4b03670
dns: fix trace_events name for resolveCaa()
Trott Nov 5, 2020
eea7e3b
tools,lib: tighten prefer-primordials rules for Error statics
aduh95 Nov 7, 2020
85cf305
doc,fs: document value of stats.isDirectory on symbolic links
coderaiser Apr 25, 2019
99cb362
doc: advise users to import the full set of trusted release keys
haqer1 Apr 4, 2020
d6bd78f
doc,net: document socket.timeout
kobelb Jul 28, 2020
71fa9c6
doc: add note regarding file structure in src/README.md
lundibundi Aug 31, 2020
11f5924
http2: add has method to proxySocketHandler
masx200 Sep 14, 2020
36c20d9
doc,stream: write(chunk, encoding, cb) encoding can be null
dev-script Sep 27, 2020
f73b8d8
test: improve error message for policy failures
bmeck Oct 13, 2020
292915a
bootstrap: refactor to use more primordials
aduh95 Nov 6, 2020
0a4c96a
test: use global.EventTarget instead of internals
aduh95 Nov 6, 2020
7ef9c70
fs: replace finally with PromisePrototypeFinally
baruchiro Nov 6, 2020
7a37590
module: refactor to use more primordials
aduh95 Nov 7, 2020
f8098c3
tools,lib: recommend using safe primordials
aduh95 Nov 7, 2020
10c9ea7
build: fix zlib inlining for IA-32
RaisinTen Oct 16, 2020
0d74226
crypto: refactor to use more primordials
aduh95 Nov 6, 2020
359a659
stream: writableNeedDrain
ronag Sep 25, 2020
6ac9c8f
build, tools: look for local installation of NASM
richardlau Nov 7, 2020
923276c
doc: move v8.getHeapCodeStatistics()
Trott Nov 7, 2020
fdae9ad
test: fix races in test-performance-eventlooputil
Flarna Nov 7, 2020
dcb2760
doc: escape asterisk in cctest gtest-filter
RaisinTen Nov 8, 2020
ccf9f0e
fs: support abortsignal in writeFile
benjamingr Nov 6, 2020
9774b4c
stream: fix thrown object reference
kanongil Nov 10, 2020
5ecd1c9
2020-11-10, Version 15.2.0 (Current)
danielleadams Nov 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
errors: refactor to use more primordials
PR-URL: #35944
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Ben Coe <bencoe@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Nov 9, 2020
commit d83e2530650d6d40fd0787984ca14ce8da896275
105 changes: 65 additions & 40 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,49 @@
// message may change, the code should not.

const {
ArrayFrom,
ArrayIsArray,
ArrayPrototypeIncludes,
ArrayPrototypeIndexOf,
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePop,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
ArrayPrototypeUnshift,
Error,
ErrorCaptureStackTrace,
ErrorPrototypeToString,
JSONStringify,
Map,
MathAbs,
MathMax,
Number,
NumberIsInteger,
ObjectDefineProperty,
ObjectKeys,
RangeError,
ReflectApply,
RegExpPrototypeTest,
SafeMap,
SafeWeakMap,
String,
StringPrototypeEndsWith,
StringPrototypeIncludes,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
StringPrototypeToLowerCase,
Symbol,
SymbolFor,
SyntaxError,
TypeError,
URIError,
WeakMap,
} = primordials;

const isWindows = process.platform === 'win32';

const messages = new Map();
const messages = new SafeMap();
const codes = {};

const classRegExp = /^([A-Z][a-z0-9]*)+$/;
Expand All @@ -54,7 +72,7 @@ const kTypes = [
];

const MainContextError = Error;
const overrideStackTrace = new WeakMap();
const overrideStackTrace = new SafeWeakMap();
const kNoOverride = Symbol('kNoOverride');
const prepareStackTrace = (globalThis, error, trace) => {
// API for node internals to override error stack formatting
Expand Down Expand Up @@ -370,8 +388,8 @@ function getMessage(key, args, self) {
if (args.length === 0)
return msg;

args.unshift(msg);
return lazyInternalUtilInspect().format.apply(null, args);
ArrayPrototypeUnshift(args, msg);
return ReflectApply(lazyInternalUtilInspect().format, null, args);
}

let uvBinding;
Expand Down Expand Up @@ -644,9 +662,9 @@ function addNumericalSeparator(val) {
let i = val.length;
const start = val[0] === '-' ? 1 : 0;
for (; i >= start + 4; i -= 3) {
res = `_${val.slice(i - 3, i)}${res}`;
res = `_${StringPrototypeSlice(val, i - 3, i)}${res}`;
}
return `${val.slice(0, i)}${res}`;
return `${StringPrototypeSlice(val, 0, i)}${res}`;
}

// Used to enhance the stack that will be picked up by the inspector
Expand Down Expand Up @@ -681,7 +699,8 @@ const fatalExceptionStackEnhancers = {
// ANSI escape sequences is not reliable.
if (process.platform === 'win32') {
const info = internalBinding('os').getOSInformation();
const ver = info[2].split('.').map((a) => +a);
const ver = ArrayPrototypeMap(StringPrototypeSplit(info[2], '.'),
Number);
if (ver[0] !== 10 || ver[2] < 14393) {
useColors = false;
}
Expand Down Expand Up @@ -975,11 +994,11 @@ E('ERR_INVALID_ARG_TYPE',
}

let msg = 'The ';
if (name.endsWith(' argument')) {
if (StringPrototypeEndsWith(name, ' argument')) {
// For cases like 'first argument'
msg += `${name} `;
} else {
const type = name.includes('.') ? 'property' : 'argument';
const type = StringPrototypeIncludes(name, '.') ? 'property' : 'argument';
msg += `"${name}" ${type} `;
}
msg += 'must be ';
Expand All @@ -991,31 +1010,31 @@ E('ERR_INVALID_ARG_TYPE',
for (const value of expected) {
assert(typeof value === 'string',
'All expected entries have to be of type string');
if (kTypes.includes(value)) {
types.push(value.toLowerCase());
} else if (classRegExp.test(value)) {
instances.push(value);
if (ArrayPrototypeIncludes(kTypes, value)) {
ArrayPrototypePush(types, StringPrototypeToLowerCase(value));
} else if (RegExpPrototypeTest(classRegExp, value)) {
ArrayPrototypePush(instances, value);
} else {
assert(value !== 'object',
'The value "object" should be written as "Object"');
other.push(value);
ArrayPrototypePush(other, value);
}
}

// Special handle `object` in case other instances are allowed to outline
// the differences between each other.
if (instances.length > 0) {
const pos = types.indexOf('object');
const pos = ArrayPrototypeIndexOf(types, 'object');
if (pos !== -1) {
types.splice(pos, 1);
instances.push('Object');
ArrayPrototypeSplice(types, pos, 1);
ArrayPrototypePush(instances, 'Object');
}
}

if (types.length > 0) {
if (types.length > 2) {
const last = types.pop();
msg += `one of type ${types.join(', ')}, or ${last}`;
const last = ArrayPrototypePop(types);
msg += `one of type ${ArrayPrototypeJoin(types, ', ')}, or ${last}`;
} else if (types.length === 2) {
msg += `one of type ${types[0]} or ${types[1]}`;
} else {
Expand All @@ -1027,8 +1046,9 @@ E('ERR_INVALID_ARG_TYPE',

if (instances.length > 0) {
if (instances.length > 2) {
const last = instances.pop();
msg += `an instance of ${instances.join(', ')}, or ${last}`;
const last = ArrayPrototypePop(instances);
msg +=
`an instance of ${ArrayPrototypeJoin(instances, ', ')}, or ${last}`;
} else {
msg += `an instance of ${instances[0]}`;
if (instances.length === 2) {
Expand All @@ -1041,12 +1061,12 @@ E('ERR_INVALID_ARG_TYPE',

if (other.length > 0) {
if (other.length > 2) {
const last = other.pop();
msg += `one of ${other.join(', ')}, or ${last}`;
const last = ArrayPrototypePop(other);
msg += `one of ${ArrayPrototypeJoin(other, ', ')}, or ${last}`;
} else if (other.length === 2) {
msg += `one of ${other[0]} or ${other[1]}`;
} else {
if (other[0].toLowerCase() !== other[0])
if (StringPrototypeToLowerCase(other[0]) !== other[0])
msg += 'an ';
msg += `${other[0]}`;
}
Expand All @@ -1068,17 +1088,17 @@ E('ERR_INVALID_ARG_TYPE',
let inspected = lazyInternalUtilInspect()
.inspect(actual, { colors: false });
if (inspected.length > 25)
inspected = `${inspected.slice(0, 25)}...`;
inspected = `${StringPrototypeSlice(inspected, 0, 25)}...`;
msg += `. Received type ${typeof actual} (${inspected})`;
}
return msg;
}, TypeError);
E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
let inspected = lazyInternalUtilInspect().inspect(value);
if (inspected.length > 128) {
inspected = `${inspected.slice(0, 128)}...`;
inspected = `${StringPrototypeSlice(inspected, 0, 128)}...`;
}
const type = name.includes('.') ? 'property' : 'argument';
const type = StringPrototypeIncludes(name, '.') ? 'property' : 'argument';
return `The ${type} '${name}' ${reason}. Received ${inspected}`;
}, TypeError, RangeError);
E('ERR_INVALID_ASYNC_ID', 'Invalid %s value: %s', RangeError);
Expand Down Expand Up @@ -1195,9 +1215,10 @@ E('ERR_MANIFEST_ASSERT_INTEGRITY',
moduleURL
}" does not match the expected integrity.`;
if (realIntegrities.size) {
const sri = [...realIntegrities.entries()].map(([alg, dgs]) => {
return `${alg}-${dgs}`;
}).join(' ');
const sri = ArrayPrototypeJoin(
ArrayFrom(realIntegrities.entries(), ([alg, dgs]) => `${alg}-${dgs}`),
' '
);
msg += ` Integrities found are: ${sri}`;
} else {
msg += ' The resource was not found in the policy.';
Expand Down Expand Up @@ -1225,8 +1246,11 @@ E('ERR_MISSING_ARGS',
let msg = 'The ';
const len = args.length;
const wrap = (a) => `"${a}"`;
args = args.map(
(a) => (ArrayIsArray(a) ? a.map(wrap).join(' or ') : wrap(a))
args = ArrayPrototypeMap(
args,
(a) => (ArrayIsArray(a) ?
ArrayPrototypeJoin(ArrayPrototypeMap(a, wrap), ' or ') :
wrap(a))
);
switch (len) {
case 1:
Expand All @@ -1236,7 +1260,7 @@ E('ERR_MISSING_ARGS',
msg += `${args[0]} and ${args[1]} arguments`;
break;
default:
msg += args.slice(0, len - 1).join(', ');
msg += ArrayPrototypeJoin(ArrayPrototypeSlice(args, 0, len - 1), ', ');
msg += `, and ${args[len - 1]} arguments`;
break;
}
Expand Down Expand Up @@ -1299,9 +1323,10 @@ E('ERR_QUIC_INVALID_TLS_SESSION_TICKET',
'Invalid TLS session ticket', Error);
E('ERR_QUIC_VERSION_NEGOTIATION',
(version, requestedVersions, supportedVersions) => {
const requestedVersionsString = ArrayPrototypeJoin(requestedVersions, ', ');
return 'QUIC session received version negotiation from server. ' +
`Version: ${version}. Requested: ${requestedVersions.join(', ')} ` +
`Supported: ${supportedVersions.join(', ')}`;
`Version: ${version}. Requested: ${requestedVersionsString} ` +
`Supported: ${ArrayPrototypeJoin(supportedVersions, ', ')}`;
},
Error);
E('ERR_REQUIRE_ESM',
Expand Down Expand Up @@ -1447,18 +1472,18 @@ E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);
E('ERR_WASI_ALREADY_STARTED', 'WASI instance has already started', Error);
E('ERR_WORKER_INIT_FAILED', 'Worker initialization failure: %s', Error);
E('ERR_WORKER_INVALID_EXEC_ARGV', (errors, msg = 'invalid execArgv flags') =>
`Initiated Worker with ${msg}: ${errors.join(', ')}`,
`Initiated Worker with ${msg}: ${ArrayPrototypeJoin(errors, ', ')}`,
Error);
E('ERR_WORKER_NOT_RUNNING', 'Worker instance not running', Error);
E('ERR_WORKER_OUT_OF_MEMORY',
'Worker terminated due to reaching memory limit: %s', Error);
E('ERR_WORKER_PATH', (filename) =>
'The worker script or module filename must be an absolute path or a ' +
'relative path starting with \'./\' or \'../\'.' +
(filename.startsWith('file://') ?
(StringPrototypeStartsWith(filename, 'file://') ?
' Wrap file:// URLs with `new URL`.' : ''
) +
(filename.startsWith('data:text/javascript') ?
(StringPrototypeStartsWith(filename, 'data:text/javascript') ?
' Wrap data: URLs with `new URL`.' : ''
) +
` Received "${filename}"`,
Expand Down
24 changes: 14 additions & 10 deletions lib/internal/source_map/prepare_stack_trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

const {
ArrayPrototypeIndexOf,
Error,
ArrayPrototypeJoin,
ArrayPrototypeMap,
ErrorPrototypeToString,
StringPrototypeRepeat,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
} = primordials;

Expand All @@ -21,7 +26,6 @@ const { fileURLToPath } = require('internal/url');

// Create a prettified stacktrace, inserting context from source maps
// if possible.
const ErrorToString = Error.prototype.toString; // Capture original toString.
const prepareStackTrace = (globalThis, error, trace) => {
// API for node internals to override error stack formatting
// without interfering with userland code.
Expand All @@ -36,7 +40,7 @@ const prepareStackTrace = (globalThis, error, trace) => {
maybeOverridePrepareStackTrace(globalThis, error, trace);
if (globalOverride !== kNoOverride) return globalOverride;

const errorString = ErrorToString.call(error);
const errorString = ErrorPrototypeToString(error);

if (trace.length === 0) {
return errorString;
Expand All @@ -45,7 +49,7 @@ const prepareStackTrace = (globalThis, error, trace) => {
let errorSource = '';
let firstLine;
let firstColumn;
const preparedTrace = trace.map((t, i) => {
const preparedTrace = ArrayPrototypeJoin(ArrayPrototypeMap(trace, (t, i) => {
if (i === 0) {
firstLine = t.getLineNumber();
firstColumn = t.getColumnNumber();
Expand Down Expand Up @@ -88,8 +92,8 @@ const prepareStackTrace = (globalThis, error, trace) => {
debug(err.stack);
}
return str;
});
return `${errorSource}${errorString}\n at ${preparedTrace.join('')}`;
}), '');
return `${errorSource}${errorString}\n at ${preparedTrace}`;
};

// Places a snippet of code from where the exception was originally thrown
Expand Down Expand Up @@ -118,18 +122,18 @@ function getErrorSource(payload, originalSource, firstLine, firstColumn) {
}
}

const lines = source.split(/\r?\n/, firstLine);
const lines = StringPrototypeSplit(source, /\r?\n/, firstLine);
const line = lines[firstLine - 1];
if (!line) return exceptionLine;

// Display ^ in appropriate position, regardless of whether tabs or
// spaces are used:
let prefix = '';
for (const character of line.slice(0, firstColumn)) {
for (const character of StringPrototypeSlice(line, 0, firstColumn)) {
prefix += (character === '\t') ? '\t' :
' '.repeat(getStringWidth(character));
StringPrototypeRepeat(' ', getStringWidth(character));
}
prefix = prefix.slice(0, -1); // The last character is the '^'.
prefix = StringPrototypeSlice(prefix, 0, -1); // The last character is '^'.

exceptionLine =
`${originalSourceNoScheme}:${firstLine}\n${line}\n${prefix}^\n\n`;
Expand Down