PrettyError.js 14 KB

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