2013-03-13 00:15:14 +01:00
|
|
|
// This must be included before the first call to $(document).ready
|
|
|
|
// in order to be able to report exceptions that occur during their
|
|
|
|
// execution.
|
|
|
|
|
2013-03-11 17:25:46 +01:00
|
|
|
var blueslip = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-06-13 00:24:50 +02:00
|
|
|
var console = (function () {
|
|
|
|
if (window.console !== undefined) {
|
|
|
|
return window.console;
|
|
|
|
}
|
|
|
|
|
|
|
|
var proxy = {};
|
|
|
|
var methods = ['log', 'info', 'warn', 'error', 'trace'];
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < methods.length; i++) {
|
|
|
|
proxy[methods[i]] = function () {};
|
|
|
|
}
|
|
|
|
return proxy;
|
|
|
|
}());
|
|
|
|
|
2013-03-11 20:58:55 +01:00
|
|
|
var reported_errors = {};
|
2013-03-27 18:12:26 +01:00
|
|
|
var last_report_attempt = {};
|
2013-03-12 21:19:49 +01:00
|
|
|
function report_error(msg, stack, opts) {
|
2013-03-11 23:34:14 +01:00
|
|
|
opts = $.extend({}, {show_ui_msg: false}, opts);
|
2013-03-12 21:19:49 +01:00
|
|
|
|
|
|
|
if (stack === undefined) {
|
2013-03-11 20:54:27 +01:00
|
|
|
stack = 'No stacktrace available';
|
|
|
|
}
|
|
|
|
|
2013-03-27 18:12:26 +01:00
|
|
|
var key = ':' + msg + stack;
|
|
|
|
if (reported_errors.hasOwnProperty(key)
|
|
|
|
|| (last_report_attempt.hasOwnProperty(key)
|
|
|
|
// Only try to report a given error once every 5 minutes
|
|
|
|
&& (Date.now() - last_report_attempt[key] <= 60 * 5 * 1000)))
|
|
|
|
{
|
2013-03-11 20:58:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-27 18:12:26 +01:00
|
|
|
last_report_attempt[key] = Date.now();
|
|
|
|
|
2013-03-13 00:15:14 +01:00
|
|
|
// TODO: If an exception gets thrown before we setup ajax calls
|
|
|
|
// to include the CSRF token, our ajax call will fail. The
|
|
|
|
// elegant thing to do in that case is to either wait until that
|
|
|
|
// setup is done or do it ourselves and then retry.
|
2013-03-11 20:54:27 +01:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/report_error',
|
|
|
|
dataType: 'json',
|
2013-04-04 21:29:14 +02:00
|
|
|
data: { message: msg,
|
|
|
|
stacktrace: stack,
|
2013-03-27 18:31:18 +01:00
|
|
|
ui_message: opts.show_ui_msg,
|
2013-04-04 21:29:14 +02:00
|
|
|
more_info: JSON.stringify(opts.more_info),
|
2013-05-01 21:49:21 +02:00
|
|
|
href: window.location.href,
|
2013-03-15 22:30:07 +01:00
|
|
|
user_agent: window.navigator.userAgent},
|
2013-03-11 23:34:14 +01:00
|
|
|
timeout: 3*1000,
|
2013-03-11 20:58:55 +01:00
|
|
|
success: function () {
|
|
|
|
reported_errors[key] = true;
|
2013-05-16 22:47:08 +02:00
|
|
|
if (opts.show_ui_msg && ui !== undefined) {
|
2013-03-12 22:37:13 +01:00
|
|
|
// There are a few races here (and below in the error
|
|
|
|
// callback):
|
|
|
|
// 1) The ui module or something it requires might
|
|
|
|
// not have been compiled or initialized yet.
|
|
|
|
// 2) The DOM might not be ready yet and so fetching
|
|
|
|
// the #home-error div might fail.
|
|
|
|
|
2013-05-16 22:47:08 +02:00
|
|
|
// For (1) we just don't show the message if the ui
|
|
|
|
// hasn't been loaded yet. The user will probably
|
|
|
|
// get another error once it does. We can't solve
|
|
|
|
// (2) by using $(document).ready() because the
|
2013-03-12 22:37:13 +01:00
|
|
|
// callback never gets called (I think what's going
|
|
|
|
// on here is if the exception was raised by a
|
|
|
|
// function that was called as a result of the DOM
|
|
|
|
// becoming ready then the internal state of jQuery
|
|
|
|
// gets messed up and our callback never gets
|
|
|
|
// invoked). In any case, it will pretty clear that
|
|
|
|
// something is wrong with the page and the user will
|
|
|
|
// probably try to reload anyway.
|
2013-03-11 23:34:14 +01:00
|
|
|
ui.report_message("Oops. It seems something has gone wrong. " +
|
2013-03-12 22:37:13 +01:00
|
|
|
"The error has been reported to the fine " +
|
2013-03-11 23:34:14 +01:00
|
|
|
"folks at Humbug, but, in the mean time, " +
|
|
|
|
"please try reloading the page.",
|
|
|
|
$("#home-error"), "alert-error");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function () {
|
2013-05-16 22:47:08 +02:00
|
|
|
if (opts.show_ui_msg && ui !== undefined) {
|
2013-03-11 23:34:14 +01:00
|
|
|
ui.report_message("Oops. It seems something has gone wrong. " +
|
|
|
|
"Please try reloading the page.",
|
|
|
|
$("#home-error"), "alert-error");
|
|
|
|
}
|
2013-03-11 20:58:55 +01:00
|
|
|
}
|
2013-03-11 20:54:27 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-04-04 21:29:14 +02:00
|
|
|
function BlueslipError(msg, more_info) {
|
2013-04-22 22:04:51 +02:00
|
|
|
// One can't subclass Error normally so we have to play games
|
|
|
|
// with setting __proto__
|
|
|
|
var self = new Error(msg);
|
|
|
|
self.name = "BlueslipError";
|
|
|
|
|
|
|
|
// Indirect access to __proto__ keeps jslint quiet
|
|
|
|
var proto = '__proto__';
|
|
|
|
self[proto] = BlueslipError.prototype;
|
|
|
|
|
2013-04-04 21:29:14 +02:00
|
|
|
if (more_info !== undefined) {
|
|
|
|
self.more_info = more_info;
|
|
|
|
}
|
|
|
|
return self;
|
2013-03-12 22:24:45 +01:00
|
|
|
}
|
|
|
|
|
2013-04-22 22:04:51 +02:00
|
|
|
BlueslipError.prototype = Object.create(Error.prototype);
|
2013-03-12 22:24:45 +01:00
|
|
|
|
2013-04-03 18:53:38 +02:00
|
|
|
exports.wrap_function = function blueslip_wrap_function(func) {
|
|
|
|
if (func.blueslip_wrapper !== undefined) {
|
|
|
|
func.blueslip_wrapper_refcnt++;
|
|
|
|
return func.blueslip_wrapper;
|
|
|
|
}
|
|
|
|
var new_func = function blueslip_wrapper() {
|
|
|
|
if (page_params.debug_mode) {
|
|
|
|
return func.apply(this, arguments);
|
2013-04-04 21:18:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return func.apply(this, arguments);
|
|
|
|
} catch (ex) {
|
2013-04-04 21:19:48 +02:00
|
|
|
// Treat exceptions like a call to fatal() if they
|
|
|
|
// weren't generated from fatal()
|
|
|
|
if (ex instanceof BlueslipError) {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
2013-04-04 21:18:54 +02:00
|
|
|
var message = ex.message;
|
|
|
|
if (ex.hasOwnProperty('fileName')) {
|
|
|
|
message += " at " + ex.fileName;
|
|
|
|
if (ex.hasOwnProperty('lineNumber')) {
|
|
|
|
message += ":" + ex.lineNumber;
|
2013-03-12 22:37:13 +01:00
|
|
|
}
|
|
|
|
}
|
2013-06-10 23:47:22 +02:00
|
|
|
report_error(message, ex.stack);
|
2013-04-04 21:18:54 +02:00
|
|
|
throw ex;
|
2013-04-03 18:53:38 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
func.blueslip_wrapper = new_func;
|
|
|
|
func.blueslip_wrapper_refcnt = 1;
|
|
|
|
return new_func;
|
|
|
|
};
|
2013-03-12 22:37:13 +01:00
|
|
|
|
2013-04-03 18:53:38 +02:00
|
|
|
// Catch all exceptions from jQuery event handlers, $(document).ready
|
|
|
|
// functions, and ajax success/failure continuations and funnel them
|
|
|
|
// through blueslip.
|
2013-07-05 17:43:56 +02:00
|
|
|
(function () {
|
2013-03-14 19:16:03 +01:00
|
|
|
// This reference counting scheme can't break all the circular
|
|
|
|
// references we create because users can remove jQuery event
|
|
|
|
// handlers without referencing the particular handler they want
|
|
|
|
// to remove. We just hope this memory leak won't be too bad.
|
|
|
|
function dec_wrapper_refcnt(func) {
|
|
|
|
if (func.blueslip_wrapper_refcnt !== undefined) {
|
|
|
|
func.blueslip_wrapper_refcnt--;
|
|
|
|
if (func.blueslip_wrapper_refcnt === 0) {
|
|
|
|
delete func.blueslip_wrapper;
|
|
|
|
delete func.blueslip_wrapper_refcnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 23:27:39 +01:00
|
|
|
$.ajaxPrefilter(function (options) {
|
|
|
|
$.each(['success', 'error', 'complete'], function (idx, cb_name) {
|
|
|
|
if (options[cb_name] !== undefined) {
|
2013-04-03 18:53:38 +02:00
|
|
|
options[cb_name] = exports.wrap_function(options[cb_name]);
|
2013-03-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-03-12 22:37:13 +01:00
|
|
|
if (document.addEventListener) {
|
|
|
|
var orig_on = $.fn.on;
|
2013-03-14 18:58:16 +01:00
|
|
|
var orig_off = $.fn.off;
|
2013-03-12 22:37:13 +01:00
|
|
|
var orig_ready = $.fn.ready;
|
|
|
|
|
|
|
|
$.fn.on = function blueslip_jquery_on_wrapper(types, selector, data, fn, one) {
|
|
|
|
if (typeof types === 'object') {
|
2013-03-14 18:56:39 +01:00
|
|
|
// ( types-Object, selector, data)
|
|
|
|
// We'll get called again from the recursive call in the original
|
|
|
|
// $.fn.on
|
|
|
|
return orig_on.call(this, types, selector, data, fn, one);
|
2013-03-12 22:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only one handler, but we have to figure out which
|
|
|
|
// argument it is. The argument munging is taken from
|
|
|
|
// jQuery itself, so we tell jslint to ignore the style
|
|
|
|
// issues that the jQuery code would raise. It sucks
|
2013-03-15 17:30:22 +01:00
|
|
|
// that we have to replicate the code :(
|
2013-03-12 22:37:13 +01:00
|
|
|
/*jslint eqeq: true */
|
|
|
|
if ( data == null && fn == null ) {
|
|
|
|
// ( types, fn )
|
|
|
|
fn = selector;
|
|
|
|
data = selector = undefined;
|
|
|
|
} else if ( fn == null ) {
|
|
|
|
if ( typeof selector === "string" ) {
|
|
|
|
// ( types, selector, fn )
|
|
|
|
fn = data;
|
|
|
|
data = undefined;
|
|
|
|
} else {
|
|
|
|
// ( types, data, fn )
|
|
|
|
fn = data;
|
|
|
|
data = selector;
|
|
|
|
selector = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( fn === false ) {
|
|
|
|
fn = function () { return false; };
|
|
|
|
} else if ( !fn ) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
/*jslint eqeq: false */
|
|
|
|
|
2013-04-03 18:53:38 +02:00
|
|
|
return orig_on.call(this, types, selector, data, exports.wrap_function(fn), one);
|
2013-03-12 22:37:13 +01:00
|
|
|
};
|
|
|
|
|
2013-03-14 18:58:16 +01:00
|
|
|
$.fn.off = function (types, selector, fn) {
|
|
|
|
if (types && types.preventDefault && types.handleObj) {
|
|
|
|
// (event)
|
|
|
|
// We'll get called again through the recursive call in the original
|
|
|
|
// $.fn.off
|
|
|
|
return orig_off.call(this, types, selector, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof types === "object" ) {
|
|
|
|
// ( types-object [, selector] )
|
|
|
|
// We'll get called again through the recursive call in the original
|
|
|
|
// $.fn.off
|
|
|
|
return orig_off.call(this, types, selector, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only one handler, but we have to figure out which
|
|
|
|
// argument it is. The argument munging is taken from
|
|
|
|
// jQuery, itself.
|
|
|
|
if ( selector === false || typeof selector === "function" ) {
|
|
|
|
// ( types [, fn] )
|
|
|
|
fn = selector;
|
|
|
|
selector = undefined;
|
|
|
|
}
|
|
|
|
if ( fn === false ) {
|
|
|
|
fn = function () { return false; };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fn) {
|
|
|
|
var wrapper = fn.blueslip_wrapper;
|
|
|
|
if (wrapper !== undefined) {
|
2013-03-14 19:16:03 +01:00
|
|
|
dec_wrapper_refcnt(fn);
|
2013-03-14 18:58:16 +01:00
|
|
|
fn = wrapper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return orig_off.call(this, types, selector, fn);
|
|
|
|
};
|
|
|
|
|
2013-03-12 22:37:13 +01:00
|
|
|
$.fn.ready = function blueslip_jquery_ready_wrapper(func) {
|
2013-04-03 18:53:38 +02:00
|
|
|
return orig_ready.call(this, exports.wrap_function(func));
|
2013-03-12 22:37:13 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
2013-04-04 21:29:14 +02:00
|
|
|
exports.log = function blueslip_log (msg, more_info) {
|
2013-03-11 17:25:46 +01:00
|
|
|
console.log(msg);
|
2013-04-04 21:29:14 +02:00
|
|
|
if (more_info !== undefined) {
|
|
|
|
console.log("Additional information: ", more_info);
|
|
|
|
}
|
2013-03-11 17:25:46 +01:00
|
|
|
};
|
|
|
|
|
2013-04-04 21:29:14 +02:00
|
|
|
exports.info = function blueslip_info (msg, more_info) {
|
2013-03-11 17:25:46 +01:00
|
|
|
console.info(msg);
|
2013-04-04 21:29:14 +02:00
|
|
|
if (more_info !== undefined) {
|
|
|
|
console.info("Additional information: ", more_info);
|
|
|
|
}
|
2013-03-11 17:25:46 +01:00
|
|
|
};
|
|
|
|
|
2013-04-04 21:29:14 +02:00
|
|
|
exports.warn = function blueslip_warn (msg, more_info) {
|
2013-03-11 17:25:46 +01:00
|
|
|
console.warn(msg);
|
2013-03-25 23:26:14 +01:00
|
|
|
if (page_params.debug_mode) {
|
2013-03-11 17:25:46 +01:00
|
|
|
console.trace();
|
|
|
|
}
|
2013-04-04 21:29:14 +02:00
|
|
|
if (more_info !== undefined) {
|
|
|
|
console.warn("Additional information: ", more_info);
|
|
|
|
}
|
2013-03-11 17:25:46 +01:00
|
|
|
};
|
|
|
|
|
2013-04-04 21:29:14 +02:00
|
|
|
exports.error = function blueslip_error (msg, more_info) {
|
2013-03-25 23:26:14 +01:00
|
|
|
if (page_params.debug_mode) {
|
2013-04-04 21:29:14 +02:00
|
|
|
throw new BlueslipError(msg, more_info);
|
2013-03-11 17:25:46 +01:00
|
|
|
} else {
|
|
|
|
console.error(msg);
|
2013-04-04 21:29:14 +02:00
|
|
|
if (more_info !== undefined) {
|
|
|
|
console.error("Additional information: ", more_info);
|
|
|
|
}
|
|
|
|
report_error(msg, Error().stack, {more_info: more_info});
|
2013-03-11 17:25:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-04 21:29:14 +02:00
|
|
|
exports.fatal = function blueslip_fatal (msg, more_info) {
|
2013-03-25 23:26:14 +01:00
|
|
|
if (! page_params.debug_mode) {
|
2013-06-10 23:47:22 +02:00
|
|
|
report_error(msg, Error().stack, {more_info: more_info});
|
2013-03-11 20:54:27 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 21:29:14 +02:00
|
|
|
throw new BlueslipError(msg, more_info);
|
2013-03-11 17:25:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|