PrettyError.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // Generated by CoffeeScript 1.8.0
  2. var ParsedError, PrettyError, RenderKid, array, defaultStyle, instance, nodePaths, object, prop, _fn, _i, _len, _ref, _ref1,
  3. __slice = [].slice,
  4. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  5. _ref = require('utila'), object = _ref.object, array = _ref.array;
  6. defaultStyle = require('./defaultStyle');
  7. ParsedError = require('./ParsedError');
  8. nodePaths = require('./nodePaths');
  9. RenderKid = require('renderkid');
  10. instance = null;
  11. module.exports = PrettyError = (function() {
  12. var self;
  13. self = PrettyError;
  14. PrettyError._filters = {
  15. 'module.exports': function(item) {
  16. if (item.what == null) {
  17. return;
  18. }
  19. item.what = item.what.replace(/\.module\.exports\./g, ' - ');
  20. }
  21. };
  22. PrettyError._getDefaultStyle = function() {
  23. return defaultStyle();
  24. };
  25. PrettyError.start = function() {
  26. if (instance == null) {
  27. instance = new self;
  28. instance.start();
  29. }
  30. return instance;
  31. };
  32. PrettyError.stop = function() {
  33. return instance != null ? instance.stop() : void 0;
  34. };
  35. function PrettyError() {
  36. this._useColors = true;
  37. this._maxItems = 50;
  38. this._packagesToSkip = [];
  39. this._pathsToSkip = [];
  40. this._skipCallbacks = [];
  41. this._filterCallbacks = [];
  42. this._parsedErrorFilters = [];
  43. this._aliases = [];
  44. this._renderer = new RenderKid;
  45. this._style = self._getDefaultStyle();
  46. this._renderer.style(this._style);
  47. }
  48. PrettyError.prototype.start = function() {
  49. var prepeare;
  50. this._oldPrepareStackTrace = Error.prepareStackTrace;
  51. prepeare = this._oldPrepareStackTrace || function(exc, frames) {
  52. var result;
  53. result = exc.toString();
  54. frames = frames.map(function(frame) {
  55. return " at " + (frame.toString());
  56. });
  57. return result + "\n" + frames.join("\n");
  58. };
  59. Error.prepareStackTrace = (function(_this) {
  60. return function(exc, trace) {
  61. var stack;
  62. stack = prepeare.apply(null, arguments);
  63. return _this.render({
  64. stack: stack,
  65. message: exc.toString().replace(/^.*: /, '')
  66. }, false);
  67. };
  68. })(this);
  69. return this;
  70. };
  71. PrettyError.prototype.stop = function() {
  72. Error.prepareStackTrace = this._oldPrepareStackTrace;
  73. return this._oldPrepareStackTrace = null;
  74. };
  75. PrettyError.prototype.config = function(c) {
  76. var alias, path, _ref1;
  77. if (c.skipPackages != null) {
  78. if (c.skipPackages === false) {
  79. this.unskipAllPackages();
  80. } else {
  81. this.skipPackage.apply(this, c.skipPackages);
  82. }
  83. }
  84. if (c.skipPaths != null) {
  85. if (c.skipPaths === false) {
  86. this.unskipAllPaths();
  87. } else {
  88. this.skipPath.apply(this, c.skipPaths);
  89. }
  90. }
  91. if (c.skip != null) {
  92. if (c.skip === false) {
  93. this.unskipAll();
  94. } else {
  95. this.skip.apply(this, c.skip);
  96. }
  97. }
  98. if (c.maxItems != null) {
  99. this.setMaxItems(c.maxItems);
  100. }
  101. if (c.skipNodeFiles === true) {
  102. this.skipNodeFiles();
  103. } else if (c.skipNodeFiles === false) {
  104. this.unskipNodeFiles();
  105. }
  106. if (c.filters != null) {
  107. if (c.filters === false) {
  108. this.removeAllFilters();
  109. } else {
  110. this.filter.apply(this, c.filters);
  111. }
  112. }
  113. if (c.parsedErrorFilters != null) {
  114. if (c.parsedErrorFilters === false) {
  115. this.removeAllParsedErrorFilters();
  116. } else {
  117. this.filterParsedError.apply(this, c.parsedErrorFilters);
  118. }
  119. }
  120. if (c.aliases != null) {
  121. if (object.isBareObject(c.aliases)) {
  122. _ref1 = c.aliases;
  123. for (path in _ref1) {
  124. alias = _ref1[path];
  125. this.alias(path, alias);
  126. }
  127. } else if (c.aliases === false) {
  128. this.removeAllAliases();
  129. }
  130. }
  131. return this;
  132. };
  133. PrettyError.prototype.withoutColors = function() {
  134. this._useColors = false;
  135. return this;
  136. };
  137. PrettyError.prototype.withColors = function() {
  138. this._useColors = true;
  139. return this;
  140. };
  141. PrettyError.prototype.skipPackage = function() {
  142. var packages, pkg, _i, _len;
  143. packages = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  144. for (_i = 0, _len = packages.length; _i < _len; _i++) {
  145. pkg = packages[_i];
  146. this._packagesToSkip.push(String(pkg));
  147. }
  148. return this;
  149. };
  150. PrettyError.prototype.unskipPackage = function() {
  151. var packages, pkg, _i, _len;
  152. packages = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  153. for (_i = 0, _len = packages.length; _i < _len; _i++) {
  154. pkg = packages[_i];
  155. array.pluckOneItem(this._packagesToSkip, pkg);
  156. }
  157. return this;
  158. };
  159. PrettyError.prototype.unskipAllPackages = function() {
  160. this._packagesToSkip.length = 0;
  161. return this;
  162. };
  163. PrettyError.prototype.skipPath = function() {
  164. var path, paths, _i, _len;
  165. paths = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  166. for (_i = 0, _len = paths.length; _i < _len; _i++) {
  167. path = paths[_i];
  168. this._pathsToSkip.push(path);
  169. }
  170. return this;
  171. };
  172. PrettyError.prototype.unskipPath = function() {
  173. var path, paths, _i, _len;
  174. paths = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  175. for (_i = 0, _len = paths.length; _i < _len; _i++) {
  176. path = paths[_i];
  177. array.pluckOneItem(this._pathsToSkip, path);
  178. }
  179. return this;
  180. };
  181. PrettyError.prototype.unskipAllPaths = function() {
  182. this._pathsToSkip.length = 0;
  183. return this;
  184. };
  185. PrettyError.prototype.skip = function() {
  186. var callbacks, cb, _i, _len;
  187. callbacks = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  188. for (_i = 0, _len = callbacks.length; _i < _len; _i++) {
  189. cb = callbacks[_i];
  190. this._skipCallbacks.push(cb);
  191. }
  192. return this;
  193. };
  194. PrettyError.prototype.unskip = function() {
  195. var callbacks, cb, _i, _len;
  196. callbacks = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  197. for (_i = 0, _len = callbacks.length; _i < _len; _i++) {
  198. cb = callbacks[_i];
  199. array.pluckOneItem(this._skipCallbacks, cb);
  200. }
  201. return this;
  202. };
  203. PrettyError.prototype.unskipAll = function() {
  204. this._skipCallbacks.length = 0;
  205. return this;
  206. };
  207. PrettyError.prototype.skipNodeFiles = function() {
  208. return this.skipPath.apply(this, nodePaths);
  209. };
  210. PrettyError.prototype.unskipNodeFiles = function() {
  211. return this.unskipPath.apply(this, nodePaths);
  212. };
  213. PrettyError.prototype.filter = function() {
  214. var callbacks, cb, _i, _len;
  215. callbacks = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  216. for (_i = 0, _len = callbacks.length; _i < _len; _i++) {
  217. cb = callbacks[_i];
  218. this._filterCallbacks.push(cb);
  219. }
  220. return this;
  221. };
  222. PrettyError.prototype.removeFilter = function() {
  223. var callbacks, cb, _i, _len;
  224. callbacks = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  225. for (_i = 0, _len = callbacks.length; _i < _len; _i++) {
  226. cb = callbacks[_i];
  227. array.pluckOneItem(this._filterCallbacks, cb);
  228. }
  229. return this;
  230. };
  231. PrettyError.prototype.removeAllFilters = function() {
  232. this._filterCallbacks.length = 0;
  233. return this;
  234. };
  235. PrettyError.prototype.filterParsedError = function() {
  236. var callbacks, cb, _i, _len;
  237. callbacks = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  238. for (_i = 0, _len = callbacks.length; _i < _len; _i++) {
  239. cb = callbacks[_i];
  240. this._parsedErrorFilters.push(cb);
  241. }
  242. return this;
  243. };
  244. PrettyError.prototype.removeParsedErrorFilter = function() {
  245. var callbacks, cb, _i, _len;
  246. callbacks = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  247. for (_i = 0, _len = callbacks.length; _i < _len; _i++) {
  248. cb = callbacks[_i];
  249. array.pluckOneItem(this._parsedErrorFilters, cb);
  250. }
  251. return this;
  252. };
  253. PrettyError.prototype.removeAllParsedErrorFilters = function() {
  254. this._parsedErrorFilters.length = 0;
  255. return this;
  256. };
  257. PrettyError.prototype.setMaxItems = function(maxItems) {
  258. if (maxItems == null) {
  259. maxItems = 50;
  260. }
  261. if (maxItems === 0) {
  262. maxItems = 50;
  263. }
  264. this._maxItems = maxItems | 0;
  265. return this;
  266. };
  267. PrettyError.prototype.alias = function(stringOrRx, alias) {
  268. this._aliases.push({
  269. stringOrRx: stringOrRx,
  270. alias: alias
  271. });
  272. return this;
  273. };
  274. PrettyError.prototype.removeAlias = function(stringOrRx) {
  275. array.pluckByCallback(this._aliases, function(pair) {
  276. return pair.stringOrRx === stringOrRx;
  277. });
  278. return this;
  279. };
  280. PrettyError.prototype.removeAllAliases = function() {
  281. this._aliases.length = 0;
  282. return this;
  283. };
  284. PrettyError.prototype._getStyle = function() {
  285. return this._style;
  286. };
  287. PrettyError.prototype.appendStyle = function(toAppend) {
  288. object.appendOnto(this._style, toAppend);
  289. this._renderer.style(toAppend);
  290. return this;
  291. };
  292. PrettyError.prototype._getRenderer = function() {
  293. return this._renderer;
  294. };
  295. PrettyError.prototype.render = function(e, logIt, useColors) {
  296. var obj, rendered;
  297. if (logIt == null) {
  298. logIt = false;
  299. }
  300. if (useColors == null) {
  301. useColors = this._useColors;
  302. }
  303. obj = this.getObject(e);
  304. rendered = this._renderer.render(obj, useColors);
  305. if (logIt === true) {
  306. console.error(rendered);
  307. }
  308. return rendered;
  309. };
  310. PrettyError.prototype.getObject = function(e) {
  311. var count, header, i, item, obj, traceItems, _i, _len, _ref1;
  312. if (!(e instanceof ParsedError)) {
  313. e = new ParsedError(e);
  314. }
  315. this._applyParsedErrorFiltersOn(e);
  316. header = {
  317. title: (function() {
  318. var ret;
  319. ret = {};
  320. if (e.wrapper !== '') {
  321. ret.wrapper = "" + e.wrapper;
  322. }
  323. ret.kind = e.kind;
  324. return ret;
  325. })(),
  326. colon: ':',
  327. message: String(e.message).trim()
  328. };
  329. traceItems = [];
  330. count = -1;
  331. _ref1 = e.trace;
  332. for (i = _i = 0, _len = _ref1.length; _i < _len; i = ++_i) {
  333. item = _ref1[i];
  334. if (item == null) {
  335. continue;
  336. }
  337. if (this._skipOrFilter(item, i) === true) {
  338. continue;
  339. }
  340. count++;
  341. if (count > this._maxItems) {
  342. break;
  343. }
  344. if (typeof item === 'string') {
  345. traceItems.push({
  346. item: {
  347. custom: item
  348. }
  349. });
  350. continue;
  351. }
  352. traceItems.push((function() {
  353. var markupItem;
  354. markupItem = {
  355. item: {
  356. header: {
  357. pointer: (function() {
  358. if (item.file == null) {
  359. return '';
  360. }
  361. return {
  362. file: item.file,
  363. colon: ':',
  364. line: item.line
  365. };
  366. })()
  367. },
  368. footer: (function() {
  369. var foooter;
  370. foooter = {
  371. addr: item.shortenedAddr
  372. };
  373. if (item.extra != null) {
  374. foooter.extra = item.extra;
  375. }
  376. return foooter;
  377. })()
  378. }
  379. };
  380. if (typeof item.what === 'string' && item.what.trim().length > 0) {
  381. markupItem.item.header.what = item.what;
  382. }
  383. return markupItem;
  384. })());
  385. }
  386. obj = {
  387. 'pretty-error': {
  388. header: header
  389. }
  390. };
  391. if (traceItems.length > 0) {
  392. obj['pretty-error'].trace = traceItems;
  393. }
  394. return obj;
  395. };
  396. PrettyError.prototype._skipOrFilter = function(item, itemNumber) {
  397. var cb, modName, pair, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6;
  398. if (typeof item === 'object') {
  399. if (_ref1 = item.modName, __indexOf.call(this._packagesToSkip, _ref1) >= 0) {
  400. return true;
  401. }
  402. if (_ref2 = item.path, __indexOf.call(this._pathsToSkip, _ref2) >= 0) {
  403. return true;
  404. }
  405. _ref3 = item.packages;
  406. for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
  407. modName = _ref3[_i];
  408. if (__indexOf.call(this._packagesToSkip, modName) >= 0) {
  409. return true;
  410. }
  411. }
  412. if (typeof item.shortenedAddr === 'string') {
  413. _ref4 = this._aliases;
  414. for (_j = 0, _len1 = _ref4.length; _j < _len1; _j++) {
  415. pair = _ref4[_j];
  416. item.shortenedAddr = item.shortenedAddr.replace(pair.stringOrRx, pair.alias);
  417. }
  418. }
  419. }
  420. _ref5 = this._skipCallbacks;
  421. for (_k = 0, _len2 = _ref5.length; _k < _len2; _k++) {
  422. cb = _ref5[_k];
  423. if (cb(item, itemNumber) === true) {
  424. return true;
  425. }
  426. }
  427. _ref6 = this._filterCallbacks;
  428. for (_l = 0, _len3 = _ref6.length; _l < _len3; _l++) {
  429. cb = _ref6[_l];
  430. cb(item, itemNumber);
  431. }
  432. return false;
  433. };
  434. PrettyError.prototype._applyParsedErrorFiltersOn = function(error) {
  435. var cb, _i, _len, _ref1;
  436. _ref1 = this._parsedErrorFilters;
  437. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  438. cb = _ref1[_i];
  439. cb(error);
  440. }
  441. };
  442. return PrettyError;
  443. })();
  444. _ref1 = ['renderer', 'style'];
  445. _fn = function() {
  446. var methodName;
  447. methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length);
  448. return PrettyError.prototype.__defineGetter__(prop, function() {
  449. return this[methodName]();
  450. });
  451. };
  452. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  453. prop = _ref1[_i];
  454. _fn();
  455. }