source: imaps-frontend/node_modules/pretty-error/src/PrettyError.coffee@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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