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