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 jasnell committed Apr 13, 2021
1 parent 4def7c4 commit 8021710
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}

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
Original file line number Diff line number Diff line change
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
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);
});

0 comments on commit 8021710

Please sign in to comment.