PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/pretty-error/src/pretty-error.coffee

https://gitlab.com/chriswhalen/flipkicks
CoffeeScript | 309 lines | 228 code | 78 blank | 3 comment | 50 complexity | 7d819c42fbe94d4b15f57ae49e38df6d MD5 | raw file
  1. {object, array} = require 'utila'
  2. defaultStyle = require './default-style'
  3. ParsedError = require './parsed-error'
  4. nodePaths = require './node-paths'
  5. RenderKid = require 'renderkid'
  6. instance = null
  7. module.exports = class PrettyError
  8. self = @
  9. @_filters:
  10. 'module.exports': (item) ->
  11. return unless item.what?
  12. item.what = item.what.replace /\.module\.exports\./g, ' - '
  13. return
  14. @_getDefaultStyle: ->
  15. defaultStyle()
  16. @start: ->
  17. unless instance?
  18. instance = new self
  19. instance.start()
  20. instance
  21. @stop: ->
  22. instance?.stop()
  23. constructor: ->
  24. @_useColors = yes
  25. @_maxItems = 50
  26. @_packagesToSkip = []
  27. @_pathsToSkip = []
  28. @_skipCallbacks = []
  29. @_filterCallbacks = []
  30. @_parsedErrorFilters = []
  31. @_aliases = []
  32. @_renderer = new RenderKid
  33. @_style = self._getDefaultStyle()
  34. @_renderer.style @_style
  35. start: ->
  36. @_oldPrepareStackTrace = Error.prepareStackTrace
  37. prepeare = @_oldPrepareStackTrace or (exc, frames) ->
  38. result = exc.toString()
  39. frames = frames.map (frame) -> " at #{frame.toString()}"
  40. result + "\n" + frames.join "\n"
  41. # https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
  42. Error.prepareStackTrace = (exc, trace) =>
  43. stack = prepeare.apply(null, arguments)
  44. @render {stack, message: exc.toString().replace /^.*: /, ''}, no
  45. @
  46. stop: ->
  47. Error.prepareStackTrace = @_oldPrepareStackTrace
  48. @_oldPrepareStackTrace = null
  49. config: (c) ->
  50. if c.skipPackages?
  51. if c.skipPackages is no
  52. @unskipAllPackages()
  53. else
  54. @skipPackage.apply @, c.skipPackages
  55. if c.skipPaths?
  56. if c.skipPaths is no
  57. @unskipAllPaths()
  58. else
  59. @skipPath.apply @, c.skipPaths
  60. if c.skip?
  61. if c.skip is no
  62. @unskipAll()
  63. else
  64. @skip.apply @, c.skip
  65. if c.maxItems?
  66. @setMaxItems c.maxItems
  67. if c.skipNodeFiles is yes
  68. @skipNodeFiles()
  69. else if c.skipNodeFiles is no
  70. @unskipNodeFiles()
  71. if c.filters?
  72. if c.filters is no
  73. @removeAllFilters()
  74. else
  75. @filter.apply @, c.filters
  76. if c.parsedErrorFilters?
  77. if c.parsedErrorFilters is no
  78. @removeAllParsedErrorFilters()
  79. else
  80. @filterParsedError.apply @, c.parsedErrorFilters
  81. if c.aliases?
  82. if object.isBareObject c.aliases
  83. @alias path, alias for path, alias of c.aliases
  84. else if c.aliases is no
  85. @removeAllAliases()
  86. @
  87. withoutColors: ->
  88. @_useColors = false
  89. @
  90. withColors: ->
  91. @_useColors = true
  92. @
  93. skipPackage: (packages...) ->
  94. @_packagesToSkip.push String pkg for pkg in packages
  95. @
  96. unskipPackage: (packages...) ->
  97. array.pluckOneItem(@_packagesToSkip, pkg) for pkg in packages
  98. @
  99. unskipAllPackages: ->
  100. @_packagesToSkip.length = 0
  101. @
  102. skipPath: (paths...) ->
  103. @_pathsToSkip.push path for path in paths
  104. @
  105. unskipPath: (paths...) ->
  106. array.pluckOneItem(@_pathsToSkip, path) for path in paths
  107. @
  108. unskipAllPaths: ->
  109. @_pathsToSkip.length = 0
  110. @
  111. skip: (callbacks...) ->
  112. @_skipCallbacks.push cb for cb in callbacks
  113. @
  114. unskip: (callbacks...) ->
  115. array.pluckOneItem(@_skipCallbacks, cb) for cb in callbacks
  116. @
  117. unskipAll: ->
  118. @_skipCallbacks.length = 0
  119. @
  120. skipNodeFiles: ->
  121. @skipPath.apply @, nodePaths
  122. unskipNodeFiles: ->
  123. @unskipPath.apply @, nodePaths
  124. filter: (callbacks...) ->
  125. @_filterCallbacks.push cb for cb in callbacks
  126. @
  127. removeFilter: (callbacks...) ->
  128. array.pluckOneItem(@_filterCallbacks, cb) for cb in callbacks
  129. @
  130. removeAllFilters: ->
  131. @_filterCallbacks.length = 0
  132. @
  133. filterParsedError: (callbacks...) ->
  134. @_parsedErrorFilters.push cb for cb in callbacks
  135. @
  136. removeParsedErrorFilter: (callbacks...) ->
  137. array.pluckOneItem(@_parsedErrorFilters, cb) for cb in callbacks
  138. @
  139. removeAllParsedErrorFilters: ->
  140. @_parsedErrorFilters.length = 0
  141. @
  142. setMaxItems: (maxItems = 50) ->
  143. if maxItems is 0 then maxItems = 50
  144. @_maxItems = maxItems|0
  145. @
  146. alias: (stringOrRx, alias) ->
  147. @_aliases.push {stringOrRx, alias}
  148. @
  149. removeAlias: (stringOrRx) ->
  150. array.pluckByCallback @_aliases, (pair) ->
  151. pair.stringOrRx is stringOrRx
  152. @
  153. removeAllAliases: ->
  154. @_aliases.length = 0
  155. @
  156. _getStyle: ->
  157. @_style
  158. appendStyle: (toAppend) ->
  159. object.appendOnto @_style, toAppend
  160. @_renderer.style toAppend
  161. @
  162. _getRenderer: ->
  163. @_renderer
  164. render: (e, logIt = no, useColors = @_useColors) ->
  165. obj = @getObject e
  166. rendered = @_renderer.render(obj, useColors)
  167. console.error rendered if logIt is yes
  168. rendered
  169. getObject: (e) ->
  170. unless e instanceof ParsedError
  171. e = new ParsedError e
  172. @_applyParsedErrorFiltersOn e
  173. header =
  174. title: do ->
  175. ret = {}
  176. # some errors are thrown to display other errors.
  177. # we call them wrappers here.
  178. if e.wrapper isnt ''
  179. ret.wrapper = "#{e.wrapper}"
  180. ret.kind = e.kind
  181. ret
  182. colon: ':'
  183. message: String(e.message).trim()
  184. traceItems = []
  185. count = -1
  186. for item, i in e.trace
  187. continue unless item?
  188. continue if @_skipOrFilter(item, i) is yes
  189. count++
  190. break if count > @_maxItems
  191. if typeof item is 'string'
  192. traceItems.push item: custom: item
  193. continue
  194. traceItems.push item:
  195. header:
  196. pointer: do ->
  197. return '' unless item.file?
  198. file: item.file
  199. colon: ':'
  200. line: item.line
  201. what: item.what
  202. footer: do ->
  203. foooter = addr: item.shortenedAddr
  204. if item.extra? then foooter.extra = item.extra
  205. foooter
  206. obj = 'pretty-error':
  207. header: header
  208. if traceItems.length > 0
  209. obj['pretty-error'].trace = traceItems
  210. obj
  211. _skipOrFilter: (item, itemNumber) ->
  212. if typeof item is 'object'
  213. return yes if item.modName in @_packagesToSkip
  214. return yes if item.path in @_pathsToSkip
  215. for modName in item.packages
  216. return yes if modName in @_packagesToSkip
  217. if typeof item.shortenedAddr is 'string'
  218. for pair in @_aliases
  219. item.shortenedAddr = item.shortenedAddr.replace pair.stringOrRx, pair.alias
  220. for cb in @_skipCallbacks
  221. return yes if cb(item, itemNumber) is yes
  222. for cb in @_filterCallbacks
  223. cb(item, itemNumber)
  224. return no
  225. _applyParsedErrorFiltersOn: (error) ->
  226. for cb in @_parsedErrorFilters
  227. cb error
  228. return
  229. for prop in ['renderer', 'style'] then do ->
  230. methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length)
  231. PrettyError::__defineGetter__ prop, -> do @[methodName]