GeezerVison is showing github.com — util: add decorateErrorStack() · nodejs/node@8ca412b
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/util.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -899,3 +899,14 @@ exports._exceptionWithHostPort = function(err, |
899 | 899 | } |
900 | 900 | return ex; |
901 | 901 | }; |
| 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 | +}; |
Collapse file
Copy file name to clipboard +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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); |