source: imaps-frontend/node_modules/pretty-error/src/ParsedError.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: 5.3 KB
Line 
1sysPath = require 'path'
2
3module.exports = class ParsedError
4 constructor: (@error) ->
5 do @_parse
6
7 _parse: ->
8 @_trace = []
9 @_kind = 'Error'
10 @_wrapper = ''
11
12 @_wrapper = String @error.wrapper if @error.wrapper?
13
14 unless typeof @error is 'object'
15 @_message = String @error
16 else
17 @_stack = @error.stack
18
19 if @error.kind?
20 @_kind = String @error.kind
21 else if typeof @_stack is 'string'
22 if m = @_stack.match /^([a-zA-Z0-9\_\$]+):\ /
23 @_kind = m[1]
24
25 @_message = @error.message? and String(@error.message) or ''
26 if typeof @_stack is 'string'
27 @_parseStack()
28
29 return
30
31 _parseStack: ->
32 messageLines = []
33 reachedTrace = no
34
35 for line in @_stack.split '\n'
36 continue if line.trim() is ''
37 if reachedTrace
38 @_trace.push @_parseTraceItem line
39 else
40 if line.match /^\s*at\s.+/
41 reachedTrace = yes
42 @_trace.push @_parseTraceItem line
43 else if !@_message.split '\n'.indexOf line
44 messageLines.push line
45
46 message = messageLines.join '\n'
47 if message.substr(0, @_kind.length) is @_kind
48 message =
49 message
50 .substr(@_kind.length, message.length)
51 .replace(/^\:\s+/, '')
52
53 if message.length
54 @_message = if @_message.length
55 then [
56 @_message
57 message
58 ].join '\n'
59 else
60 message
61
62 return
63
64 _parseTraceItem: (text) ->
65 text = text.trim()
66
67 return if text is ''
68 return text unless text.match /^at\ /
69
70 # remove the 'at ' part
71 text = text.replace /^at /, ''
72
73 return if text in ['Error (<anonymous>)', 'Error (<anonymous>:null:null)']
74
75 original = text
76
77 # the part that comes before the address
78 what = null
79
80 # address, including path to module and line/col
81 addr = null
82
83 # path to module
84 path = null
85
86 # module dir
87 dir = null
88
89 # module basename
90 file = null
91
92 # line number (if using a compiler, the line number of the module
93 # in that compiler will be used)
94 line = null
95
96 # column, same as above
97 col = null
98
99 # if using a compiler, this will translate to the line number of
100 # the js equivalent of that module
101 jsLine = null
102
103 # like above
104 jsCol = null
105
106 # path that doesn't include `node_module` dirs
107 shortenedPath = null
108
109 # like above
110 shortenedAddr = null
111
112 packageName = '[current]'
113
114 # pick out the address
115 if m = text.match /\(([^\)]+)\)$/
116 addr = m[1].trim()
117
118 if addr?
119 what = text.substr 0, text.length - addr.length - 2
120 what = what.trim()
121
122 # might not have a 'what' clause
123 unless addr?
124 addr = text.trim()
125
126 addr = @_fixPath addr
127 remaining = addr
128
129 # remove the <js> clause if the file is a compiled one
130 if m = remaining.match /\,\ <js>:(\d+):(\d+)$/
131 jsLine = m[1]
132 jsCol = m[2]
133 remaining = remaining.substr 0, remaining.length - m[0].length
134
135 # the line/col part
136 if m = remaining.match /:(\d+):(\d+)$/
137 line = m[1]
138 col = m[2]
139 remaining = remaining.substr 0, remaining.length - m[0].length
140 path = remaining
141
142 # file and dir
143 if path?
144 file = sysPath.basename path
145 dir = sysPath.dirname path
146
147 if dir is '.' then dir = ''
148
149 path = @_fixPath path
150 file = @_fixPath file
151 dir = @_fixPath dir
152
153 if dir?
154 d = dir.replace /[\\]{1,2}/g, '/'
155 if m = d.match ///
156 node_modules/([^/]+)(?!.*node_modules.*)
157 ///
158
159 packageName = m[1]
160
161 unless jsLine?
162 jsLine = line
163 jsCol = col
164
165 if path?
166 r = @_rectifyPath path
167 shortenedPath = r.path
168 shortenedAddr = shortenedPath + addr.substr(path.length, addr.length)
169 packages = r.packages
170
171 original: original
172 what: what
173 addr: addr
174 path: path
175 dir: dir
176 file: file
177 line: parseInt line
178 col: parseInt col
179 jsLine: parseInt jsLine
180 jsCol: parseInt jsCol
181 packageName: packageName
182 shortenedPath: shortenedPath
183 shortenedAddr: shortenedAddr
184 packages: packages || []
185
186 _getMessage: -> @_message
187 _getKind: -> @_kind
188 _getWrapper: -> @_wrapper
189 _getStack: -> @_stack
190 _getArguments: -> @error.arguments
191 _getType: -> @error.type
192 _getTrace: -> @_trace
193 _fixPath: (path) -> path.replace(///[\\]{1,2}///g, '/')
194
195 _rectifyPath: (path, nameForCurrentPackage) ->
196 path = String path
197 remaining = path
198
199 return path: path, packages: [] unless m = path.match /^(.+?)\/node_modules\/(.+)$/
200
201 parts = []
202 packages = []
203
204 if typeof nameForCurrentPackage is 'string'
205 parts.push "[#{nameForCurrentPackage}]"
206 packages.push "[#{nameForCurrentPackage}]"
207 else
208 parts.push "[#{m[1].match(/([^\/]+)$/)[1]}]"
209 packages.push m[1].match(/([^\/]+)$/)[1]
210
211 rest = m[2]
212
213 while m = rest.match /([^\/]+)\/node_modules\/(.+)$/
214 parts.push "[#{m[1]}]"
215 packages.push m[1]
216 rest = m[2]
217
218 if m = rest.match /([^\/]+)\/(.+)$/
219 parts.push "[#{m[1]}]"
220 packages.push m[1]
221 rest = m[2]
222
223 parts.push rest
224
225 path: parts.join "/"
226 packages: packages
227
228for prop in ['message', 'kind', 'arguments', 'type', 'stack', 'trace', 'wrapper'] then do ->
229 methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length)
230
231 Object.defineProperty ParsedError::, prop,
232 get: -> this[methodName]()
Note: See TracBrowser for help on using the repository browser.