node/lib/internal/util.js
Ben Noordhuis 04b1a2f756 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>
2015-11-30 23:58:32 +01:00

78 lines
2.0 KiB
JavaScript

'use strict';
const binding = process.binding('util');
const prefix = `(${process.release.name}:${process.pid}) `;
exports.getHiddenValue = binding.getHiddenValue;
// All the internal deprecations have to use this function only, as this will
// prepend the prefix to the actual message.
exports.deprecate = function(fn, msg) {
return exports._deprecate(fn, `${prefix}${msg}`);
};
// All the internal deprecations have to use this function only, as this will
// prepend the prefix to the actual message.
exports.printDeprecationMessage = function(msg, warned) {
return exports._printDeprecationMessage(`${prefix}${msg}`, warned);
};
exports._printDeprecationMessage = function(msg, warned) {
if (process.noDeprecation)
return true;
if (warned)
return warned;
if (process.throwDeprecation)
throw new Error(msg);
else if (process.traceDeprecation)
console.trace(msg.startsWith(prefix) ? msg.replace(prefix, '') : msg);
else
console.error(msg);
return true;
};
// Mark that a method should not be used.
// Returns a modified function which warns once by default.
// If --no-deprecation is set, then it is a no-op.
exports._deprecate = function(fn, msg) {
// Allow for deprecating things in the process of starting up.
if (global.process === undefined) {
return function() {
return exports._deprecate(fn, msg).apply(this, arguments);
};
}
if (process.noDeprecation === true) {
return fn;
}
var warned = false;
function deprecated() {
warned = exports._printDeprecationMessage(msg, warned);
return fn.apply(this, arguments);
}
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);
};