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

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


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

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,
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,
promisify
} = require('internal/util');

Expand Down Expand Up @@ -257,6 +258,7 @@ module.exports = {
format,
formatWithOptions,
getSystemErrorName,
getSystemErrorMap,
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';

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