Skip to content

Commit

Permalink
util: add getSystemErrorMap() impl
Browse files Browse the repository at this point in the history
PR-URL: #38101
Fixes: #37951
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
eladkishon authored and targos committed May 1, 2021
1 parent e6c599b commit 08a6d9e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
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);
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 @@ -44,6 +44,13 @@ const experimentalWarnings = new Set();

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

let uvBinding;

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

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

function getSystemErrorMap() {
return lazyUv().getErrorMap();
}

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

Expand Down Expand Up @@ -414,6 +425,7 @@ module.exports = {
emitExperimentalWarning,
filterDuplicateStrings,
getConstructorOf,
getSystemErrorMap,
getSystemErrorName,
isError,
isInsideNodeModules,
Expand Down
2 changes: 2 additions & 0 deletions lib/util.js
Expand Up @@ -57,6 +57,7 @@ const types = require('internal/util/types');

const {
deprecate,
getSystemErrorMap,
getSystemErrorName: internalErrorName,
promisify
} = require('internal/util');
Expand Down Expand Up @@ -244,6 +245,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);
});

0 comments on commit 08a6d9e

Please sign in to comment.