lib: consolidate lazyErrmapGet()

There are currently two implementations of this function.
This commit removes the redundancy, and removes "lazy" from
the name.

PR-URL: https://github.com/nodejs/node/pull/29285
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
cjihrig 2019-08-23 15:47:40 -04:00 committed by Rich Trott
parent 66043e1812
commit 627bf59e8d
2 changed files with 12 additions and 20 deletions

View File

@ -327,7 +327,7 @@ function lazyUv() {
return uvBinding; return uvBinding;
} }
function lazyErrmapGet(name) { function uvErrmapGet(name) {
uvBinding = lazyUv(); uvBinding = lazyUv();
if (!uvBinding.errmap) { if (!uvBinding.errmap) {
uvBinding.errmap = uvBinding.getErrorMap(); uvBinding.errmap = uvBinding.getErrorMap();
@ -346,7 +346,7 @@ function lazyErrmapGet(name) {
* @returns {Error} * @returns {Error}
*/ */
function uvException(ctx) { function uvException(ctx) {
const [ code, uvmsg ] = lazyErrmapGet(ctx.errno); const [ code, uvmsg ] = uvErrmapGet(ctx.errno);
let message = `${code}: ${ctx.message || uvmsg}, ${ctx.syscall}`; let message = `${code}: ${ctx.message || uvmsg}, ${ctx.syscall}`;
let path; let path;
@ -404,7 +404,7 @@ function uvException(ctx) {
* @returns {Error} * @returns {Error}
*/ */
function uvExceptionWithHostPort(err, syscall, address, port) { function uvExceptionWithHostPort(err, syscall, address, port) {
const [ code, uvmsg ] = lazyErrmapGet(err); const [ code, uvmsg ] = uvErrmapGet(err);
const message = `${syscall} ${code}: ${uvmsg}`; const message = `${syscall} ${code}: ${uvmsg}`;
let details = ''; let details = '';
@ -671,6 +671,7 @@ module.exports = {
hideStackFrames, hideStackFrames,
isStackOverflowError, isStackOverflowError,
connResetException, connResetException,
uvErrmapGet,
uvException, uvException,
uvExceptionWithHostPort, uvExceptionWithHostPort,
SystemError, SystemError,

View File

@ -1,12 +1,14 @@
'use strict'; 'use strict';
const { Object, Reflect } = primordials; const { Object, Reflect } = primordials;
const { const {
ERR_INVALID_ARG_TYPE, codes: {
ERR_NO_CRYPTO, ERR_INVALID_ARG_TYPE,
ERR_UNKNOWN_SIGNAL ERR_NO_CRYPTO,
} = require('internal/errors').codes; ERR_UNKNOWN_SIGNAL
},
uvErrmapGet
} = require('internal/errors');
const { signals } = internalBinding('constants').os; const { signals } = internalBinding('constants').os;
const { const {
getHiddenValue, getHiddenValue,
@ -244,19 +246,8 @@ function getConstructorOf(obj) {
return null; return null;
} }
let uvBinding;
function lazyErrmapGet(name) {
if (!uvBinding) {
uvBinding = internalBinding('uv');
}
if (!uvBinding.errmap) {
uvBinding.errmap = uvBinding.getErrorMap();
}
return uvBinding.errmap.get(name);
}
function getSystemErrorName(err) { function getSystemErrorName(err) {
const entry = lazyErrmapGet(err); const entry = uvErrmapGet(err);
return entry ? entry[0] : `Unknown system error ${err}`; return entry ? entry[0] : `Unknown system error ${err}`;
} }