FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

util: add decorateErrorStack() · nodejs/node@e68ea16 · GitHub

/ node Public

Commit e68ea16

Browse files
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

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎lib/util.js‎

Lines 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+
};
Lines 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)

Back | FazBrowse Home | New Git URL