Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestion for getSystemErrorMap() implementation #38101

16 changes: 15 additions & 1 deletion doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
<!-- YAML
added: v9.7.0
EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved
-->

* Returns: {Map}

Returns a Map of all system error codes available from Node.js API.
EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved
The mapping between error codes and error names is platform-dependent.
See [Common System Errors][] for the names of common errors.



EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved
```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);
EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved
console.error(name); // ENOENT
});
```
Expand Down
13 changes: 13 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ const experimentalWarnings = new SafeSet();

const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex


EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved
let uvBinding;

function lazyUv() {
uvBinding ??= internalBinding('uv');
return uvBinding;
}

function removeColors(str) {
return StringPrototypeReplace(str, colorRegExp, '');
}
Expand Down Expand Up @@ -286,6 +294,10 @@ function getSystemErrorName(err) {
return entry ? entry[0] : `Unknown system error ${err}`;
}

function getSystemErrorMap() {
return lazyUv().getErrorMap();
}
EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved

const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom');
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');

Expand Down Expand Up @@ -443,6 +455,7 @@ module.exports = {
filterDuplicateStrings,
getConstructorOf,
getSystemErrorName,
getSystemErrorMap,
EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved
isError,
isInsideNodeModules,
join,
Expand Down
2 changes: 2 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const types = require('internal/util/types');
const {
deprecate,
getSystemErrorName: internalErrorName,
getSystemErrorMap,
EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved
promisify
} = require('internal/util');

Expand Down Expand Up @@ -257,6 +258,7 @@ module.exports = {
format,
formatWithOptions,
getSystemErrorName,
getSystemErrorMap,
EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved
inherits,
inspect,
isArray: ArrayIsArray,
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-uv-errmap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Flags: --expose-internals
'use strict';

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);
});