ParsedError.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Generated by CoffeeScript 1.8.0
  2. var ParsedError, prop, sysPath, _fn, _i, _len, _ref;
  3. sysPath = require('path');
  4. module.exports = ParsedError = (function() {
  5. function ParsedError(error) {
  6. this.error = error;
  7. this._parse();
  8. }
  9. ParsedError.prototype._parse = function() {
  10. var m;
  11. this._trace = [];
  12. this._kind = 'Error';
  13. this._wrapper = '';
  14. if (this.error.wrapper != null) {
  15. this._wrapper = String(this.error.wrapper);
  16. }
  17. if (typeof this.error !== 'object') {
  18. this._message = String(this.error);
  19. } else {
  20. this._stack = this.error.stack;
  21. if (this.error.kind != null) {
  22. this._kind = String(this.error.kind);
  23. } else if (typeof this._stack === 'string') {
  24. if (m = this._stack.match(/^([a-zA-Z0-9\_\$]+):\ /)) {
  25. this._kind = m[1];
  26. }
  27. }
  28. this._message = (this.error.message != null) && String(this.error.message) || '';
  29. if (typeof this._stack === 'string') {
  30. this._parseStack();
  31. }
  32. }
  33. };
  34. ParsedError.prototype._parseStack = function() {
  35. var line, message, messageLines, reachedTrace, _i, _len, _ref;
  36. messageLines = [];
  37. reachedTrace = false;
  38. _ref = this._stack.split('\n');
  39. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  40. line = _ref[_i];
  41. if (line.trim() === '') {
  42. continue;
  43. }
  44. if (reachedTrace) {
  45. this._trace.push(this._parseTraceItem(line));
  46. } else {
  47. if (line.match(/^\s*at\s.+/)) {
  48. reachedTrace = true;
  49. this._trace.push(this._parseTraceItem(line));
  50. } else if (!this._message.split('\n'.indexOf(line))) {
  51. messageLines.push(line);
  52. }
  53. }
  54. }
  55. message = messageLines.join('\n');
  56. if (message.substr(0, this._kind.length) === this._kind) {
  57. message = message.substr(this._kind.length, message.length).replace(/^\:\s+/, '');
  58. }
  59. if (message.length) {
  60. this._message = this._message.length ? [this._message, message].join('\n') : message;
  61. }
  62. };
  63. ParsedError.prototype._parseTraceItem = function(text) {
  64. var addr, col, d, dir, file, jsCol, jsLine, line, m, original, packageName, packages, path, r, remaining, shortenedAddr, shortenedPath, what;
  65. text = text.trim();
  66. if (text === '') {
  67. return;
  68. }
  69. if (!text.match(/^at\ /)) {
  70. return text;
  71. }
  72. text = text.replace(/^at /, '');
  73. if (text === 'Error (<anonymous>)' || text === 'Error (<anonymous>:null:null)') {
  74. return;
  75. }
  76. original = text;
  77. what = null;
  78. addr = null;
  79. path = null;
  80. dir = null;
  81. file = null;
  82. line = null;
  83. col = null;
  84. jsLine = null;
  85. jsCol = null;
  86. shortenedPath = null;
  87. shortenedAddr = null;
  88. packageName = '[current]';
  89. if (m = text.match(/\(([^\)]+)\)$/)) {
  90. addr = m[1].trim();
  91. }
  92. if (addr != null) {
  93. what = text.substr(0, text.length - addr.length - 2);
  94. what = what.trim();
  95. }
  96. if (addr == null) {
  97. addr = text.trim();
  98. }
  99. addr = this._fixPath(addr);
  100. remaining = addr;
  101. if (m = remaining.match(/\,\ <js>:(\d+):(\d+)$/)) {
  102. jsLine = m[1];
  103. jsCol = m[2];
  104. remaining = remaining.substr(0, remaining.length - m[0].length);
  105. }
  106. if (m = remaining.match(/:(\d+):(\d+)$/)) {
  107. line = m[1];
  108. col = m[2];
  109. remaining = remaining.substr(0, remaining.length - m[0].length);
  110. path = remaining;
  111. }
  112. if (path != null) {
  113. file = sysPath.basename(path);
  114. dir = sysPath.dirname(path);
  115. if (dir === '.') {
  116. dir = '';
  117. }
  118. path = this._fixPath(path);
  119. file = this._fixPath(file);
  120. dir = this._fixPath(dir);
  121. }
  122. if (dir != null) {
  123. d = dir.replace(/[\\]{1,2}/g, '/');
  124. if (m = d.match(/node_modules\/([^\/]+)(?!.*node_modules.*)/)) {
  125. packageName = m[1];
  126. }
  127. }
  128. if (jsLine == null) {
  129. jsLine = line;
  130. jsCol = col;
  131. }
  132. if (path != null) {
  133. r = this._rectifyPath(path);
  134. shortenedPath = r.path;
  135. shortenedAddr = shortenedPath + addr.substr(path.length, addr.length);
  136. packages = r.packages;
  137. }
  138. return {
  139. original: original,
  140. what: what,
  141. addr: addr,
  142. path: path,
  143. dir: dir,
  144. file: file,
  145. line: parseInt(line),
  146. col: parseInt(col),
  147. jsLine: parseInt(jsLine),
  148. jsCol: parseInt(jsCol),
  149. packageName: packageName,
  150. shortenedPath: shortenedPath,
  151. shortenedAddr: shortenedAddr,
  152. packages: packages || []
  153. };
  154. };
  155. ParsedError.prototype._getMessage = function() {
  156. return this._message;
  157. };
  158. ParsedError.prototype._getKind = function() {
  159. return this._kind;
  160. };
  161. ParsedError.prototype._getWrapper = function() {
  162. return this._wrapper;
  163. };
  164. ParsedError.prototype._getStack = function() {
  165. return this._stack;
  166. };
  167. ParsedError.prototype._getArguments = function() {
  168. return this.error["arguments"];
  169. };
  170. ParsedError.prototype._getType = function() {
  171. return this.error.type;
  172. };
  173. ParsedError.prototype._getTrace = function() {
  174. return this._trace;
  175. };
  176. ParsedError.prototype._fixPath = function(path) {
  177. return path.replace(/[\\]{1,2}/g, '/');
  178. };
  179. ParsedError.prototype._rectifyPath = function(path, nameForCurrentPackage) {
  180. var m, packages, parts, remaining, rest;
  181. path = String(path);
  182. remaining = path;
  183. if (!(m = path.match(/^(.+?)\/node_modules\/(.+)$/))) {
  184. return {
  185. path: path,
  186. packages: []
  187. };
  188. }
  189. parts = [];
  190. packages = [];
  191. if (typeof nameForCurrentPackage === 'string') {
  192. parts.push("[" + nameForCurrentPackage + "]");
  193. packages.push("[" + nameForCurrentPackage + "]");
  194. } else {
  195. parts.push("[" + (m[1].match(/([^\/]+)$/)[1]) + "]");
  196. packages.push(m[1].match(/([^\/]+)$/)[1]);
  197. }
  198. rest = m[2];
  199. while (m = rest.match(/([^\/]+)\/node_modules\/(.+)$/)) {
  200. parts.push("[" + m[1] + "]");
  201. packages.push(m[1]);
  202. rest = m[2];
  203. }
  204. if (m = rest.match(/([^\/]+)\/(.+)$/)) {
  205. parts.push("[" + m[1] + "]");
  206. packages.push(m[1]);
  207. rest = m[2];
  208. }
  209. parts.push(rest);
  210. return {
  211. path: parts.join("/"),
  212. packages: packages
  213. };
  214. };
  215. return ParsedError;
  216. })();
  217. _ref = ['message', 'kind', 'arguments', 'type', 'stack', 'trace', 'wrapper'];
  218. _fn = function() {
  219. var methodName;
  220. methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length);
  221. return Object.defineProperty(ParsedError.prototype, prop, {
  222. get: function() {
  223. return this[methodName]();
  224. }
  225. });
  226. };
  227. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  228. prop = _ref[_i];
  229. _fn();
  230. }