Skip to content

Commit

Permalink
errors: lazy load util in internal/errors.js
Browse files Browse the repository at this point in the history
Backport-PR-URL: #19191
PR-URL: #18358
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
  • Loading branch information
joyeecheung authored and rvagg committed Aug 16, 2018
1 parent 58b5a61 commit 9696bf9
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ const messages = new Map();
const { defineProperty } = Object;

// Lazily loaded
var util = null;
var util_ = null;
function lazyUtil() {
if (!util_) {
util_ = require('util');
}
return util_;
}

function makeNodeError(Base) {
return class NodeError extends Base {
Expand Down Expand Up @@ -65,11 +71,11 @@ class AssertionError extends Error {
if (message) {
super(message);
} else {
const util = lazyUtil();
if (actual && actual.stack && actual instanceof Error)
actual = `${actual.name}: ${actual.message}`;
if (expected && expected.stack && expected instanceof Error)
expected = `${expected.name}: ${expected.message}`;
if (util === null) util = require('util');
super(`${util.inspect(actual).slice(0, 128)} ` +
`${operator} ${util.inspect(expected).slice(0, 128)}`);
}
Expand Down Expand Up @@ -104,7 +110,7 @@ function message(key, args) {
if (typeof msg === 'function') {
fmt = msg;
} else {
if (util === null) util = require('util');
const util = lazyUtil();
fmt = util.format;
if (args === undefined || args.length === 0)
return msg;
Expand Down

0 comments on commit 9696bf9

Please sign in to comment.