1 |
|
2 | const util = require('util');
|
3 |
|
4 | function extendTrace(object, property, pos) {
|
5 | const old = object[property];
|
6 | object[property] = function() {
|
7 | const error = new Error();
|
8 | const name = object.constructor.name + '#' + property + '(' +
|
9 | Array.prototype.slice.call(arguments).map(function(el) {
|
10 | return util.inspect(el, false, 0);
|
11 | }).join(', ') + ')';
|
12 |
|
13 | if (typeof pos === 'undefined') pos = -1;
|
14 | if (pos < 0) pos += arguments.length;
|
15 | const cb = arguments[pos];
|
16 | if (typeof arguments[pos] === 'function') {
|
17 | arguments[pos] = function replacement() {
|
18 | const err = arguments[0];
|
19 | if (err && err.stack && !err.__augmented) {
|
20 | err.stack = filter(err).join('\n');
|
21 | err.stack += '\n--> in ' + name;
|
22 | err.stack += '\n' + filter(error).slice(1).join('\n');
|
23 | err.__augmented = true;
|
24 | }
|
25 | return cb.apply(this, arguments);
|
26 | };
|
27 | }
|
28 | return old.apply(this, arguments);
|
29 | };
|
30 | }
|
31 | exports.extendTrace = extendTrace;
|
32 |
|
33 |
|
34 | function filter(error) {
|
35 | return error.stack.split('\n').filter(function(line) {
|
36 | return line.indexOf(__filename) < 0;
|
37 | });
|
38 | }
|