Skip to content

Commit 04b1a2f

Browse files
committed
util: move .decorateErrorStack to internal/util
Move the method that was added in commit 8ca412b from earlier this month from lib/util.js to lib/internal/util.js. Avoids exposing a method that we may not wish to expose just yet, seeing how it relies on implementation details. PR-URL: #4026 Reviewed-By: Colin Ihrig <[email protected]>
1 parent ee72ee7 commit 04b1a2f

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

lib/internal/util.js

+18
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,21 @@ exports._deprecate = function(fn, msg) {
5757

5858
return deprecated;
5959
};
60+
61+
exports.decorateErrorStack = function decorateErrorStack(err) {
62+
if (!(exports.isError(err) && err.stack))
63+
return;
64+
65+
const arrow = exports.getHiddenValue(err, 'arrowMessage');
66+
67+
if (arrow)
68+
err.stack = arrow + err.stack;
69+
};
70+
71+
exports.isError = function isError(e) {
72+
return exports.objectToString(e) === '[object Error]' || e instanceof Error;
73+
};
74+
75+
exports.objectToString = function objectToString(o) {
76+
return Object.prototype.toString.call(o);
77+
};

lib/repl.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const internalModule = require('internal/module');
25+
const internalUtil = require('internal/util');
2526
const util = require('util');
2627
const inherits = util.inherits;
2728
const Stream = require('stream');
@@ -276,7 +277,7 @@ function REPLServer(prompt,
276277
self._domain.on('error', function(e) {
277278
debug('domain error');
278279
const top = replMap.get(self);
279-
util.decorateErrorStack(e);
280+
internalUtil.decorateErrorStack(e);
280281
top.outputStream.write((e.stack || e) + '\n');
281282
top.lineParser.reset();
282283
top.bufferedCommand = '';

lib/util.js

+3-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const Buffer = require('buffer').Buffer;
55
const internalUtil = require('internal/util');
66
const binding = process.binding('util');
77

8+
const isError = internalUtil.isError;
9+
const objectToString = internalUtil.objectToString;
10+
811
var Debug;
912

1013
const formatRegExp = /%[sdj%]/g;
@@ -739,9 +742,6 @@ function isDate(d) {
739742
}
740743
exports.isDate = isDate;
741744

742-
function isError(e) {
743-
return objectToString(e) === '[object Error]' || e instanceof Error;
744-
}
745745
exports.isError = isError;
746746

747747
function isFunction(arg) {
@@ -757,10 +757,6 @@ exports.isPrimitive = isPrimitive;
757757

758758
exports.isBuffer = Buffer.isBuffer;
759759

760-
function objectToString(o) {
761-
return Object.prototype.toString.call(o);
762-
}
763-
764760

765761
function pad(n) {
766762
return n < 10 ? '0' + n.toString(10) : n.toString(10);
@@ -899,14 +895,3 @@ exports._exceptionWithHostPort = function(err,
899895
}
900896
return ex;
901897
};
902-
903-
904-
exports.decorateErrorStack = function(err) {
905-
if (!(isError(err) && err.stack))
906-
return;
907-
908-
const arrow = internalUtil.getHiddenValue(err, 'arrowMessage');
909-
910-
if (arrow)
911-
err.stack = arrow + err.stack;
912-
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
// Flags: --expose_internals
12
'use strict';
23
const common = require('../common');
34
const assert = require('assert');
4-
const util = require('util');
5+
const internalUtil = require('internal/util');
56

67
assert.doesNotThrow(function() {
7-
util.decorateErrorStack();
8-
util.decorateErrorStack(null);
9-
util.decorateErrorStack(1);
10-
util.decorateErrorStack(true);
8+
internalUtil.decorateErrorStack();
9+
internalUtil.decorateErrorStack(null);
10+
internalUtil.decorateErrorStack(1);
11+
internalUtil.decorateErrorStack(true);
1112
});
1213

1314
// Verify that a stack property is not added to non-Errors
1415
const obj = {};
15-
util.decorateErrorStack(obj);
16+
internalUtil.decorateErrorStack(obj);
1617
assert.strictEqual(obj.stack, undefined);
1718

1819
// Verify that the stack is decorated when possible
@@ -23,13 +24,13 @@ try {
2324
} catch (e) {
2425
err = e;
2526
assert(!/var foo bar;/.test(err.stack));
26-
util.decorateErrorStack(err);
27+
internalUtil.decorateErrorStack(err);
2728
}
2829

2930
assert(/var foo bar;/.test(err.stack));
3031

3132
// Verify that the stack is unchanged when there is no arrow message
3233
err = new Error('foo');
3334
const originalStack = err.stack;
34-
util.decorateErrorStack(err);
35+
internalUtil.decorateErrorStack(err);
3536
assert.strictEqual(originalStack, err.stack);

0 commit comments

Comments
 (0)