Skip to content

Commit

Permalink
util: move .decorateErrorStack to internal/util
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bnoordhuis authored and rvagg committed Dec 5, 2015
1 parent 8988e1e commit 098a311
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
18 changes: 18 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,21 @@ exports._deprecate = function(fn, msg) {

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);
};
3 changes: 2 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const internalModule = require('internal/module');
const internalUtil = require('internal/util');
const util = require('util');
const inherits = util.inherits;
const Stream = require('stream');
Expand Down Expand Up @@ -276,7 +277,7 @@ function REPLServer(prompt,
self._domain.on('error', function(e) {
debug('domain error');
const top = replMap.get(self);
util.decorateErrorStack(e);
internalUtil.decorateErrorStack(e);
top.outputStream.write((e.stack || e) + '\n');
top.lineParser.reset();
top.bufferedCommand = '';
Expand Down
21 changes: 3 additions & 18 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const Buffer = require('buffer').Buffer;
const internalUtil = require('internal/util');
const binding = process.binding('util');

const isError = internalUtil.isError;
const objectToString = internalUtil.objectToString;

var Debug;

const formatRegExp = /%[sdj%]/g;
Expand Down Expand Up @@ -680,9 +683,6 @@ function isDate(d) {
}
exports.isDate = isDate;

function isError(e) {
return objectToString(e) === '[object Error]' || e instanceof Error;
}
exports.isError = isError;

function isFunction(arg) {
Expand All @@ -698,10 +698,6 @@ exports.isPrimitive = isPrimitive;

exports.isBuffer = Buffer.isBuffer;

function objectToString(o) {
return Object.prototype.toString.call(o);
}


function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
Expand Down Expand Up @@ -883,14 +879,3 @@ exports._exceptionWithHostPort = function(err,
}
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;
};
17 changes: 9 additions & 8 deletions test/parallel/test-util-decorate-error-stack.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// Flags: --expose_internals
'use strict';
const common = require('../common');
const assert = require('assert');
const util = require('util');
const internalUtil = require('internal/util');

assert.doesNotThrow(function() {
util.decorateErrorStack();
util.decorateErrorStack(null);
util.decorateErrorStack(1);
util.decorateErrorStack(true);
internalUtil.decorateErrorStack();
internalUtil.decorateErrorStack(null);
internalUtil.decorateErrorStack(1);
internalUtil.decorateErrorStack(true);
});

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

// Verify that the stack is decorated when possible
Expand All @@ -23,13 +24,13 @@ try {
} catch (e) {
err = e;
assert(!/var foo bar;/.test(err.stack));
util.decorateErrorStack(err);
internalUtil.decorateErrorStack(err);
}

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

// Verify that the stack is unchanged when there is no arrow message
err = new Error('foo');
const originalStack = err.stack;
util.decorateErrorStack(err);
internalUtil.decorateErrorStack(err);
assert.strictEqual(originalStack, err.stack);

0 comments on commit 098a311

Please sign in to comment.