ParsedError.js 6.4 KB

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