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