Skip to content

Commit

Permalink
lib: reduce usage of require('util')
Browse files Browse the repository at this point in the history
Replace `require('util').inspect` and `require('util').format` with
`require('util/internal/inspect').inspect` and
`require('util/internal/inspect').format` in `lib/internal/errors.js`.

PR-URL: #26782
Refs: #26546
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
dnlup authored and BethGriggs committed Mar 26, 2019
1 parent 5d4c0db commit dede0e1
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ function lazyInternalUtil() {
return internalUtil;
}

let internalUtilInspect = null;
function lazyInternalUtilInspect() {
if (!internalUtilInspect) {
internalUtilInspect = require('internal/util/inspect');
}
return internalUtilInspect;
}

let buffer;
function lazyBuffer() {
if (buffer === undefined)
Expand Down Expand Up @@ -260,7 +268,6 @@ function E(sym, val, def, ...otherClasses) {
function getMessage(key, args, self) {
const msg = messages.get(key);

if (util === undefined) util = require('util');
if (assert === undefined) assert = require('internal/assert');

if (typeof msg === 'function') {
Expand All @@ -282,7 +289,7 @@ function getMessage(key, args, self) {
return msg;

args.unshift(msg);
return util.format.apply(null, args);
return lazyInternalUtilInspect().format.apply(null, args);
}

let uvBinding;
Expand Down Expand Up @@ -820,7 +827,7 @@ E('ERR_INVALID_ARG_TYPE',
return msg;
}, TypeError);
E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
let inspected = util.inspect(value);
let inspected = lazyInternalUtilInspect().inspect(value);
if (inspected.length > 128) {
inspected = `${inspected.slice(0, 128)}...`;
}
Expand Down

0 comments on commit dede0e1

Please sign in to comment.