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

19 changes: 19 additions & 0 deletions doc/api/util.md
Expand Up @@ -376,6 +376,25 @@ fs.access('file/that/does/not/exist', (err) => {
});
```

## `util.getSystemErrorMap()`
<!-- YAML
added: REPLACEME
-->

* Returns: {Map}

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.

```js
fs.access('file/that/does/not/exist', (err) => {
const errorMap = util.getSystemErrorMap();
const name = errorMap.get(err.errno);
EladKeyshawn marked this conversation as resolved.
Show resolved Hide resolved
console.error(name); // ENOENT
});
```

## `util.inherits(constructor, superConstructor)`
<!-- YAML
added: v0.3.0
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/util.js
Expand Up @@ -53,6 +53,13 @@ const experimentalWarnings = new SafeSet();

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

let uvBinding;

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

function removeColors(str) {
return StringPrototypeReplace(str, colorRegExp, '');
}
Expand Down Expand Up @@ -286,6 +293,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 @@ -442,6 +453,7 @@ module.exports = {
emitExperimentalWarning,
filterDuplicateStrings,
getConstructorOf,
getSystemErrorMap,
getSystemErrorName,
isError,
isInsideNodeModules,
Expand Down
2 changes: 2 additions & 0 deletions lib/util.js
Expand Up @@ -67,6 +67,7 @@ const types = require('internal/util/types');

const {
deprecate,
getSystemErrorMap,
getSystemErrorName: internalErrorName,
promisify
} = require('internal/util');
Expand Down Expand Up @@ -256,6 +257,7 @@ module.exports = {
deprecate,
format,
formatWithOptions,
getSystemErrorMap,
getSystemErrorName,
inherits,
inspect,
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-uv-errmap.js
@@ -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);
});