Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
util: add decorateErrorStack()
This commit adds the decorateErrorStack() method. This function
uses the internal util's getHiddenValue() method to extract
arrow messages from error objects and attach them to the
error's stack trace.

PR-URL: #4013
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
cjihrig committed Nov 25, 2015
commit 8ca412b7a3d5e6e84441bc846c5f42e8dcd2f1b9
11 changes: 11 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,14 @@ exports._exceptionWithHostPort = function(err,
}
return ex;
};


exports.decorateErrorStack = function(err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason it's not in internal/util ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I tried using internal/util, but the REPL doesn't seem to like it. It gives this error when starting node:

Error: Cannot find module 'internal/util'

if (!(isError(err) && err.stack))
return;

const arrow = internalUtil.getHiddenValue(err, 'arrowMessage');

if (arrow)
err.stack = arrow + err.stack;
};
35 changes: 35 additions & 0 deletions test/parallel/test-util-decorate-error-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const util = require('util');

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

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

// Verify that the stack is decorated when possible
let err;

try {
require('../fixtures/syntax/bad_syntax');
} catch (e) {
err = e;
assert(!/var foo bar;/.test(err.stack));
util.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);
assert.strictEqual(originalStack, err.stack);