Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e68ea16

Browse filesBrowse files
cjihrigrvagg
authored andcommitted
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>
1 parent 054a216 commit e68ea16
Copy full SHA for e68ea16

File tree

Expand file treeCollapse file tree

2 files changed

+46
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+46
-0
lines changed
Open diff view settings
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,3 +883,14 @@ exports._exceptionWithHostPort = function(err,
883883
}
884884
return ex;
885885
};
886+
887+
888+
exports.decorateErrorStack = function(err) {
889+
if (!(isError(err) && err.stack))
890+
return;
891+
892+
const arrow = internalUtil.getHiddenValue(err, 'arrowMessage');
893+
894+
if (arrow)
895+
err.stack = arrow + err.stack;
896+
};
Collapse file
+35Lines changed: 35 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.