ravenjs-gem 1.0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2013 UC Berkeley - ETS
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Raven.js Gem
2
+ [![Gem Version](https://badge.fury.io/rb/ravenjs-gem.png)](http://badge.fury.io/rb/ravenjs-gem) [![Dependency Status](https://gemnasium.com/ets-berkeley-edu/ravenjs-gem.png)](https://gemnasium.com/ets-berkeley-edu/ravenjs-gem) [![Code Climate](https://codeclimate.com/github/ets-berkeley-edu/ravenjs-gem.png)](https://codeclimate.com/github/ets-berkeley-edu/ravenjs-gem)
3
+
4
+ [Raven.js][ravenjs] as a Ruby gem.
5
+
6
+ ## Getting Started
7
+
8
+ Add the gem to your Gemfile:
9
+
10
+ ```ruby
11
+ gem "ravenjs-gem"
12
+ ```
13
+
14
+ And run
15
+
16
+ ```bash
17
+ bundle install
18
+ ```
19
+ in the terminal to download the resources.
20
+
21
+ ### Adding the files to your projects
22
+
23
+ In order for the files to load, you'll need to do add them.
24
+
25
+ `application.js`:
26
+
27
+ ```javascript
28
+ //= require ravenjs
29
+ ```
30
+
31
+ and you should be good to go.
32
+
33
+ ## Updating this plug-in
34
+
35
+ If you would like to update this gem you should take the following steps:
36
+
37
+ 1. `rake download VERSION=X.X.X`. If you don't specify the version, it will get the latest one.
38
+ 1. `rake tag VERSION=X.X.X` will tag the version you've specified as the standard version.
39
+ 1. Make a Pull request
40
+
41
+ Then the maintainer of the gem will need to do the following steps:
42
+
43
+ 1. Update the version [lib/ravenjs-gem/version.rb](lib/ravenjs-gem/version.rb)
44
+ 1. Run ``gem build ravenjs-gem.gemspec`` to package the gem
45
+ 1. Once satisfied, push the gem up to RubyGems.org with ``gem push ravenjs-gem-<VERSION>.gem``
46
+ 1. Update [the changelog](CHANGELOG.md)
47
+
48
+ [ravenjs]: https://github.com/getsentry/raven-js
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ Bundler::GemHelper.install_tasks
9
+
10
+ require 'rake'
11
+ require 'open-uri'
12
+ require 'json'
13
+
14
+ dir_assets = 'vendor/assets/'
15
+ dir_js = dir_assets + 'javascripts'
16
+
17
+ desc 'Downloads the raven.js files from GitHub'
18
+ task :download do |t|
19
+
20
+ def create_dir dir, version
21
+ dir_name = File.join(dir, version)
22
+ Dir.mkdir(dir_name) unless Dir.exist?(dir_name)
23
+ end
24
+
25
+ def download_file url, dir, filename, version
26
+ Dir.chdir(File.join(dir, version)) do
27
+ if File.exists?(filename)
28
+ puts " #{dir + '/' + version + '/' + filename} already exists"
29
+ next
30
+ end
31
+ puts " #{url}"
32
+ open(filename, "w") { |file| file.write(open(url).read)}
33
+ end
34
+ end
35
+
36
+ # Specify which version you want
37
+ version = ENV['VERSION']
38
+ version ||= 'latest'
39
+ puts "Target version: #{version.chomp('/')}"
40
+
41
+ # Get the different versions
42
+ tags_url = 'https://api.github.com/repos/getsentry/raven-js/tags'
43
+ result = JSON.parse(open(tags_url).read)
44
+ versions = result.map { |item| item['name'] }
45
+
46
+ puts "Available versions: #{versions.inspect}"
47
+
48
+ # Figuring out which version to get
49
+ if versions.include? version
50
+ get_version = version
51
+ else
52
+ get_version = versions.first
53
+
54
+ if !(versions.include? version) && version != 'latest'
55
+ puts "Warning: The version you've specified: '#{version}' wasn't found, using the latest version instead: '#{get_version}'"
56
+ end
57
+ end
58
+
59
+ # Get the right commit
60
+ commit = result.select { |item| item['name'] == get_version }.first['commit']['sha']
61
+ puts "We'll use the following commit to get the files: #{commit}"
62
+
63
+ # Creating the necessary directories
64
+ create_dir dir_js, get_version
65
+
66
+ # Download the right files
67
+ url_root = 'https://raw.github.com/getsentry/raven-js/' + commit + '/dist/' + get_version + '/'
68
+ url_js = url_root + 'raven.js'
69
+ url_js_min = url_root + 'raven.min.js'
70
+ url_js_map = url_root + 'raven.min.map'
71
+
72
+ puts "Downloading... \n"
73
+ download_file url_js, dir_js, 'raven.js', get_version
74
+ download_file url_js_min, dir_js, 'raven.min.js', get_version
75
+ download_file url_js_map, dir_js, 'raven.min.map', get_version
76
+
77
+ end
78
+
79
+ desc 'Tag the default file versions for asset helpers'
80
+ task :tag do |t|
81
+ Rake::Task['tag_default'].invoke
82
+ end
83
+
84
+ task :tag_default do |t|
85
+
86
+ def copy_files dir, version
87
+ Dir.entries(File.join(dir, version)).each do |file|
88
+ file_source = File.join(dir, version, file)
89
+ file_destination = File.join(dir, file)
90
+ if File.file?(file_source)
91
+ FileUtils.cp file_source, file_destination, { verbose: true }
92
+ end
93
+ end
94
+ end
95
+
96
+ version = ENV['VERSION']
97
+ version ||= 'latest'
98
+
99
+ puts "Target version: #{version.chomp('/')}"
100
+
101
+ Dir.chdir(dir_js) do
102
+ version_directories = Dir.glob("*").select { |fn| File.directory?(fn) }.sort.reverse
103
+
104
+ puts "Available versions: #{version_directories.inspect}"
105
+ if !(version_directories.include? version)
106
+ if version != 'latest'
107
+ puts "WARN: Specified version='#{version}' not found, setting to latest version: '#{version_directories.first}'"
108
+ end
109
+ version = version_directories.first
110
+ end
111
+ end
112
+
113
+ copy_files dir_js, version
114
+ end
@@ -0,0 +1,6 @@
1
+ require "rails/engine"
2
+
3
+ module RavenjsGem
4
+ class Engine < Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module RavenjsGem
2
+ VERSION = "1.0.7.0"
3
+ end
@@ -0,0 +1,1648 @@
1
+ /*! Raven.js 1.0.6 | github.com/getsentry/raven-js */
2
+
3
+ /*
4
+ * Includes TraceKit
5
+ * https://github.com/getsentry/TraceKit
6
+ *
7
+ * Copyright 2013 Matt Robenolt and other contributors
8
+ * Released under the BSD license
9
+ * https://github.com/getsentry/raven-js/blob/master/LICENSE
10
+ *
11
+ */
12
+ /*
13
+ TraceKit - Cross brower stack traces - github.com/occ/TraceKit
14
+ MIT license
15
+ */
16
+
17
+ ;(function(window, undefined) {
18
+
19
+
20
+ var TraceKit = {};
21
+ var _oldTraceKit = window.TraceKit;
22
+
23
+ // global reference to slice
24
+ var _slice = [].slice;
25
+ var UNKNOWN_FUNCTION = '?';
26
+
27
+
28
+ /**
29
+ * _has, a better form of hasOwnProperty
30
+ * Example: _has(MainHostObject, property) === true/false
31
+ *
32
+ * @param {Object} host object to check property
33
+ * @param {string} key to check
34
+ */
35
+ function _has(object, key) {
36
+ return Object.prototype.hasOwnProperty.call(object, key);
37
+ }
38
+
39
+ function _isUndefined(what) {
40
+ return typeof what === 'undefined';
41
+ }
42
+
43
+ /**
44
+ * TraceKit.noConflict: Export TraceKit out to another variable
45
+ * Example: var TK = TraceKit.noConflict()
46
+ */
47
+ TraceKit.noConflict = function noConflict() {
48
+ window.TraceKit = _oldTraceKit;
49
+ return TraceKit;
50
+ };
51
+
52
+ /**
53
+ * TraceKit.wrap: Wrap any function in a TraceKit reporter
54
+ * Example: func = TraceKit.wrap(func);
55
+ *
56
+ * @param {Function} func Function to be wrapped
57
+ * @return {Function} The wrapped func
58
+ */
59
+ TraceKit.wrap = function traceKitWrapper(func) {
60
+ function wrapped() {
61
+ try {
62
+ return func.apply(this, arguments);
63
+ } catch (e) {
64
+ TraceKit.report(e);
65
+ throw e;
66
+ }
67
+ }
68
+ return wrapped;
69
+ };
70
+
71
+ /**
72
+ * TraceKit.report: cross-browser processing of unhandled exceptions
73
+ *
74
+ * Syntax:
75
+ * TraceKit.report.subscribe(function(stackInfo) { ... })
76
+ * TraceKit.report.unsubscribe(function(stackInfo) { ... })
77
+ * TraceKit.report(exception)
78
+ * try { ...code... } catch(ex) { TraceKit.report(ex); }
79
+ *
80
+ * Supports:
81
+ * - Firefox: full stack trace with line numbers, plus column number
82
+ * on top frame; column number is not guaranteed
83
+ * - Opera: full stack trace with line and column numbers
84
+ * - Chrome: full stack trace with line and column numbers
85
+ * - Safari: line and column number for the top frame only; some frames
86
+ * may be missing, and column number is not guaranteed
87
+ * - IE: line and column number for the top frame only; some frames
88
+ * may be missing, and column number is not guaranteed
89
+ *
90
+ * In theory, TraceKit should work on all of the following versions:
91
+ * - IE5.5+ (only 8.0 tested)
92
+ * - Firefox 0.9+ (only 3.5+ tested)
93
+ * - Opera 7+ (only 10.50 tested; versions 9 and earlier may require
94
+ * Exceptions Have Stacktrace to be enabled in opera:config)
95
+ * - Safari 3+ (only 4+ tested)
96
+ * - Chrome 1+ (only 5+ tested)
97
+ * - Konqueror 3.5+ (untested)
98
+ *
99
+ * Requires TraceKit.computeStackTrace.
100
+ *
101
+ * Tries to catch all unhandled exceptions and report them to the
102
+ * subscribed handlers. Please note that TraceKit.report will rethrow the
103
+ * exception. This is REQUIRED in order to get a useful stack trace in IE.
104
+ * If the exception does not reach the top of the browser, you will only
105
+ * get a stack trace from the point where TraceKit.report was called.
106
+ *
107
+ * Handlers receive a stackInfo object as described in the
108
+ * TraceKit.computeStackTrace docs.
109
+ */
110
+ TraceKit.report = (function reportModuleWrapper() {
111
+ var handlers = [],
112
+ lastException = null,
113
+ lastExceptionStack = null;
114
+
115
+ /**
116
+ * Add a crash handler.
117
+ * @param {Function} handler
118
+ */
119
+ function subscribe(handler) {
120
+ handlers.push(handler);
121
+ }
122
+
123
+ /**
124
+ * Remove a crash handler.
125
+ * @param {Function} handler
126
+ */
127
+ function unsubscribe(handler) {
128
+ for (var i = handlers.length - 1; i >= 0; --i) {
129
+ if (handlers[i] === handler) {
130
+ handlers.splice(i, 1);
131
+ }
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Dispatch stack information to all handlers.
137
+ * @param {Object.<string, *>} stack
138
+ */
139
+ function notifyHandlers(stack, windowError) {
140
+ var exception = null;
141
+ if (windowError && !TraceKit.collectWindowErrors) {
142
+ return;
143
+ }
144
+ for (var i in handlers) {
145
+ if (_has(handlers, i)) {
146
+ try {
147
+ handlers[i].apply(null, [stack].concat(_slice.call(arguments, 2)));
148
+ } catch (inner) {
149
+ exception = inner;
150
+ }
151
+ }
152
+ }
153
+
154
+ if (exception) {
155
+ throw exception;
156
+ }
157
+ }
158
+
159
+ var _oldOnerrorHandler = window.onerror;
160
+
161
+ /**
162
+ * Ensures all global unhandled exceptions are recorded.
163
+ * Supported by Gecko and IE.
164
+ * @param {string} message Error message.
165
+ * @param {string} url URL of script that generated the exception.
166
+ * @param {(number|string)} lineNo The line number at which the error
167
+ * occurred.
168
+ */
169
+ window.onerror = function traceKitWindowOnError(message, url, lineNo) {
170
+ var stack = null;
171
+
172
+ if (lastExceptionStack) {
173
+ TraceKit.computeStackTrace.augmentStackTraceWithInitialElement(lastExceptionStack, url, lineNo, message);
174
+ stack = lastExceptionStack;
175
+ lastExceptionStack = null;
176
+ lastException = null;
177
+ } else {
178
+ var location = {
179
+ 'url': url,
180
+ 'line': lineNo
181
+ };
182
+ location.func = TraceKit.computeStackTrace.guessFunctionName(location.url, location.line);
183
+ location.context = TraceKit.computeStackTrace.gatherContext(location.url, location.line);
184
+ stack = {
185
+ 'mode': 'onerror',
186
+ 'message': message,
187
+ 'url': document.location.href,
188
+ 'stack': [location],
189
+ 'useragent': navigator.userAgent
190
+ };
191
+ }
192
+
193
+ notifyHandlers(stack, 'from window.onerror');
194
+
195
+ if (_oldOnerrorHandler) {
196
+ return _oldOnerrorHandler.apply(this, arguments);
197
+ }
198
+
199
+ return false;
200
+ };
201
+
202
+ /**
203
+ * Reports an unhandled Error to TraceKit.
204
+ * @param {Error} ex
205
+ */
206
+ function report(ex) {
207
+ var args = _slice.call(arguments, 1);
208
+ if (lastExceptionStack) {
209
+ if (lastException === ex) {
210
+ return; // already caught by an inner catch block, ignore
211
+ } else {
212
+ var s = lastExceptionStack;
213
+ lastExceptionStack = null;
214
+ lastException = null;
215
+ notifyHandlers.apply(null, [s, null].concat(args));
216
+ }
217
+ }
218
+
219
+ var stack = TraceKit.computeStackTrace(ex);
220
+ lastExceptionStack = stack;
221
+ lastException = ex;
222
+
223
+ // If the stack trace is incomplete, wait for 2 seconds for
224
+ // slow slow IE to see if onerror occurs or not before reporting
225
+ // this exception; otherwise, we will end up with an incomplete
226
+ // stack trace
227
+ window.setTimeout(function () {
228
+ if (lastException === ex) {
229
+ lastExceptionStack = null;
230
+ lastException = null;
231
+ notifyHandlers.apply(null, [stack, null].concat(args));
232
+ }
233
+ }, (stack.incomplete ? 2000 : 0));
234
+
235
+ throw ex; // re-throw to propagate to the top level (and cause window.onerror)
236
+ }
237
+
238
+ report.subscribe = subscribe;
239
+ report.unsubscribe = unsubscribe;
240
+ return report;
241
+ }());
242
+
243
+ /**
244
+ * TraceKit.computeStackTrace: cross-browser stack traces in JavaScript
245
+ *
246
+ * Syntax:
247
+ * s = TraceKit.computeStackTrace.ofCaller([depth])
248
+ * s = TraceKit.computeStackTrace(exception) // consider using TraceKit.report instead (see below)
249
+ * Returns:
250
+ * s.name - exception name
251
+ * s.message - exception message
252
+ * s.stack[i].url - JavaScript or HTML file URL
253
+ * s.stack[i].func - function name, or empty for anonymous functions (if guessing did not work)
254
+ * s.stack[i].args - arguments passed to the function, if known
255
+ * s.stack[i].line - line number, if known
256
+ * s.stack[i].column - column number, if known
257
+ * s.stack[i].context - an array of source code lines; the middle element corresponds to the correct line#
258
+ * s.mode - 'stack', 'stacktrace', 'multiline', 'callers', 'onerror', or 'failed' -- method used to collect the stack trace
259
+ *
260
+ * Supports:
261
+ * - Firefox: full stack trace with line numbers and unreliable column
262
+ * number on top frame
263
+ * - Opera 10: full stack trace with line and column numbers
264
+ * - Opera 9-: full stack trace with line numbers
265
+ * - Chrome: full stack trace with line and column numbers
266
+ * - Safari: line and column number for the topmost stacktrace element
267
+ * only
268
+ * - IE: no line numbers whatsoever
269
+ *
270
+ * Tries to guess names of anonymous functions by looking for assignments
271
+ * in the source code. In IE and Safari, we have to guess source file names
272
+ * by searching for function bodies inside all page scripts. This will not
273
+ * work for scripts that are loaded cross-domain.
274
+ * Here be dragons: some function names may be guessed incorrectly, and
275
+ * duplicate functions may be mismatched.
276
+ *
277
+ * TraceKit.computeStackTrace should only be used for tracing purposes.
278
+ * Logging of unhandled exceptions should be done with TraceKit.report,
279
+ * which builds on top of TraceKit.computeStackTrace and provides better
280
+ * IE support by utilizing the window.onerror event to retrieve information
281
+ * about the top of the stack.
282
+ *
283
+ * Note: In IE and Safari, no stack trace is recorded on the Error object,
284
+ * so computeStackTrace instead walks its *own* chain of callers.
285
+ * This means that:
286
+ * * in Safari, some methods may be missing from the stack trace;
287
+ * * in IE, the topmost function in the stack trace will always be the
288
+ * caller of computeStackTrace.
289
+ *
290
+ * This is okay for tracing (because you are likely to be calling
291
+ * computeStackTrace from the function you want to be the topmost element
292
+ * of the stack trace anyway), but not okay for logging unhandled
293
+ * exceptions (because your catch block will likely be far away from the
294
+ * inner function that actually caused the exception).
295
+ *
296
+ * Tracing example:
297
+ * function trace(message) {
298
+ * var stackInfo = TraceKit.computeStackTrace.ofCaller();
299
+ * var data = message + "\n";
300
+ * for(var i in stackInfo.stack) {
301
+ * var item = stackInfo.stack[i];
302
+ * data += (item.func || '[anonymous]') + "() in " + item.url + ":" + (item.line || '0') + "\n";
303
+ * }
304
+ * if (window.console)
305
+ * console.info(data);
306
+ * else
307
+ * alert(data);
308
+ * }
309
+ */
310
+ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
311
+ var debug = false,
312
+ sourceCache = {};
313
+
314
+ /**
315
+ * Attempts to retrieve source code via XMLHttpRequest, which is used
316
+ * to look up anonymous function names.
317
+ * @param {string} url URL of source code.
318
+ * @return {string} Source contents.
319
+ */
320
+ function loadSource(url) {
321
+ if (!TraceKit.remoteFetching) { //Only attempt request if remoteFetching is on.
322
+ return '';
323
+ }
324
+ try {
325
+ function getXHR() {
326
+ try {
327
+ return new window.XMLHttpRequest();
328
+ } catch (e) {
329
+ // explicitly bubble up the exception if not found
330
+ return new window.ActiveXObject('Microsoft.XMLHTTP');
331
+ }
332
+ }
333
+
334
+ var request = getXHR();
335
+ request.open('GET', url, false);
336
+ request.send('');
337
+ return request.responseText;
338
+ } catch (e) {
339
+ return '';
340
+ }
341
+ }
342
+
343
+ /**
344
+ * Retrieves source code from the source code cache.
345
+ * @param {string} url URL of source code.
346
+ * @return {Array.<string>} Source contents.
347
+ */
348
+ function getSource(url) {
349
+ if (!_has(sourceCache, url)) {
350
+ // URL needs to be able to fetched within the acceptable domain. Otherwise,
351
+ // cross-domain errors will be triggered.
352
+ var source = '';
353
+ if (url.indexOf(document.domain) !== -1) {
354
+ source = loadSource(url);
355
+ }
356
+ sourceCache[url] = source ? source.split('\n') : [];
357
+ }
358
+
359
+ return sourceCache[url];
360
+ }
361
+
362
+ /**
363
+ * Tries to use an externally loaded copy of source code to determine
364
+ * the name of a function by looking at the name of the variable it was
365
+ * assigned to, if any.
366
+ * @param {string} url URL of source code.
367
+ * @param {(string|number)} lineNo Line number in source code.
368
+ * @return {string} The function name, if discoverable.
369
+ */
370
+ function guessFunctionName(url, lineNo) {
371
+ var reFunctionArgNames = /function ([^(]*)\(([^)]*)\)/,
372
+ reGuessFunction = /['"]?([0-9A-Za-z$_]+)['"]?\s*[:=]\s*(function|eval|new Function)/,
373
+ line = '',
374
+ maxLines = 10,
375
+ source = getSource(url),
376
+ m;
377
+
378
+ if (!source.length) {
379
+ return UNKNOWN_FUNCTION;
380
+ }
381
+
382
+ // Walk backwards from the first line in the function until we find the line which
383
+ // matches the pattern above, which is the function definition
384
+ for (var i = 0; i < maxLines; ++i) {
385
+ line = source[lineNo - i] + line;
386
+
387
+ if (!_isUndefined(line)) {
388
+ if ((m = reGuessFunction.exec(line))) {
389
+ return m[1];
390
+ } else if ((m = reFunctionArgNames.exec(line))) {
391
+ return m[1];
392
+ }
393
+ }
394
+ }
395
+
396
+ return UNKNOWN_FUNCTION;
397
+ }
398
+
399
+ /**
400
+ * Retrieves the surrounding lines from where an exception occurred.
401
+ * @param {string} url URL of source code.
402
+ * @param {(string|number)} line Line number in source code to centre
403
+ * around for context.
404
+ * @return {?Array.<string>} Lines of source code.
405
+ */
406
+ function gatherContext(url, line) {
407
+ var source = getSource(url);
408
+
409
+ if (!source.length) {
410
+ return null;
411
+ }
412
+
413
+ var context = [],
414
+ // linesBefore & linesAfter are inclusive with the offending line.
415
+ // if linesOfContext is even, there will be one extra line
416
+ // *before* the offending line.
417
+ linesBefore = Math.floor(TraceKit.linesOfContext / 2),
418
+ // Add one extra line if linesOfContext is odd
419
+ linesAfter = linesBefore + (TraceKit.linesOfContext % 2),
420
+ start = Math.max(0, line - linesBefore - 1),
421
+ end = Math.min(source.length, line + linesAfter - 1);
422
+
423
+ line -= 1; // convert to 0-based index
424
+
425
+ for (var i = start; i < end; ++i) {
426
+ if (!_isUndefined(source[i])) {
427
+ context.push(source[i]);
428
+ }
429
+ }
430
+
431
+ return context.length > 0 ? context : null;
432
+ }
433
+
434
+ /**
435
+ * Escapes special characters, except for whitespace, in a string to be
436
+ * used inside a regular expression as a string literal.
437
+ * @param {string} text The string.
438
+ * @return {string} The escaped string literal.
439
+ */
440
+ function escapeRegExp(text) {
441
+ return text.replace(/[\-\[\]{}()*+?.,\\\^$|#]/g, '\\$&');
442
+ }
443
+
444
+ /**
445
+ * Escapes special characters in a string to be used inside a regular
446
+ * expression as a string literal. Also ensures that HTML entities will
447
+ * be matched the same as their literal friends.
448
+ * @param {string} body The string.
449
+ * @return {string} The escaped string.
450
+ */
451
+ function escapeCodeAsRegExpForMatchingInsideHTML(body) {
452
+ return escapeRegExp(body).replace('<', '(?:<|&lt;)').replace('>', '(?:>|&gt;)').replace('&', '(?:&|&amp;)').replace('"', '(?:"|&quot;)').replace(/\s+/g, '\\s+');
453
+ }
454
+
455
+ /**
456
+ * Determines where a code fragment occurs in the source code.
457
+ * @param {RegExp} re The function definition.
458
+ * @param {Array.<string>} urls A list of URLs to search.
459
+ * @return {?Object.<string, (string|number)>} An object containing
460
+ * the url, line, and column number of the defined function.
461
+ */
462
+ function findSourceInUrls(re, urls) {
463
+ var source, m;
464
+ for (var i = 0, j = urls.length; i < j; ++i) {
465
+ // console.log('searching', urls[i]);
466
+ if ((source = getSource(urls[i])).length) {
467
+ source = source.join('\n');
468
+ if ((m = re.exec(source))) {
469
+ // console.log('Found function in ' + urls[i]);
470
+
471
+ return {
472
+ 'url': urls[i],
473
+ 'line': source.substring(0, m.index).split('\n').length,
474
+ 'column': m.index - source.lastIndexOf('\n', m.index) - 1
475
+ };
476
+ }
477
+ }
478
+ }
479
+
480
+ // console.log('no match');
481
+
482
+ return null;
483
+ }
484
+
485
+ /**
486
+ * Determines at which column a code fragment occurs on a line of the
487
+ * source code.
488
+ * @param {string} fragment The code fragment.
489
+ * @param {string} url The URL to search.
490
+ * @param {(string|number)} line The line number to examine.
491
+ * @return {?number} The column number.
492
+ */
493
+ function findSourceInLine(fragment, url, line) {
494
+ var source = getSource(url),
495
+ re = new RegExp('\\b' + escapeRegExp(fragment) + '\\b'),
496
+ m;
497
+
498
+ line -= 1;
499
+
500
+ if (source && source.length > line && (m = re.exec(source[line]))) {
501
+ return m.index;
502
+ }
503
+
504
+ return null;
505
+ }
506
+
507
+ /**
508
+ * Determines where a function was defined within the source code.
509
+ * @param {(Function|string)} func A function reference or serialized
510
+ * function definition.
511
+ * @return {?Object.<string, (string|number)>} An object containing
512
+ * the url, line, and column number of the defined function.
513
+ */
514
+ function findSourceByFunctionBody(func) {
515
+ var urls = [window.location.href],
516
+ scripts = document.getElementsByTagName('script'),
517
+ body,
518
+ code = '' + func,
519
+ codeRE = /^function(?:\s+([\w$]+))?\s*\(([\w\s,]*)\)\s*\{\s*(\S[\s\S]*\S)\s*\}\s*$/,
520
+ eventRE = /^function on([\w$]+)\s*\(event\)\s*\{\s*(\S[\s\S]*\S)\s*\}\s*$/,
521
+ re,
522
+ parts,
523
+ result;
524
+
525
+ for (var i = 0; i < scripts.length; ++i) {
526
+ var script = scripts[i];
527
+ if (script.src) {
528
+ urls.push(script.src);
529
+ }
530
+ }
531
+
532
+ if (!(parts = codeRE.exec(code))) {
533
+ re = new RegExp(escapeRegExp(code).replace(/\s+/g, '\\s+'));
534
+ }
535
+
536
+ // not sure if this is really necessary, but I don’t have a test
537
+ // corpus large enough to confirm that and it was in the original.
538
+ else {
539
+ var name = parts[1] ? '\\s+' + parts[1] : '',
540
+ args = parts[2].split(',').join('\\s*,\\s*');
541
+
542
+ body = escapeRegExp(parts[3]).replace(/;$/, ';?'); // semicolon is inserted if the function ends with a comment.replace(/\s+/g, '\\s+');
543
+ re = new RegExp('function' + name + '\\s*\\(\\s*' + args + '\\s*\\)\\s*{\\s*' + body + '\\s*}');
544
+ }
545
+
546
+ // look for a normal function definition
547
+ if ((result = findSourceInUrls(re, urls))) {
548
+ return result;
549
+ }
550
+
551
+ // look for an old-school event handler function
552
+ if ((parts = eventRE.exec(code))) {
553
+ var event = parts[1];
554
+ body = escapeCodeAsRegExpForMatchingInsideHTML(parts[2]);
555
+
556
+ // look for a function defined in HTML as an onXXX handler
557
+ re = new RegExp('on' + event + '=[\\\'"]\\s*' + body + '\\s*[\\\'"]', 'i');
558
+
559
+ if ((result = findSourceInUrls(re, urls[0]))) {
560
+ return result;
561
+ }
562
+
563
+ // look for ???
564
+ re = new RegExp(body);
565
+
566
+ if ((result = findSourceInUrls(re, urls))) {
567
+ return result;
568
+ }
569
+ }
570
+
571
+ return null;
572
+ }
573
+
574
+ // Contents of Exception in various browsers.
575
+ //
576
+ // SAFARI:
577
+ // ex.message = Can't find variable: qq
578
+ // ex.line = 59
579
+ // ex.sourceId = 580238192
580
+ // ex.sourceURL = http://...
581
+ // ex.expressionBeginOffset = 96
582
+ // ex.expressionCaretOffset = 98
583
+ // ex.expressionEndOffset = 98
584
+ // ex.name = ReferenceError
585
+ //
586
+ // FIREFOX:
587
+ // ex.message = qq is not defined
588
+ // ex.fileName = http://...
589
+ // ex.lineNumber = 59
590
+ // ex.stack = ...stack trace... (see the example below)
591
+ // ex.name = ReferenceError
592
+ //
593
+ // CHROME:
594
+ // ex.message = qq is not defined
595
+ // ex.name = ReferenceError
596
+ // ex.type = not_defined
597
+ // ex.arguments = ['aa']
598
+ // ex.stack = ...stack trace...
599
+ //
600
+ // INTERNET EXPLORER:
601
+ // ex.message = ...
602
+ // ex.name = ReferenceError
603
+ //
604
+ // OPERA:
605
+ // ex.message = ...message... (see the example below)
606
+ // ex.name = ReferenceError
607
+ // ex.opera#sourceloc = 11 (pretty much useless, duplicates the info in ex.message)
608
+ // ex.stacktrace = n/a; see 'opera:config#UserPrefs|Exceptions Have Stacktrace'
609
+
610
+ /**
611
+ * Computes stack trace information from the stack property.
612
+ * Chrome and Gecko use this property.
613
+ * @param {Error} ex
614
+ * @return {?Object.<string, *>} Stack trace information.
615
+ */
616
+ function computeStackTraceFromStackProp(ex) {
617
+ if (!ex.stack) {
618
+ return null;
619
+ }
620
+
621
+ var chrome = /^\s*at (?:((?:\[object object\])?\S+) )?\(?((?:file|http|https):.*?):(\d+)(?::(\d+))?\)?\s*$/i,
622
+ gecko = /^\s*(\S*)(?:\((.*?)\))?@((?:file|http|https).*?):(\d+)(?::(\d+))?\s*$/i,
623
+ lines = ex.stack.split('\n'),
624
+ stack = [],
625
+ parts,
626
+ element,
627
+ reference = /^(.*) is undefined$/.exec(ex.message);
628
+
629
+ for (var i = 0, j = lines.length; i < j; ++i) {
630
+ if ((parts = gecko.exec(lines[i]))) {
631
+ element = {
632
+ 'url': parts[3],
633
+ 'func': parts[1] || UNKNOWN_FUNCTION,
634
+ 'args': parts[2] ? parts[2].split(',') : '',
635
+ 'line': +parts[4],
636
+ 'column': parts[5] ? +parts[5] : null
637
+ };
638
+ } else if ((parts = chrome.exec(lines[i]))) {
639
+ element = {
640
+ 'url': parts[2],
641
+ 'func': parts[1] || UNKNOWN_FUNCTION,
642
+ 'line': +parts[3],
643
+ 'column': parts[4] ? +parts[4] : null
644
+ };
645
+ } else {
646
+ continue;
647
+ }
648
+
649
+ if (!element.func && element.line) {
650
+ element.func = guessFunctionName(element.url, element.line);
651
+ }
652
+
653
+ if (element.line) {
654
+ element.context = gatherContext(element.url, element.line);
655
+ }
656
+
657
+ stack.push(element);
658
+ }
659
+
660
+ if (stack[0] && stack[0].line && !stack[0].column && reference) {
661
+ stack[0].column = findSourceInLine(reference[1], stack[0].url, stack[0].line);
662
+ }
663
+
664
+ if (!stack.length) {
665
+ return null;
666
+ }
667
+
668
+ return {
669
+ 'mode': 'stack',
670
+ 'name': ex.name,
671
+ 'message': ex.message,
672
+ 'url': document.location.href,
673
+ 'stack': stack,
674
+ 'useragent': navigator.userAgent
675
+ };
676
+ }
677
+
678
+ /**
679
+ * Computes stack trace information from the stacktrace property.
680
+ * Opera 10 uses this property.
681
+ * @param {Error} ex
682
+ * @return {?Object.<string, *>} Stack trace information.
683
+ */
684
+ function computeStackTraceFromStacktraceProp(ex) {
685
+ // Access and store the stacktrace property before doing ANYTHING
686
+ // else to it because Opera is not very good at providing it
687
+ // reliably in other circumstances.
688
+ var stacktrace = ex.stacktrace;
689
+
690
+ var testRE = / line (\d+), column (\d+) in (?:<anonymous function: ([^>]+)>|([^\)]+))\((.*)\) in (.*):\s*$/i,
691
+ lines = stacktrace.split('\n'),
692
+ stack = [],
693
+ parts;
694
+
695
+ for (var i = 0, j = lines.length; i < j; i += 2) {
696
+ if ((parts = testRE.exec(lines[i]))) {
697
+ var element = {
698
+ 'line': +parts[1],
699
+ 'column': +parts[2],
700
+ 'func': parts[3] || parts[4],
701
+ 'args': parts[5] ? parts[5].split(',') : [],
702
+ 'url': parts[6]
703
+ };
704
+
705
+ if (!element.func && element.line) {
706
+ element.func = guessFunctionName(element.url, element.line);
707
+ }
708
+ if (element.line) {
709
+ try {
710
+ element.context = gatherContext(element.url, element.line);
711
+ } catch (exc) {}
712
+ }
713
+
714
+ if (!element.context) {
715
+ element.context = [lines[i + 1]];
716
+ }
717
+
718
+ stack.push(element);
719
+ }
720
+ }
721
+
722
+ if (!stack.length) {
723
+ return null;
724
+ }
725
+
726
+ return {
727
+ 'mode': 'stacktrace',
728
+ 'name': ex.name,
729
+ 'message': ex.message,
730
+ 'url': document.location.href,
731
+ 'stack': stack,
732
+ 'useragent': navigator.userAgent
733
+ };
734
+ }
735
+
736
+ /**
737
+ * NOT TESTED.
738
+ * Computes stack trace information from an error message that includes
739
+ * the stack trace.
740
+ * Opera 9 and earlier use this method if the option to show stack
741
+ * traces is turned on in opera:config.
742
+ * @param {Error} ex
743
+ * @return {?Object.<string, *>} Stack information.
744
+ */
745
+ function computeStackTraceFromOperaMultiLineMessage(ex) {
746
+ // Opera includes a stack trace into the exception message. An example is:
747
+ //
748
+ // Statement on line 3: Undefined variable: undefinedFunc
749
+ // Backtrace:
750
+ // Line 3 of linked script file://localhost/Users/andreyvit/Projects/TraceKit/javascript-client/sample.js: In function zzz
751
+ // undefinedFunc(a);
752
+ // Line 7 of inline#1 script in file://localhost/Users/andreyvit/Projects/TraceKit/javascript-client/sample.html: In function yyy
753
+ // zzz(x, y, z);
754
+ // Line 3 of inline#1 script in file://localhost/Users/andreyvit/Projects/TraceKit/javascript-client/sample.html: In function xxx
755
+ // yyy(a, a, a);
756
+ // Line 1 of function script
757
+ // try { xxx('hi'); return false; } catch(ex) { TraceKit.report(ex); }
758
+ // ...
759
+
760
+ var lines = ex.message.split('\n');
761
+ if (lines.length < 4) {
762
+ return null;
763
+ }
764
+
765
+ var lineRE1 = /^\s*Line (\d+) of linked script ((?:file|http|https)\S+)(?:: in function (\S+))?\s*$/i,
766
+ lineRE2 = /^\s*Line (\d+) of inline#(\d+) script in ((?:file|http|https)\S+)(?:: in function (\S+))?\s*$/i,
767
+ lineRE3 = /^\s*Line (\d+) of function script\s*$/i,
768
+ stack = [],
769
+ scripts = document.getElementsByTagName('script'),
770
+ inlineScriptBlocks = [],
771
+ parts,
772
+ i,
773
+ len,
774
+ source;
775
+
776
+ for (i in scripts) {
777
+ if (_has(scripts, i) && !scripts[i].src) {
778
+ inlineScriptBlocks.push(scripts[i]);
779
+ }
780
+ }
781
+
782
+ for (i = 2, len = lines.length; i < len; i += 2) {
783
+ var item = null;
784
+ if ((parts = lineRE1.exec(lines[i]))) {
785
+ item = {
786
+ 'url': parts[2],
787
+ 'func': parts[3],
788
+ 'line': +parts[1]
789
+ };
790
+ } else if ((parts = lineRE2.exec(lines[i]))) {
791
+ item = {
792
+ 'url': parts[3],
793
+ 'func': parts[4]
794
+ };
795
+ var relativeLine = (+parts[1]); // relative to the start of the <SCRIPT> block
796
+ var script = inlineScriptBlocks[parts[2] - 1];
797
+ if (script) {
798
+ source = getSource(item.url);
799
+ if (source) {
800
+ source = source.join('\n');
801
+ var pos = source.indexOf(script.innerText);
802
+ if (pos >= 0) {
803
+ item.line = relativeLine + source.substring(0, pos).split('\n').length;
804
+ }
805
+ }
806
+ }
807
+ } else if ((parts = lineRE3.exec(lines[i]))) {
808
+ var url = window.location.href.replace(/#.*$/, ''),
809
+ line = parts[1];
810
+ var re = new RegExp(escapeCodeAsRegExpForMatchingInsideHTML(lines[i + 1]));
811
+ source = findSourceInUrls(re, [url]);
812
+ item = {
813
+ 'url': url,
814
+ 'line': source ? source.line : line,
815
+ 'func': ''
816
+ };
817
+ }
818
+
819
+ if (item) {
820
+ if (!item.func) {
821
+ item.func = guessFunctionName(item.url, item.line);
822
+ }
823
+ var context = gatherContext(item.url, item.line);
824
+ var midline = (context ? context[Math.floor(context.length / 2)] : null);
825
+ if (context && midline.replace(/^\s*/, '') === lines[i + 1].replace(/^\s*/, '')) {
826
+ item.context = context;
827
+ } else {
828
+ // if (context) alert("Context mismatch. Correct midline:\n" + lines[i+1] + "\n\nMidline:\n" + midline + "\n\nContext:\n" + context.join("\n") + "\n\nURL:\n" + item.url);
829
+ item.context = [lines[i + 1]];
830
+ }
831
+ stack.push(item);
832
+ }
833
+ }
834
+ if (!stack.length) {
835
+ return null; // could not parse multiline exception message as Opera stack trace
836
+ }
837
+
838
+ return {
839
+ 'mode': 'multiline',
840
+ 'name': ex.name,
841
+ 'message': lines[0],
842
+ 'url': document.location.href,
843
+ 'stack': stack,
844
+ 'useragent': navigator.userAgent
845
+ };
846
+ }
847
+
848
+ /**
849
+ * Adds information about the first frame to incomplete stack traces.
850
+ * Safari and IE require this to get complete data on the first frame.
851
+ * @param {Object.<string, *>} stackInfo Stack trace information from
852
+ * one of the compute* methods.
853
+ * @param {string} url The URL of the script that caused an error.
854
+ * @param {(number|string)} lineNo The line number of the script that
855
+ * caused an error.
856
+ * @param {string=} message The error generated by the browser, which
857
+ * hopefully contains the name of the object that caused the error.
858
+ * @return {boolean} Whether or not the stack information was
859
+ * augmented.
860
+ */
861
+ function augmentStackTraceWithInitialElement(stackInfo, url, lineNo, message) {
862
+ var initial = {
863
+ 'url': url,
864
+ 'line': lineNo
865
+ };
866
+
867
+ if (initial.url && initial.line) {
868
+ stackInfo.incomplete = false;
869
+
870
+ if (!initial.func) {
871
+ initial.func = guessFunctionName(initial.url, initial.line);
872
+ }
873
+
874
+ if (!initial.context) {
875
+ initial.context = gatherContext(initial.url, initial.line);
876
+ }
877
+
878
+ var reference = / '([^']+)' /.exec(message);
879
+ if (reference) {
880
+ initial.column = findSourceInLine(reference[1], initial.url, initial.line);
881
+ }
882
+
883
+ if (stackInfo.stack.length > 0) {
884
+ if (stackInfo.stack[0].url === initial.url) {
885
+ if (stackInfo.stack[0].line === initial.line) {
886
+ return false; // already in stack trace
887
+ } else if (!stackInfo.stack[0].line && stackInfo.stack[0].func === initial.func) {
888
+ stackInfo.stack[0].line = initial.line;
889
+ stackInfo.stack[0].context = initial.context;
890
+ return false;
891
+ }
892
+ }
893
+ }
894
+
895
+ stackInfo.stack.unshift(initial);
896
+ stackInfo.partial = true;
897
+ return true;
898
+ } else {
899
+ stackInfo.incomplete = true;
900
+ }
901
+
902
+ return false;
903
+ }
904
+
905
+ /**
906
+ * Computes stack trace information by walking the arguments.caller
907
+ * chain at the time the exception occurred. This will cause earlier
908
+ * frames to be missed but is the only way to get any stack trace in
909
+ * Safari and IE. The top frame is restored by
910
+ * {@link augmentStackTraceWithInitialElement}.
911
+ * @param {Error} ex
912
+ * @return {?Object.<string, *>} Stack trace information.
913
+ */
914
+ function computeStackTraceByWalkingCallerChain(ex, depth) {
915
+ var functionName = /function\s+([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)?\s*\(/i,
916
+ stack = [],
917
+ funcs = {},
918
+ recursion = false,
919
+ parts,
920
+ item,
921
+ source;
922
+
923
+ for (var curr = computeStackTraceByWalkingCallerChain.caller; curr && !recursion; curr = curr.caller) {
924
+ if (curr === computeStackTrace || curr === TraceKit.report) {
925
+ // console.log('skipping internal function');
926
+ continue;
927
+ }
928
+
929
+ item = {
930
+ 'url': null,
931
+ 'func': UNKNOWN_FUNCTION,
932
+ 'line': null,
933
+ 'column': null
934
+ };
935
+
936
+ if (curr.name) {
937
+ item.func = curr.name;
938
+ } else if ((parts = functionName.exec(curr.toString()))) {
939
+ item.func = parts[1];
940
+ }
941
+
942
+ if ((source = findSourceByFunctionBody(curr))) {
943
+ item.url = source.url;
944
+ item.line = source.line;
945
+
946
+ if (item.func === UNKNOWN_FUNCTION) {
947
+ item.func = guessFunctionName(item.url, item.line);
948
+ }
949
+
950
+ var reference = / '([^']+)' /.exec(ex.message || ex.description);
951
+ if (reference) {
952
+ item.column = findSourceInLine(reference[1], source.url, source.line);
953
+ }
954
+ }
955
+
956
+ if (funcs['' + curr]) {
957
+ recursion = true;
958
+ }else{
959
+ funcs['' + curr] = true;
960
+ }
961
+
962
+ stack.push(item);
963
+ }
964
+
965
+ if (depth) {
966
+ // console.log('depth is ' + depth);
967
+ // console.log('stack is ' + stack.length);
968
+ stack.splice(0, depth);
969
+ }
970
+
971
+ var result = {
972
+ 'mode': 'callers',
973
+ 'name': ex.name,
974
+ 'message': ex.message,
975
+ 'url': document.location.href,
976
+ 'stack': stack,
977
+ 'useragent': navigator.userAgent
978
+ };
979
+ augmentStackTraceWithInitialElement(result, ex.sourceURL || ex.fileName, ex.line || ex.lineNumber, ex.message || ex.description);
980
+ return result;
981
+ }
982
+
983
+ /**
984
+ * Computes a stack trace for an exception.
985
+ * @param {Error} ex
986
+ * @param {(string|number)=} depth
987
+ */
988
+ function computeStackTrace(ex, depth) {
989
+ var stack = null;
990
+ depth = (depth == null ? 0 : +depth);
991
+
992
+ try {
993
+ // This must be tried first because Opera 10 *destroys*
994
+ // its stacktrace property if you try to access the stack
995
+ // property first!!
996
+ stack = computeStackTraceFromStacktraceProp(ex);
997
+ if (stack) {
998
+ return stack;
999
+ }
1000
+ } catch (e) {
1001
+ if (debug) {
1002
+ throw e;
1003
+ }
1004
+ }
1005
+
1006
+ try {
1007
+ stack = computeStackTraceFromStackProp(ex);
1008
+ if (stack) {
1009
+ return stack;
1010
+ }
1011
+ } catch (e) {
1012
+ if (debug) {
1013
+ throw e;
1014
+ }
1015
+ }
1016
+
1017
+ try {
1018
+ stack = computeStackTraceFromOperaMultiLineMessage(ex);
1019
+ if (stack) {
1020
+ return stack;
1021
+ }
1022
+ } catch (e) {
1023
+ if (debug) {
1024
+ throw e;
1025
+ }
1026
+ }
1027
+
1028
+ try {
1029
+ stack = computeStackTraceByWalkingCallerChain(ex, depth + 1);
1030
+ if (stack) {
1031
+ return stack;
1032
+ }
1033
+ } catch (e) {
1034
+ if (debug) {
1035
+ throw e;
1036
+ }
1037
+ }
1038
+
1039
+ return {
1040
+ 'mode': 'failed'
1041
+ };
1042
+ }
1043
+
1044
+ /**
1045
+ * Logs a stacktrace starting from the previous call and working down.
1046
+ * @param {(number|string)=} depth How many frames deep to trace.
1047
+ * @return {Object.<string, *>} Stack trace information.
1048
+ */
1049
+ function computeStackTraceOfCaller(depth) {
1050
+ depth = (depth == null ? 0 : +depth) + 1; // "+ 1" because "ofCaller" should drop one frame
1051
+ try {
1052
+ throw new Error();
1053
+ } catch (ex) {
1054
+ return computeStackTrace(ex, depth + 1);
1055
+ }
1056
+
1057
+ return null;
1058
+ }
1059
+
1060
+ computeStackTrace.augmentStackTraceWithInitialElement = augmentStackTraceWithInitialElement;
1061
+ computeStackTrace.guessFunctionName = guessFunctionName;
1062
+ computeStackTrace.gatherContext = gatherContext;
1063
+ computeStackTrace.ofCaller = computeStackTraceOfCaller;
1064
+
1065
+ return computeStackTrace;
1066
+ }());
1067
+
1068
+ /**
1069
+ * Extends support for global error handling for asynchronous browser
1070
+ * functions. Adopted from Closure Library's errorhandler.js
1071
+ */
1072
+ (function extendToAsynchronousCallbacks() {
1073
+ var _helper = function _helper(fnName) {
1074
+ var originalFn = window[fnName];
1075
+ window[fnName] = function traceKitAsyncExtension() {
1076
+ // Make a copy of the arguments
1077
+ var args = _slice.call(arguments);
1078
+ var originalCallback = args[0];
1079
+ if (typeof (originalCallback) === 'function') {
1080
+ args[0] = TraceKit.wrap(originalCallback);
1081
+ }
1082
+ // IE < 9 doesn't support .call/.apply on setInterval/setTimeout, but it
1083
+ // also only supports 2 argument and doesn't care what "this" is, so we
1084
+ // can just call the original function directly.
1085
+ if (originalFn.apply) {
1086
+ return originalFn.apply(this, args);
1087
+ } else {
1088
+ return originalFn(args[0], args[1]);
1089
+ }
1090
+ };
1091
+ };
1092
+
1093
+ _helper('setTimeout');
1094
+ _helper('setInterval');
1095
+ }());
1096
+
1097
+ /**
1098
+ * Extended support for backtraces and global error handling for most
1099
+ * asynchronous jQuery functions.
1100
+ */
1101
+ (function traceKitAsyncForjQuery($) {
1102
+
1103
+ // quit if jQuery isn't on the page
1104
+ if (!$) {
1105
+ return;
1106
+ }
1107
+
1108
+ var _oldEventAdd = $.event.add;
1109
+ $.event.add = function traceKitEventAdd(elem, types, handler, data, selector) {
1110
+ var _handler;
1111
+
1112
+ if (handler.handler) {
1113
+ _handler = handler.handler;
1114
+ handler.handler = TraceKit.wrap(handler.handler);
1115
+ } else {
1116
+ _handler = handler;
1117
+ handler = TraceKit.wrap(handler);
1118
+ }
1119
+
1120
+ // If the handler we are attaching doesn’t have the same guid as
1121
+ // the original, it will never be removed when someone tries to
1122
+ // unbind the original function later. Technically as a result of
1123
+ // this our guids are no longer globally unique, but whatever, that
1124
+ // never hurt anybody RIGHT?!
1125
+ if (_handler.guid) {
1126
+ handler.guid = _handler.guid;
1127
+ } else {
1128
+ handler.guid = _handler.guid = $.guid++;
1129
+ }
1130
+
1131
+ return _oldEventAdd.call(this, elem, types, handler, data, selector);
1132
+ };
1133
+
1134
+ var _oldReady = $.fn.ready;
1135
+ $.fn.ready = function traceKitjQueryReadyWrapper(fn) {
1136
+ return _oldReady.call(this, TraceKit.wrap(fn));
1137
+ };
1138
+
1139
+ var _oldAjax = $.ajax;
1140
+ $.ajax = function traceKitAjaxWrapper(s) {
1141
+ var keys = ['complete', 'error', 'success'], key;
1142
+ while(key = keys.pop()) {
1143
+ if ($.isFunction(s[key])) {
1144
+ s[key] = TraceKit.wrap(s[key]);
1145
+ }
1146
+ }
1147
+
1148
+ try {
1149
+ return _oldAjax.call(this, s);
1150
+ } catch (e) {
1151
+ TraceKit.report(e);
1152
+ throw e;
1153
+ }
1154
+ };
1155
+
1156
+ }(window.jQuery));
1157
+
1158
+ //Default options:
1159
+ if (!TraceKit.remoteFetching) {
1160
+ TraceKit.remoteFetching = true;
1161
+ }
1162
+ if (!TraceKit.collectWindowErrors) {
1163
+ TraceKit.collectWindowErrors = true;
1164
+ }
1165
+ if (!TraceKit.linesOfContext || TraceKit.linesOfContext < 1) {
1166
+ // 5 lines before, the offending line, 5 lines after
1167
+ TraceKit.linesOfContext = 11;
1168
+ }
1169
+
1170
+
1171
+
1172
+ // Export to global object
1173
+ window.TraceKit = TraceKit;
1174
+
1175
+ }(window));
1176
+ ;(function(window, undefined){
1177
+ 'use strict';
1178
+
1179
+ // First, check for JSON support
1180
+ // If there is no JSON, we no-op the core features of Raven
1181
+ // since JSON is required to encode the payload
1182
+ var _Raven = window.Raven,
1183
+ hasJSON = !isUndefined(window.JSON),
1184
+ globalServer,
1185
+ globalUser,
1186
+ globalKey,
1187
+ globalProject,
1188
+ globalOptions = {
1189
+ logger: 'javascript',
1190
+ ignoreErrors: [],
1191
+ ignoreUrls: []
1192
+ };
1193
+
1194
+ var TK = TraceKit.noConflict();
1195
+
1196
+ // Disable Tracekit's remote fetching by default
1197
+ TK.remoteFetching = false;
1198
+
1199
+ /*
1200
+ * The core Raven singleton
1201
+ *
1202
+ * @this {Raven}
1203
+ */
1204
+ var Raven = {
1205
+ VERSION: '1.0.6',
1206
+
1207
+ /*
1208
+ * Allow multiple versions of Raven to be installed.
1209
+ * Strip Raven from the global context and returns the instance.
1210
+ *
1211
+ * @return {Raven}
1212
+ */
1213
+ noConflict: function() {
1214
+ window.Raven = _Raven;
1215
+ return Raven;
1216
+ },
1217
+
1218
+ /*
1219
+ * Configure Raven with a DSN and extra options
1220
+ *
1221
+ * @param {string} dsn The public Sentry DSN
1222
+ * @param {object} options Optional set of of global options [optional]
1223
+ * @return {Raven}
1224
+ */
1225
+ config: function(dsn, options) {
1226
+ var uri = parseUri(dsn),
1227
+ lastSlash = uri.path.lastIndexOf('/'),
1228
+ path = uri.path.substr(1, lastSlash);
1229
+
1230
+ if (options && options.ignoreErrors && window.console && console.warn) {
1231
+ console.warn('DeprecationWarning: `ignoreErrors` is going to be removed soon.');
1232
+ }
1233
+
1234
+ // merge in options
1235
+ if (options) {
1236
+ each(options, function(key, value){
1237
+ globalOptions[key] = value;
1238
+ });
1239
+ }
1240
+
1241
+ // "Script error." is hard coded into browsers for errors that it can't read.
1242
+ // this is the result of a script being pulled in from an external domain and CORS.
1243
+ globalOptions.ignoreErrors.push('Script error.');
1244
+
1245
+ globalKey = uri.user;
1246
+ globalProject = ~~uri.path.substr(lastSlash + 1);
1247
+
1248
+ // assemble the endpoint from the uri pieces
1249
+ globalServer = '//' + uri.host +
1250
+ (uri.port ? ':' + uri.port : '') +
1251
+ '/' + path + 'api/' + globalProject + '/store/';
1252
+
1253
+ if (uri.protocol) {
1254
+ globalServer = uri.protocol + ':' + globalServer;
1255
+ }
1256
+
1257
+ if (globalOptions.fetchContext) {
1258
+ TK.remoteFetching = true;
1259
+ }
1260
+
1261
+ // return for chaining
1262
+ return Raven;
1263
+ },
1264
+
1265
+ /*
1266
+ * Installs a global window.onerror error handler
1267
+ * to capture and report uncaught exceptions.
1268
+ * At this point, install() is required to be called due
1269
+ * to the way TraceKit is set up.
1270
+ *
1271
+ * @return {Raven}
1272
+ */
1273
+ install: function() {
1274
+ if (!isSetup()) return;
1275
+
1276
+ TK.report.subscribe(handleStackInfo);
1277
+
1278
+ return Raven;
1279
+ },
1280
+
1281
+ /*
1282
+ * Wrap code within a context so Raven can capture errors
1283
+ * reliably across domains that is executed immediately.
1284
+ *
1285
+ * @param {object} options A specific set of options for this context [optional]
1286
+ * @param {function} func The callback to be immediately executed within the context
1287
+ * @param {array} args An array of arguments to be called with the callback [optional]
1288
+ */
1289
+ context: function(options, func, args) {
1290
+ if (isFunction(options)) {
1291
+ args = func || [];
1292
+ func = options;
1293
+ options = undefined;
1294
+ }
1295
+
1296
+ Raven.wrap(options, func).apply(this, args);
1297
+ },
1298
+
1299
+ /*
1300
+ * Wrap code within a context and returns back a new function to be executed
1301
+ *
1302
+ * @param {object} options A specific set of options for this context [optional]
1303
+ * @param {function} func The function to be wrapped in a new context
1304
+ * @return {function} The newly wrapped functions with a context
1305
+ */
1306
+ wrap: function(options, func) {
1307
+ // options is optional
1308
+ if (isFunction(options)) {
1309
+ func = options;
1310
+ options = undefined;
1311
+ }
1312
+
1313
+ return function() {
1314
+ try {
1315
+ func.apply(this, arguments);
1316
+ } catch(e) {
1317
+ Raven.captureException(e, options);
1318
+ throw e;
1319
+ }
1320
+ };
1321
+ },
1322
+
1323
+ /*
1324
+ * Uninstalls the global error handler.
1325
+ *
1326
+ * @return {Raven}
1327
+ */
1328
+ uninstall: function() {
1329
+ TK.report.unsubscribe(handleStackInfo);
1330
+
1331
+ return Raven;
1332
+ },
1333
+
1334
+ /*
1335
+ * Manually capture an exception and send it over to Sentry
1336
+ *
1337
+ * @param {error} ex An exception to be logged
1338
+ * @param {object} options A specific set of options for this error [optional]
1339
+ * @return {Raven}
1340
+ */
1341
+ captureException: function(ex, options) {
1342
+ // If a string is passed through, recall as a message
1343
+ if (typeof ex === 'string') {
1344
+ return Raven.captureMessage(ex, options);
1345
+ }
1346
+
1347
+ // TraceKit.report will re-raise any exception passed to it,
1348
+ // which means you have to wrap it in try/catch. Instead, we
1349
+ // can wrap it here and only re-raise if TraceKit.report
1350
+ // raises an exception different from the one we asked to
1351
+ // report on.
1352
+ try {
1353
+ TK.report(ex, options);
1354
+ } catch(ex1) {
1355
+ if(ex !== ex1) {
1356
+ throw ex1;
1357
+ }
1358
+ }
1359
+
1360
+ return Raven;
1361
+ },
1362
+
1363
+ /*
1364
+ * Manually send a message to Sentry
1365
+ *
1366
+ * @param {string} msg A plain message to be captured in Sentry
1367
+ * @param {object} options A specific set of options for this message [optional]
1368
+ * @return {Raven}
1369
+ */
1370
+ captureMessage: function(msg, options) {
1371
+ // Fire away!
1372
+ send(
1373
+ arrayMerge({
1374
+ message: msg
1375
+ }, options)
1376
+ );
1377
+
1378
+ return Raven;
1379
+ },
1380
+
1381
+ /*
1382
+ * Set/clear a user to be sent along with the payload.
1383
+ *
1384
+ * @param {object} user An object representing user data [optional]
1385
+ * @return {Raven}
1386
+ */
1387
+ setUser: function(user) {
1388
+ globalUser = user;
1389
+
1390
+ return Raven;
1391
+ }
1392
+ };
1393
+
1394
+ var uriKeys = 'source protocol authority userInfo user password host port relative path directory file query anchor'.split(' '),
1395
+ uriPattern = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
1396
+
1397
+ /**** Private functions ****/
1398
+ function parseUri(str) {
1399
+ var m = uriPattern.exec(str),
1400
+ uri = {},
1401
+ i = 14;
1402
+
1403
+ while (i--) uri[uriKeys[i]] = m[i] || '';
1404
+
1405
+ return uri;
1406
+ }
1407
+
1408
+ function isUndefined(what) {
1409
+ return typeof what === 'undefined';
1410
+ }
1411
+
1412
+ function isFunction(what) {
1413
+ return typeof what === 'function';
1414
+ }
1415
+
1416
+ function each(obj, callback) {
1417
+ var i, j;
1418
+
1419
+ if (isUndefined(obj.length)) {
1420
+ for (i in obj) {
1421
+ if (obj.hasOwnProperty(i)) {
1422
+ callback.call(null, i, obj[i]);
1423
+ }
1424
+ }
1425
+ } else {
1426
+ for (i = 0, j = obj.length; i < j; i++) {
1427
+ callback.call(null, i, obj[i]);
1428
+ }
1429
+ }
1430
+ }
1431
+
1432
+ var cachedAuth;
1433
+
1434
+ function getAuthQueryString() {
1435
+ if (cachedAuth) return cachedAuth;
1436
+
1437
+ var qs = [
1438
+ 'sentry_version=2.0',
1439
+ 'sentry_client=raven-js/' + Raven.VERSION
1440
+ ];
1441
+ if (globalKey) {
1442
+ qs.push('sentry_key=' + globalKey);
1443
+ }
1444
+
1445
+ cachedAuth = '?' + qs.join('&');
1446
+ return cachedAuth;
1447
+ }
1448
+
1449
+ function handleStackInfo(stackInfo, options) {
1450
+ var frames = [],
1451
+ i = 0,
1452
+ j, frame;
1453
+
1454
+ if (stackInfo.stack && (j = stackInfo.stack.length)) {
1455
+ for (; i < j; i++) {
1456
+ frame = normalizeFrame(stackInfo.stack[i]);
1457
+ if (frame) {
1458
+ frames.push(frame);
1459
+ }
1460
+ }
1461
+ }
1462
+
1463
+ processException(
1464
+ stackInfo.name,
1465
+ stackInfo.message,
1466
+ stackInfo.url,
1467
+ stackInfo.lineno,
1468
+ frames,
1469
+ options
1470
+ );
1471
+ }
1472
+
1473
+ function normalizeFrame(frame) {
1474
+ if (!frame.url) return;
1475
+
1476
+ // normalize the frames data
1477
+ var normalized = {
1478
+ filename: frame.url,
1479
+ lineno: frame.line,
1480
+ colno: frame.column,
1481
+ 'function': frame.func || '?'
1482
+ }, context = extractContextFromFrame(frame);
1483
+
1484
+ if (context) {
1485
+ var i = 3, keys = ['pre_context', 'context_line', 'post_context'];
1486
+ while (i--) normalized[keys[i]] = context[i];
1487
+ }
1488
+
1489
+ normalized.in_app = !/(Raven|TraceKit)\./.test(normalized['function']);
1490
+
1491
+ return normalized;
1492
+ }
1493
+
1494
+ function extractContextFromFrame(frame) {
1495
+ // immediately check if we should even attempt to parse a context
1496
+ if (!frame.context || !globalOptions.fetchContext) return;
1497
+
1498
+ var context = frame.context,
1499
+ pivot = ~~(context.length / 2),
1500
+ i = context.length, isMinified = false;
1501
+
1502
+ while (i--) {
1503
+ // We're making a guess to see if the source is minified or not.
1504
+ // To do that, we make the assumption if *any* of the lines passed
1505
+ // in are greater than 300 characters long, we bail.
1506
+ // Sentry will see that there isn't a context
1507
+ if (context[i].length > 300) {
1508
+ isMinified = true;
1509
+ break;
1510
+ }
1511
+ }
1512
+
1513
+ if (isMinified) {
1514
+ // The source is minified and we don't know which column. Fuck it.
1515
+ if (isUndefined(frame.column)) return;
1516
+
1517
+ // If the source is minified and has a frame column
1518
+ // we take a chunk of the offending line to hopefully shed some light
1519
+ return [
1520
+ [], // no pre_context
1521
+ context[pivot].substr(frame.column, 50), // grab 50 characters, starting at the offending column
1522
+ [] // no post_context
1523
+ ];
1524
+ }
1525
+
1526
+ return [
1527
+ context.slice(0, pivot), // pre_context
1528
+ context[pivot], // context_line
1529
+ context.slice(pivot + 1) // post_context
1530
+ ];
1531
+ }
1532
+
1533
+ function processException(type, message, fileurl, lineno, frames, options) {
1534
+ var stacktrace, label, i;
1535
+
1536
+ // IE8 really doesn't have Array.prototype.indexOf
1537
+ // Filter out a message that matches our ignore list
1538
+ i = globalOptions.ignoreErrors.length;
1539
+ while (i--) {
1540
+ if (message === globalOptions.ignoreErrors[i]) {
1541
+ return;
1542
+ }
1543
+ }
1544
+
1545
+ if (frames && frames.length) {
1546
+ stacktrace = {frames: frames};
1547
+ fileurl = fileurl || frames[0].filename;
1548
+ } else if (fileurl) {
1549
+ stacktrace = {
1550
+ frames: [{
1551
+ filename: fileurl,
1552
+ lineno: lineno
1553
+ }]
1554
+ };
1555
+ }
1556
+
1557
+ i = globalOptions.ignoreUrls.length;
1558
+ while (i--) {
1559
+ if (globalOptions.ignoreUrls[i].test(fileurl)) {
1560
+ return;
1561
+ }
1562
+ }
1563
+
1564
+ label = lineno ? message + ' at ' + lineno : message;
1565
+
1566
+ // Fire away!
1567
+ send(
1568
+ arrayMerge({
1569
+ 'sentry.interfaces.Exception': {
1570
+ type: type,
1571
+ value: message
1572
+ },
1573
+ 'sentry.interfaces.Stacktrace': stacktrace,
1574
+ culprit: fileurl,
1575
+ message: label
1576
+ }, options)
1577
+ );
1578
+ }
1579
+
1580
+ function arrayMerge(arr1, arr2) {
1581
+ if (!arr2) {
1582
+ return arr1;
1583
+ }
1584
+ each(arr2, function(key, value){
1585
+ arr1[key] = value;
1586
+ });
1587
+ return arr1;
1588
+ }
1589
+
1590
+ function getHttpData() {
1591
+ var http = {
1592
+ url: window.location.href,
1593
+ headers: {
1594
+ 'User-Agent': navigator.userAgent
1595
+ }
1596
+ };
1597
+
1598
+ if (window.document.referrer) {
1599
+ http.headers.Referer = window.document.referrer;
1600
+ }
1601
+
1602
+ return http;
1603
+ }
1604
+
1605
+ function send(data) {
1606
+ if (!isSetup()) return;
1607
+
1608
+ data = arrayMerge({
1609
+ project: globalProject,
1610
+ logger: globalOptions.logger,
1611
+ site: globalOptions.site,
1612
+ platform: 'javascript',
1613
+ 'sentry.interfaces.Http': getHttpData()
1614
+ }, data);
1615
+
1616
+ if (globalUser) data['sentry.interfaces.User'] = globalUser;
1617
+
1618
+ if (isFunction(globalOptions.dataCallback)) {
1619
+ data = globalOptions.dataCallback(data);
1620
+ }
1621
+
1622
+ makeRequest(data);
1623
+ }
1624
+
1625
+ function makeRequest(data) {
1626
+ new Image().src = globalServer + getAuthQueryString() + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
1627
+ }
1628
+
1629
+ function isSetup() {
1630
+ if (!hasJSON) return false; // needs JSON support
1631
+ if (!globalServer) {
1632
+ if (window.console && console.error) {
1633
+ console.error("Error: Raven has not been configured.");
1634
+ }
1635
+ return false;
1636
+ }
1637
+ return true;
1638
+ }
1639
+
1640
+ // Expose Raven to the world
1641
+ window.Raven = Raven;
1642
+
1643
+ // AMD
1644
+ if (typeof define === 'function' && define.amd) {
1645
+ define(function() { return Raven; });
1646
+ }
1647
+
1648
+ })(window);