stack-trace.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. exports.get = function(belowFn) {
  2. var oldLimit = Error.stackTraceLimit;
  3. Error.stackTraceLimit = Infinity;
  4. var dummyObject = {};
  5. var v8Handler = Error.prepareStackTrace;
  6. Error.prepareStackTrace = function(dummyObject, v8StackTrace) {
  7. return v8StackTrace;
  8. };
  9. Error.captureStackTrace(dummyObject, belowFn || exports.get);
  10. var v8StackTrace = dummyObject.stack;
  11. Error.prepareStackTrace = v8Handler;
  12. Error.stackTraceLimit = oldLimit;
  13. return v8StackTrace;
  14. };
  15. exports.parse = function(err) {
  16. if (!err.stack) {
  17. return [];
  18. }
  19. var self = this;
  20. var lines = err.stack.split('\n').slice(1);
  21. return lines
  22. .map(function(line) {
  23. if (line.match(/^\s*[-]{4,}$/)) {
  24. return self._createParsedCallSite({
  25. fileName: line,
  26. lineNumber: null,
  27. functionName: null,
  28. typeName: null,
  29. methodName: null,
  30. columnNumber: null,
  31. 'native': null,
  32. });
  33. }
  34. var lineMatch = line.match(/at (?:(.+)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/);
  35. if (!lineMatch) {
  36. return;
  37. }
  38. var object = null;
  39. var method = null;
  40. var functionName = null;
  41. var typeName = null;
  42. var methodName = null;
  43. var isNative = (lineMatch[5] === 'native');
  44. if (lineMatch[1]) {
  45. functionName = lineMatch[1];
  46. var methodStart = functionName.lastIndexOf('.');
  47. if (functionName[methodStart-1] == '.')
  48. methodStart--;
  49. if (methodStart > 0) {
  50. object = functionName.substr(0, methodStart);
  51. method = functionName.substr(methodStart + 1);
  52. var objectEnd = object.indexOf('.Module');
  53. if (objectEnd > 0) {
  54. functionName = functionName.substr(objectEnd + 1);
  55. object = object.substr(0, objectEnd);
  56. }
  57. }
  58. typeName = null;
  59. }
  60. if (method) {
  61. typeName = object;
  62. methodName = method;
  63. }
  64. if (method === '<anonymous>') {
  65. methodName = null;
  66. functionName = null;
  67. }
  68. var properties = {
  69. fileName: lineMatch[2] || null,
  70. lineNumber: parseInt(lineMatch[3], 10) || null,
  71. functionName: functionName,
  72. typeName: typeName,
  73. methodName: methodName,
  74. columnNumber: parseInt(lineMatch[4], 10) || null,
  75. 'native': isNative,
  76. };
  77. return self._createParsedCallSite(properties);
  78. })
  79. .filter(function(callSite) {
  80. return !!callSite;
  81. });
  82. };
  83. function CallSite(properties) {
  84. for (var property in properties) {
  85. this[property] = properties[property];
  86. }
  87. }
  88. var strProperties = [
  89. 'this',
  90. 'typeName',
  91. 'functionName',
  92. 'methodName',
  93. 'fileName',
  94. 'lineNumber',
  95. 'columnNumber',
  96. 'function',
  97. 'evalOrigin'
  98. ];
  99. var boolProperties = [
  100. 'topLevel',
  101. 'eval',
  102. 'native',
  103. 'constructor'
  104. ];
  105. strProperties.forEach(function (property) {
  106. CallSite.prototype[property] = null;
  107. CallSite.prototype['get' + property[0].toUpperCase() + property.substr(1)] = function () {
  108. return this[property];
  109. }
  110. });
  111. boolProperties.forEach(function (property) {
  112. CallSite.prototype[property] = false;
  113. CallSite.prototype['is' + property[0].toUpperCase() + property.substr(1)] = function () {
  114. return this[property];
  115. }
  116. });
  117. exports._createParsedCallSite = function(properties) {
  118. return new CallSite(properties);
  119. };