Skip to content

Commit 8ca412b

Browse files
committedNov 25, 2015
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 <[email protected]>
1 parent 16db4c4 commit 8ca412b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
 

‎lib/util.js

+11
Original file line numberDiff line numberDiff line change
@@ -899,3 +899,14 @@ exports._exceptionWithHostPort = function(err,
899899
}
900900
return ex;
901901
};
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
@@ -0,0 +1,35 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const util = require('util');
5+
6+
assert.doesNotThrow(function() {
7+
util.decorateErrorStack();
8+
util.decorateErrorStack(null);
9+
util.decorateErrorStack(1);
10+
util.decorateErrorStack(true);
11+
});
12+
13+
// Verify that a stack property is not added to non-Errors
14+
const obj = {};
15+
util.decorateErrorStack(obj);
16+
assert.strictEqual(obj.stack, undefined);
17+
18+
// Verify that the stack is decorated when possible
19+
let err;
20+
21+
try {
22+
require('../fixtures/syntax/bad_syntax');
23+
} catch (e) {
24+
err = e;
25+
assert(!/var foo bar;/.test(err.stack));
26+
util.decorateErrorStack(err);
27+
}
28+
29+
assert(/var foo bar;/.test(err.stack));
30+
31+
// Verify that the stack is unchanged when there is no arrow message
32+
err = new Error('foo');
33+
const originalStack = err.stack;
34+
util.decorateErrorStack(err);
35+
assert.strictEqual(originalStack, err.stack);

0 commit comments

Comments
 (0)
Please sign in to comment.