From e0f0fc81a4529f9f1eb61a1474276ae04ee97e03 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Tue, 6 Apr 2021 00:18:34 +0300 Subject: [PATCH 01/14] suggestion for getSystemErrorMap() impl --- lib/internal/errors.js | 6 ++++++ lib/internal/util.js | 6 ++++++ lib/util.js | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 34c168ce38ab91..4a4b12ff0574b8 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -428,6 +428,11 @@ function uvErrmapGet(name) { return MapPrototypeGet(uvBinding.errmap, name); } +function uvGetFullErrMap() { + uvBinding = lazyUv(); + return uvBinding.getErrorMap(); +} + const captureLargerStackTrace = hideStackFrames( function captureLargerStackTrace(err) { userStackTraceLimit = Error.stackTraceLimit; @@ -782,6 +787,7 @@ module.exports = { isStackOverflowError, connResetException, uvErrmapGet, + uvGetFullErrMap, uvException, uvExceptionWithHostPort, SystemError, diff --git a/lib/internal/util.js b/lib/internal/util.js index 46eabc6f08c092..ec3db0ed6ef432 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -35,6 +35,7 @@ const { ERR_UNKNOWN_SIGNAL }, uvErrmapGet, + uvGetFullErrMap, overrideStackTrace, } = require('internal/errors'); const { signals } = internalBinding('constants').os; @@ -286,6 +287,10 @@ function getSystemErrorName(err) { return entry ? entry[0] : `Unknown system error ${err}`; } +function getSystemErrorMap() { + return uvGetFullErrMap(); + } + const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom'); const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs'); @@ -443,6 +448,7 @@ module.exports = { filterDuplicateStrings, getConstructorOf, getSystemErrorName, + getSystemErrorMap, isError, isInsideNodeModules, join, diff --git a/lib/util.js b/lib/util.js index abd1f6e663610e..38eaffe0b0cd2e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -68,6 +68,7 @@ const types = require('internal/util/types'); const { deprecate, getSystemErrorName: internalErrorName, + getSystemErrorMap: internalErrorMap, promisify } = require('internal/util'); @@ -245,6 +246,9 @@ function getSystemErrorName(err) { return internalErrorName(err); } +function getSystemErrorMap() { + return internalErrorMap(); +} // Keep the `exports =` so that various functions can still be monkeypatched module.exports = { _errnoException: errnoException, @@ -257,6 +261,7 @@ module.exports = { format, formatWithOptions, getSystemErrorName, + getSystemErrorMap, inherits, inspect, isArray: ArrayIsArray, From 54c6e38fd608a0d7d21d6fa54d81389c46191854 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Tue, 6 Apr 2021 00:35:52 +0300 Subject: [PATCH 02/14] lint --- lib/internal/errors.js | 4 ++-- lib/internal/util.js | 4 ++-- lib/util.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 4a4b12ff0574b8..be3c2c89110c52 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -429,8 +429,8 @@ function uvErrmapGet(name) { } function uvGetFullErrMap() { - uvBinding = lazyUv(); - return uvBinding.getErrorMap(); + uvBinding = lazyUv(); + return uvBinding.getErrorMap(); } const captureLargerStackTrace = hideStackFrames( diff --git a/lib/internal/util.js b/lib/internal/util.js index ec3db0ed6ef432..dd0339e7339434 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -288,8 +288,8 @@ function getSystemErrorName(err) { } function getSystemErrorMap() { - return uvGetFullErrMap(); - } + return uvGetFullErrMap(); +} const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom'); const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs'); diff --git a/lib/util.js b/lib/util.js index 38eaffe0b0cd2e..e4906f5b316a97 100644 --- a/lib/util.js +++ b/lib/util.js @@ -247,7 +247,7 @@ function getSystemErrorName(err) { } function getSystemErrorMap() { - return internalErrorMap(); + return internalErrorMap(); } // Keep the `exports =` so that various functions can still be monkeypatched module.exports = { From 36082c2a5e788c8e55bb10e7aefddbe4b92c0b67 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Tue, 6 Apr 2021 21:30:41 +0300 Subject: [PATCH 03/14] avoid unneeded call indirection --- lib/internal/errors.js | 6 ------ lib/internal/util.js | 14 ++++++++++++-- lib/util.js | 6 +----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index be3c2c89110c52..34c168ce38ab91 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -428,11 +428,6 @@ function uvErrmapGet(name) { return MapPrototypeGet(uvBinding.errmap, name); } -function uvGetFullErrMap() { - uvBinding = lazyUv(); - return uvBinding.getErrorMap(); -} - const captureLargerStackTrace = hideStackFrames( function captureLargerStackTrace(err) { userStackTraceLimit = Error.stackTraceLimit; @@ -787,7 +782,6 @@ module.exports = { isStackOverflowError, connResetException, uvErrmapGet, - uvGetFullErrMap, uvException, uvExceptionWithHostPort, SystemError, diff --git a/lib/internal/util.js b/lib/internal/util.js index dd0339e7339434..c58014173a0cc8 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -35,7 +35,6 @@ const { ERR_UNKNOWN_SIGNAL }, uvErrmapGet, - uvGetFullErrMap, overrideStackTrace, } = require('internal/errors'); const { signals } = internalBinding('constants').os; @@ -54,6 +53,16 @@ const experimentalWarnings = new SafeSet(); const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex + +let uvBinding; + +function lazyUv() { + if (!uvBinding) { + uvBinding = internalBinding('uv'); + } + return uvBinding; +} + function removeColors(str) { return StringPrototypeReplace(str, colorRegExp, ''); } @@ -288,7 +297,8 @@ function getSystemErrorName(err) { } function getSystemErrorMap() { - return uvGetFullErrMap(); + uvBinding = lazyUv(); + return uvBinding.getErrorMap(); } const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom'); diff --git a/lib/util.js b/lib/util.js index e4906f5b316a97..57f80b7a5faf59 100644 --- a/lib/util.js +++ b/lib/util.js @@ -68,7 +68,7 @@ const types = require('internal/util/types'); const { deprecate, getSystemErrorName: internalErrorName, - getSystemErrorMap: internalErrorMap, + getSystemErrorMap, promisify } = require('internal/util'); @@ -245,10 +245,6 @@ function getSystemErrorName(err) { } return internalErrorName(err); } - -function getSystemErrorMap() { - return internalErrorMap(); -} // Keep the `exports =` so that various functions can still be monkeypatched module.exports = { _errnoException: errnoException, From 7ea64bb220213e916feca2af914c14a84e8eb9c6 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Wed, 7 Apr 2021 22:34:08 +0300 Subject: [PATCH 04/14] syntax sugar and minor changes --- lib/internal/util.js | 7 ++----- lib/util.js | 1 + 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/internal/util.js b/lib/internal/util.js index c58014173a0cc8..081c2266c99649 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -57,9 +57,7 @@ const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex let uvBinding; function lazyUv() { - if (!uvBinding) { - uvBinding = internalBinding('uv'); - } + uvBinding ??= internalBinding('uv'); return uvBinding; } @@ -297,8 +295,7 @@ function getSystemErrorName(err) { } function getSystemErrorMap() { - uvBinding = lazyUv(); - return uvBinding.getErrorMap(); + return lazyUv().getErrorMap(); } const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom'); diff --git a/lib/util.js b/lib/util.js index 57f80b7a5faf59..5a122aed2b3454 100644 --- a/lib/util.js +++ b/lib/util.js @@ -245,6 +245,7 @@ function getSystemErrorName(err) { } return internalErrorName(err); } + // Keep the `exports =` so that various functions can still be monkeypatched module.exports = { _errnoException: errnoException, From 306bdf7ccca1d7bcca804766ca34dc9324e27994 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Fri, 9 Apr 2021 11:29:08 +0300 Subject: [PATCH 05/14] add basic uv-errmap test --- test/parallel/test-uv-errmap.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 test/parallel/test-uv-errmap.js diff --git a/test/parallel/test-uv-errmap.js b/test/parallel/test-uv-errmap.js new file mode 100644 index 00000000000000..19b7f57f214d5c --- /dev/null +++ b/test/parallel/test-uv-errmap.js @@ -0,0 +1,24 @@ +// Flags: --expose-internals +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { + getSystemErrorMap, + _errnoException +} = require('util'); + +const { internalBinding } = require('internal/test/binding'); +const uv = internalBinding('uv'); +const uvKeys = Object.keys(uv); + +const errMap = getSystemErrorMap(); + +uvKeys.forEach((key) => { + if (!key.startsWith('UV_')) + return; + + const err = _errnoException(uv[key]); + const name = uv.errname(uv[key]); + assert.strictEqual(errMap.get(err.errno)[0], name); +}); From 792a9b5df7efcdac2a7c27bad15a7a8d6a47ef64 Mon Sep 17 00:00:00 2001 From: Elad Keyshawn Date: Fri, 9 Apr 2021 17:14:43 +0300 Subject: [PATCH 06/14] make linter happy Co-authored-by: Darshan Sen --- test/parallel/test-uv-errmap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-uv-errmap.js b/test/parallel/test-uv-errmap.js index 19b7f57f214d5c..6a077551b63d77 100644 --- a/test/parallel/test-uv-errmap.js +++ b/test/parallel/test-uv-errmap.js @@ -1,7 +1,7 @@ // Flags: --expose-internals 'use strict'; -const common = require('../common'); +require('../common'); const assert = require('assert'); const { getSystemErrorMap, From 87fb10e82c1f66a343817a519a23364f9cacadb2 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Fri, 9 Apr 2021 17:33:16 +0300 Subject: [PATCH 07/14] add API doc --- doc/api/util.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index d6be620b42c56d..04a66d5207f7b0 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -369,9 +369,23 @@ Returns the string name for a numeric error code that comes from a Node.js API. The mapping between error codes and error names is platform-dependent. See [Common System Errors][] for the names of common errors. +## `util.getSystemErrorMap()` + + +* Returns: {Map} + +Returns a Map of all system error codes available from Node.js API. +The mapping between error codes and error names is platform-dependent. +See [Common System Errors][] for the names of common errors. + + + ```js fs.access('file/that/does/not/exist', (err) => { - const name = util.getSystemErrorName(err.errno); + const errorMap = util.getSystemErrorMap(); + const name = errorMap.get(err.errno); console.error(name); // ENOENT }); ``` From 6d8e95ee9911f0ea54daac026b05565929540772 Mon Sep 17 00:00:00 2001 From: Elad Keyshawn Date: Fri, 9 Apr 2021 20:17:59 +0300 Subject: [PATCH 08/14] rm line Co-authored-by: Richard Lau --- doc/api/util.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index 04a66d5207f7b0..0265379fd9e2b4 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -381,7 +381,6 @@ The mapping between error codes and error names is platform-dependent. See [Common System Errors][] for the names of common errors. - ```js fs.access('file/that/does/not/exist', (err) => { const errorMap = util.getSystemErrorMap(); From a5d1b2c94b678df6aee04f78e769fa66e75da061 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Fri, 9 Apr 2021 20:22:15 +0300 Subject: [PATCH 09/14] add back example of getSystemErrorName --- doc/api/util.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/api/util.md b/doc/api/util.md index 0265379fd9e2b4..05c64156e50b18 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -369,6 +369,13 @@ Returns the string name for a numeric error code that comes from a Node.js API. The mapping between error codes and error names is platform-dependent. See [Common System Errors][] for the names of common errors. +```js +fs.access('file/that/does/not/exist', (err) => { + const name = util.getSystemErrorName(err.errno); + console.error(name); // ENOENT +}); +``` + ## `util.getSystemErrorMap()` * Returns: {Map} From a4436684882489aca62e9feca13f608643a651e3 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Fri, 9 Apr 2021 22:03:54 +0300 Subject: [PATCH 11/14] doc lint --- doc/api/util.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index ffe275709ae2d1..d57d7a654a4e79 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -387,7 +387,6 @@ Returns a Map of all system error codes available from Node.js API. The mapping between error codes and error names is platform-dependent. See [Common System Errors][] for the names of common errors. - ```js fs.access('file/that/does/not/exist', (err) => { const errorMap = util.getSystemErrorMap(); From 034779c65a5b0e23db9ffdf5f687c33339578ce7 Mon Sep 17 00:00:00 2001 From: Elad Keyshawn Date: Sat, 10 Apr 2021 08:39:16 +0300 Subject: [PATCH 12/14] Apply suggestions from code review Co-authored-by: Antoine du Hamel --- lib/internal/util.js | 2 +- lib/util.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/util.js b/lib/internal/util.js index 081c2266c99649..643af481932ed4 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -454,8 +454,8 @@ module.exports = { emitExperimentalWarning, filterDuplicateStrings, getConstructorOf, - getSystemErrorName, getSystemErrorMap, + getSystemErrorName, isError, isInsideNodeModules, join, diff --git a/lib/util.js b/lib/util.js index 5a122aed2b3454..f3c10bb7720e58 100644 --- a/lib/util.js +++ b/lib/util.js @@ -67,8 +67,8 @@ const types = require('internal/util/types'); const { deprecate, - getSystemErrorName: internalErrorName, getSystemErrorMap, + getSystemErrorName: internalErrorName, promisify } = require('internal/util'); @@ -257,8 +257,8 @@ module.exports = { deprecate, format, formatWithOptions, - getSystemErrorName, getSystemErrorMap, + getSystemErrorName, inherits, inspect, isArray: ArrayIsArray, From 0e79eb9a7dc056a7d12f7757859e24d376cc75a2 Mon Sep 17 00:00:00 2001 From: Elad Keyshawn Date: Sat, 10 Apr 2021 18:13:53 +0300 Subject: [PATCH 13/14] rm line Co-authored-by: Darshan Sen --- lib/internal/util.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/internal/util.js b/lib/internal/util.js index 643af481932ed4..eb0f6d99f1a1e6 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -53,7 +53,6 @@ const experimentalWarnings = new SafeSet(); const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex - let uvBinding; function lazyUv() { From 02b338ee9904028b58dcc2b0c3b8e258a7f24fb3 Mon Sep 17 00:00:00 2001 From: eladkeyshawn Date: Sun, 11 Apr 2021 09:15:26 +0300 Subject: [PATCH 14/14] Fix typo in util.md doc --- doc/api/util.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index d57d7a654a4e79..94d41cd22bf92c 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -383,7 +383,7 @@ added: REPLACEME * Returns: {Map} -Returns a Map of all system error codes available from Node.js API. +Returns a Map of all system error codes available from the Node.js API. The mapping between error codes and error names is platform-dependent. See [Common System Errors][] for the names of common errors.