PrettyError.coffee
7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
isPlainObject = require 'lodash/isPlainObject'
defaultStyle = require './defaultStyle'
ParsedError = require './ParsedError'
nodePaths = require './nodePaths'
RenderKid = require 'renderkid'
merge = require 'lodash/merge'
arrayUtils =
pluckByCallback: (a, cb) ->
return a if a.length < 1
removed = 0
for value, index in a
if cb value, index
removed++
continue
if removed isnt 0
a[index - removed] = a[index]
if removed > 0
a.length = a.length - removed
a
pluckOneItem: (a, item) ->
return a if a.length < 1
reached = no
for value, index in a
if not reached
if value is item
reached = yes
continue
else
a[index - 1] = a[index]
a.length = a.length - 1 if reached
a
instance = null
module.exports = class PrettyError
self = @
@_filters:
'module.exports': (item) ->
return unless item.what?
item.what = item.what.replace /\.module\.exports\./g, ' - '
return
@_getDefaultStyle: ->
defaultStyle()
@start: ->
unless instance?
instance = new self
instance.start()
instance
@stop: ->
instance?.stop()
constructor: ->
@_useColors = yes
@_maxItems = 50
@_packagesToSkip = []
@_pathsToSkip = []
@_skipCallbacks = []
@_filterCallbacks = []
@_parsedErrorFilters = []
@_aliases = []
@_renderer = new RenderKid
@_style = self._getDefaultStyle()
@_renderer.style @_style
start: ->
@_oldPrepareStackTrace = Error.prepareStackTrace
prepeare = @_oldPrepareStackTrace or (exc, frames) ->
result = exc.toString()
frames = frames.map (frame) -> " at #{frame.toString()}"
result + "\n" + frames.join "\n"
# https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
Error.prepareStackTrace = (exc, trace) =>
stack = prepeare.apply(null, arguments)
@render {stack, message: exc.toString().replace /^.*: /, ''}, no
@
stop: ->
Error.prepareStackTrace = @_oldPrepareStackTrace
@_oldPrepareStackTrace = null
config: (c) ->
if c.skipPackages?
if c.skipPackages is no
@unskipAllPackages()
else
@skipPackage.apply @, c.skipPackages
if c.skipPaths?
if c.skipPaths is no
@unskipAllPaths()
else
@skipPath.apply @, c.skipPaths
if c.skip?
if c.skip is no
@unskipAll()
else
@skip.apply @, c.skip
if c.maxItems?
@setMaxItems c.maxItems
if c.skipNodeFiles is yes
@skipNodeFiles()
else if c.skipNodeFiles is no
@unskipNodeFiles()
if c.filters?
if c.filters is no
@removeAllFilters()
else
@filter.apply @, c.filters
if c.parsedErrorFilters?
if c.parsedErrorFilters is no
@removeAllParsedErrorFilters()
else
@filterParsedError.apply @, c.parsedErrorFilters
if c.aliases?
if isPlainObject c.aliases
@alias path, alias for path, alias of c.aliases
else if c.aliases is no
@removeAllAliases()
@
withoutColors: ->
@_useColors = false
@
withColors: ->
@_useColors = true
@
skipPackage: (packages...) ->
@_packagesToSkip.push String pkg for pkg in packages
@
unskipPackage: (packages...) ->
arrayUtils.pluckOneItem(@_packagesToSkip, pkg) for pkg in packages
@
unskipAllPackages: ->
@_packagesToSkip.length = 0
@
skipPath: (paths...) ->
@_pathsToSkip.push path for path in paths
@
unskipPath: (paths...) ->
arrayUtils.pluckOneItem(@_pathsToSkip, path) for path in paths
@
unskipAllPaths: ->
@_pathsToSkip.length = 0
@
skip: (callbacks...) ->
@_skipCallbacks.push cb for cb in callbacks
@
unskip: (callbacks...) ->
arrayUtils.pluckOneItem(@_skipCallbacks, cb) for cb in callbacks
@
unskipAll: ->
@_skipCallbacks.length = 0
@
skipNodeFiles: ->
@skipPath.apply @, nodePaths
unskipNodeFiles: ->
@unskipPath.apply @, nodePaths
filter: (callbacks...) ->
@_filterCallbacks.push cb for cb in callbacks
@
removeFilter: (callbacks...) ->
arrayUtils.pluckOneItem(@_filterCallbacks, cb) for cb in callbacks
@
removeAllFilters: ->
@_filterCallbacks.length = 0
@
filterParsedError: (callbacks...) ->
@_parsedErrorFilters.push cb for cb in callbacks
@
removeParsedErrorFilter: (callbacks...) ->
arrayUtils.pluckOneItem(@_parsedErrorFilters, cb) for cb in callbacks
@
removeAllParsedErrorFilters: ->
@_parsedErrorFilters.length = 0
@
setMaxItems: (maxItems = 50) ->
if maxItems is 0 then maxItems = 50
@_maxItems = maxItems|0
@
alias: (stringOrRx, alias) ->
@_aliases.push {stringOrRx, alias}
@
removeAlias: (stringOrRx) ->
arrayUtils.pluckByCallback @_aliases, (pair) ->
pair.stringOrRx is stringOrRx
@
removeAllAliases: ->
@_aliases.length = 0
@
_getStyle: ->
@_style
appendStyle: (toAppend) ->
merge @_style, toAppend
@_renderer.style toAppend
@
_getRenderer: ->
@_renderer
render: (e, logIt = no, useColors = @_useColors) ->
obj = @getObject e
rendered = @_renderer.render(obj, useColors)
console.error rendered if logIt is yes
rendered
getObject: (e) ->
unless e instanceof ParsedError
e = new ParsedError e
@_applyParsedErrorFiltersOn e
header =
title: do ->
ret = {}
# some errors are thrown to display other errors.
# we call them wrappers here.
if e.wrapper isnt ''
ret.wrapper = "#{e.wrapper}"
ret.kind = e.kind
ret
colon: ':'
message: String(e.message).trim()
traceItems = []
count = -1
for item, i in e.trace
continue unless item?
continue if @_skipOrFilter(item, i) is yes
count++
break if count > @_maxItems
if typeof item is 'string'
traceItems.push item: custom: item
continue
traceItems.push do ->
markupItem = item:
header:
pointer: do ->
return '' unless item.file?
file: item.file
colon: ':'
line: item.line
footer: do ->
foooter = addr: item.shortenedAddr
if item.extra? then foooter.extra = item.extra
foooter
markupItem.item.header.what = item.what if typeof item.what is 'string' and item.what.trim().length > 0
markupItem
obj = 'pretty-error':
header: header
if traceItems.length > 0
obj['pretty-error'].trace = traceItems
obj
_skipOrFilter: (item, itemNumber) ->
if typeof item is 'object'
return yes if item.modName in @_packagesToSkip
return yes if item.path in @_pathsToSkip
for modName in item.packages
return yes if modName in @_packagesToSkip
if typeof item.shortenedAddr is 'string'
for pair in @_aliases
item.shortenedAddr = item.shortenedAddr.replace pair.stringOrRx, pair.alias
for cb in @_skipCallbacks
return yes if cb(item, itemNumber) is yes
for cb in @_filterCallbacks
cb(item, itemNumber)
return no
_applyParsedErrorFiltersOn: (error) ->
for cb in @_parsedErrorFilters
cb error
return
for prop in ['renderer', 'style'] then do ->
methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length)
PrettyError::__defineGetter__ prop, -> do @[methodName]