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: https://github.com/nodejs/node/pull/4026 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
ee72ee7531
commit
04b1a2f756
@ -57,3 +57,21 @@ exports._deprecate = function(fn, msg) {
|
|||||||
|
|
||||||
return deprecated;
|
return deprecated;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.decorateErrorStack = function decorateErrorStack(err) {
|
||||||
|
if (!(exports.isError(err) && err.stack))
|
||||||
|
return;
|
||||||
|
|
||||||
|
const arrow = exports.getHiddenValue(err, 'arrowMessage');
|
||||||
|
|
||||||
|
if (arrow)
|
||||||
|
err.stack = arrow + err.stack;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.isError = function isError(e) {
|
||||||
|
return exports.objectToString(e) === '[object Error]' || e instanceof Error;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.objectToString = function objectToString(o) {
|
||||||
|
return Object.prototype.toString.call(o);
|
||||||
|
};
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const internalModule = require('internal/module');
|
const internalModule = require('internal/module');
|
||||||
|
const internalUtil = require('internal/util');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const inherits = util.inherits;
|
const inherits = util.inherits;
|
||||||
const Stream = require('stream');
|
const Stream = require('stream');
|
||||||
@ -276,7 +277,7 @@ function REPLServer(prompt,
|
|||||||
self._domain.on('error', function(e) {
|
self._domain.on('error', function(e) {
|
||||||
debug('domain error');
|
debug('domain error');
|
||||||
const top = replMap.get(self);
|
const top = replMap.get(self);
|
||||||
util.decorateErrorStack(e);
|
internalUtil.decorateErrorStack(e);
|
||||||
top.outputStream.write((e.stack || e) + '\n');
|
top.outputStream.write((e.stack || e) + '\n');
|
||||||
top.lineParser.reset();
|
top.lineParser.reset();
|
||||||
top.bufferedCommand = '';
|
top.bufferedCommand = '';
|
||||||
|
21
lib/util.js
21
lib/util.js
@ -5,6 +5,9 @@ const Buffer = require('buffer').Buffer;
|
|||||||
const internalUtil = require('internal/util');
|
const internalUtil = require('internal/util');
|
||||||
const binding = process.binding('util');
|
const binding = process.binding('util');
|
||||||
|
|
||||||
|
const isError = internalUtil.isError;
|
||||||
|
const objectToString = internalUtil.objectToString;
|
||||||
|
|
||||||
var Debug;
|
var Debug;
|
||||||
|
|
||||||
const formatRegExp = /%[sdj%]/g;
|
const formatRegExp = /%[sdj%]/g;
|
||||||
@ -739,9 +742,6 @@ function isDate(d) {
|
|||||||
}
|
}
|
||||||
exports.isDate = isDate;
|
exports.isDate = isDate;
|
||||||
|
|
||||||
function isError(e) {
|
|
||||||
return objectToString(e) === '[object Error]' || e instanceof Error;
|
|
||||||
}
|
|
||||||
exports.isError = isError;
|
exports.isError = isError;
|
||||||
|
|
||||||
function isFunction(arg) {
|
function isFunction(arg) {
|
||||||
@ -757,10 +757,6 @@ exports.isPrimitive = isPrimitive;
|
|||||||
|
|
||||||
exports.isBuffer = Buffer.isBuffer;
|
exports.isBuffer = Buffer.isBuffer;
|
||||||
|
|
||||||
function objectToString(o) {
|
|
||||||
return Object.prototype.toString.call(o);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function pad(n) {
|
function pad(n) {
|
||||||
return n < 10 ? '0' + n.toString(10) : n.toString(10);
|
return n < 10 ? '0' + n.toString(10) : n.toString(10);
|
||||||
@ -899,14 +895,3 @@ exports._exceptionWithHostPort = function(err,
|
|||||||
}
|
}
|
||||||
return ex;
|
return ex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
exports.decorateErrorStack = function(err) {
|
|
||||||
if (!(isError(err) && err.stack))
|
|
||||||
return;
|
|
||||||
|
|
||||||
const arrow = internalUtil.getHiddenValue(err, 'arrowMessage');
|
|
||||||
|
|
||||||
if (arrow)
|
|
||||||
err.stack = arrow + err.stack;
|
|
||||||
};
|
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
|
// Flags: --expose_internals
|
||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const util = require('util');
|
const internalUtil = require('internal/util');
|
||||||
|
|
||||||
assert.doesNotThrow(function() {
|
assert.doesNotThrow(function() {
|
||||||
util.decorateErrorStack();
|
internalUtil.decorateErrorStack();
|
||||||
util.decorateErrorStack(null);
|
internalUtil.decorateErrorStack(null);
|
||||||
util.decorateErrorStack(1);
|
internalUtil.decorateErrorStack(1);
|
||||||
util.decorateErrorStack(true);
|
internalUtil.decorateErrorStack(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Verify that a stack property is not added to non-Errors
|
// Verify that a stack property is not added to non-Errors
|
||||||
const obj = {};
|
const obj = {};
|
||||||
util.decorateErrorStack(obj);
|
internalUtil.decorateErrorStack(obj);
|
||||||
assert.strictEqual(obj.stack, undefined);
|
assert.strictEqual(obj.stack, undefined);
|
||||||
|
|
||||||
// Verify that the stack is decorated when possible
|
// Verify that the stack is decorated when possible
|
||||||
@ -23,7 +24,7 @@ try {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
err = e;
|
err = e;
|
||||||
assert(!/var foo bar;/.test(err.stack));
|
assert(!/var foo bar;/.test(err.stack));
|
||||||
util.decorateErrorStack(err);
|
internalUtil.decorateErrorStack(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(/var foo bar;/.test(err.stack));
|
assert(/var foo bar;/.test(err.stack));
|
||||||
@ -31,5 +32,5 @@ assert(/var foo bar;/.test(err.stack));
|
|||||||
// Verify that the stack is unchanged when there is no arrow message
|
// Verify that the stack is unchanged when there is no arrow message
|
||||||
err = new Error('foo');
|
err = new Error('foo');
|
||||||
const originalStack = err.stack;
|
const originalStack = err.stack;
|
||||||
util.decorateErrorStack(err);
|
internalUtil.decorateErrorStack(err);
|
||||||
assert.strictEqual(originalStack, err.stack);
|
assert.strictEqual(originalStack, err.stack);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user