|
9 | 9 | } else { |
10 | 10 | root.ErrorStackParser = factory(root.StackFrame); |
11 | 11 | } |
12 | | -}(this, function () { |
| 12 | +}(this, function ErrorStackParser(StackFrame) { |
13 | 13 | 'use strict'; |
14 | 14 |
|
15 | 15 | // ES5 Polyfills |
|
97 | 97 | }; |
98 | 98 | } |
99 | 99 |
|
100 | | - return function ErrorStackParser() { |
101 | | - this.firefoxSafariStackEntryRegExp = /\S+\:\d+/; |
102 | | - this.chromeIEStackEntryRegExp = /\s+at /; |
| 100 | + var FIREFOX_SAFARI_STACK_REGEXP = /\S+\:\d+/; |
| 101 | + var CHROME_IE_STACK_REGEXP = /\s+at /; |
103 | 102 |
|
| 103 | + return { |
104 | 104 | /** |
105 | 105 | * Given an Error object, extract the most information from it. |
106 | 106 | * @param error {Error} |
107 | 107 | * @return Array[StackFrame] |
108 | 108 | */ |
109 | | - this.parse = function parse(error) { |
| 109 | + parse: function parse(error) { |
110 | 110 | if (typeof error.stacktrace !== 'undefined' || typeof error['opera#sourceloc'] !== 'undefined') { |
111 | 111 | return this.parseOpera(error); |
112 | | - } else if (error.stack.match(this.chromeIEStackEntryRegExp)) { |
| 112 | + } else if (error.stack.match(CHROME_IE_STACK_REGEXP)) { |
113 | 113 | return this.parseV8OrIE(error); |
114 | | - } else if (error.stack.match(this.firefoxSafariStackEntryRegExp)) { |
| 114 | + } else if (error.stack.match(FIREFOX_SAFARI_STACK_REGEXP)) { |
115 | 115 | return this.parseFFOrSafari(error); |
116 | 116 | } else { |
117 | 117 | throw new Error('Cannot parse given Error object'); |
118 | 118 | } |
119 | | - }; |
| 119 | + }, |
120 | 120 |
|
121 | 121 | /** |
122 | 122 | * Separate line and column numbers from a URL-like string. |
123 | 123 | * @param urlLike String |
124 | 124 | * @return Array[String] |
125 | 125 | */ |
126 | | - this.extractLocation = function extractLocation(urlLike) { |
| 126 | + extractLocation: function extractLocation(urlLike) { |
127 | 127 | var locationParts = urlLike.split(':'); |
128 | 128 | var lastNumber = locationParts.pop(); |
129 | 129 | var possibleNumber = locationParts[locationParts.length - 1]; |
|
133 | 133 | } else { |
134 | 134 | return [locationParts.join(':'), lastNumber, undefined]; |
135 | 135 | } |
136 | | - }; |
| 136 | + }, |
137 | 137 |
|
138 | | - this.parseV8OrIE = function parseV8OrIE(error) { |
| 138 | + parseV8OrIE: function parseV8OrIE(error) { |
139 | 139 | return error.stack.split('\n').slice(1).map(function (line) { |
140 | 140 | var tokens = line.replace(/^\s+/, '').split(/\s+/).slice(1); |
141 | 141 | var locationParts = this.extractLocation(tokens.pop().replace(/[\(\)\s]/g, '')); |
142 | 142 | var functionName = (!tokens[0] || tokens[0] === 'Anonymous') ? undefined : tokens[0]; |
143 | 143 | return new StackFrame(functionName, undefined, locationParts[0], locationParts[1], locationParts[2]); |
144 | 144 | }.bind(this)); |
145 | | - }; |
| 145 | + }, |
146 | 146 |
|
147 | | - this.parseFFOrSafari = function parseFFOrSafari(error) { |
| 147 | + parseFFOrSafari: function parseFFOrSafari(error) { |
148 | 148 | return error.stack.split('\n').filter(function (line) { |
149 | | - return !!line.match(this.firefoxSafariStackEntryRegExp); |
| 149 | + return !!line.match(FIREFOX_SAFARI_STACK_REGEXP); |
150 | 150 | }.bind(this)).map(function (line) { |
151 | 151 | var tokens = line.split('@'); |
152 | 152 | var locationParts = this.extractLocation(tokens.pop()); |
153 | 153 | var functionName = tokens.shift() || undefined; |
154 | 154 | return new StackFrame(functionName, undefined, locationParts[0], locationParts[1], locationParts[2]); |
155 | 155 | }.bind(this)); |
156 | | - }; |
| 156 | + }, |
157 | 157 |
|
158 | | - this.parseOpera = function parseOpera(e) { |
| 158 | + parseOpera: function parseOpera(e) { |
159 | 159 | if (!e.stacktrace || (e.message.indexOf('\n') > -1 && |
160 | 160 | e.message.split('\n').length > e.stacktrace.split('\n').length)) { |
161 | 161 | return this.parseOpera9(e); |
|
166 | 166 | } else { |
167 | 167 | return this.parseOpera11(e); |
168 | 168 | } |
169 | | - }; |
| 169 | + }, |
170 | 170 |
|
171 | | - this.parseOpera9 = function parseOpera9(e) { |
| 171 | + parseOpera9: function parseOpera9(e) { |
172 | 172 | var lineRE = /Line (\d+).*script (?:in )?(\S+)/i; |
173 | 173 | var lines = e.message.split('\n'); |
174 | 174 | var result = []; |
|
181 | 181 | } |
182 | 182 |
|
183 | 183 | return result; |
184 | | - }; |
| 184 | + }, |
185 | 185 |
|
186 | | - this.parseOpera10a = function parseOpera10a(e) { |
| 186 | + parseOpera10a: function parseOpera10a(e) { |
187 | 187 | var lineRE = /Line (\d+).*script (?:in )?(\S+)(?:: In function (\S+))?$/i; |
188 | 188 | var lines = e.stacktrace.split('\n'); |
189 | 189 | var result = []; |
|
196 | 196 | } |
197 | 197 |
|
198 | 198 | return result; |
199 | | - }; |
| 199 | + }, |
200 | 200 |
|
201 | 201 | // Opera 10.65+ Error.stack very similar to FF/Safari |
202 | | - this.parseOpera11 = function parseOpera11(error) { |
| 202 | + parseOpera11: function parseOpera11(error) { |
203 | 203 | return error.stack.split('\n').filter(function (line) { |
204 | | - return !!line.match(this.firefoxSafariStackEntryRegExp); |
| 204 | + return !!line.match(FIREFOX_SAFARI_STACK_REGEXP); |
205 | 205 | }.bind(this)).map(function (line) { |
206 | 206 | var tokens = line.split('@'); |
207 | 207 | var location = tokens.pop().split(':'); |
|
211 | 211 | var args = (argsRaw === undefined || argsRaw === '[arguments not available]') ? undefined : argsRaw.split(','); |
212 | 212 | return new StackFrame(functionName, args, location[0] + ':' + location[1], location[2], location[3]); |
213 | 213 | }); |
214 | | - }; |
| 214 | + } |
215 | 215 | }; |
216 | 216 | })); |
217 | 217 |
|
0 commit comments