2017-11-16 19:51:44 +01:00
|
|
|
// System documented in https://zulip.readthedocs.io/en/latest/subsystems/logging.html
|
2017-09-24 18:43:30 +02:00
|
|
|
|
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-08-15 18:54:17 +02:00
|
|
|
if (Error.stackTraceLimit !== undefined) {
|
|
|
|
Error.stackTraceLimit = 100000;
|
|
|
|
}
|
|
|
|
|
2013-10-22 21:11:13 +02:00
|
|
|
function Logger() {
|
|
|
|
this._memory_log = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.prototype = (function () {
|
2013-11-14 19:54:51 +01:00
|
|
|
function pad(num, width) {
|
|
|
|
var ret = num.toString();
|
|
|
|
while (ret.length < width) {
|
|
|
|
ret = "0" + ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-22 21:11:13 +02:00
|
|
|
function make_logger_func(name) {
|
|
|
|
return function Logger_func() {
|
|
|
|
var now = new Date();
|
|
|
|
var date_str =
|
|
|
|
now.getUTCFullYear() + '-' +
|
2013-11-14 19:54:51 +01:00
|
|
|
pad(now.getUTCMonth() + 1, 2) + '-' +
|
|
|
|
pad(now.getUTCDate(), 2) + ' ' +
|
|
|
|
pad(now.getUTCHours(), 2) + ':' +
|
|
|
|
pad(now.getUTCMinutes(), 2) + ':' +
|
|
|
|
pad(now.getUTCSeconds(), 2) + '.' +
|
|
|
|
pad(now.getUTCMilliseconds(), 3) + ' UTC';
|
2013-10-22 21:11:13 +02:00
|
|
|
|
|
|
|
var str_args = _.map(arguments, function (x) {
|
|
|
|
if (typeof(x) === 'object') {
|
|
|
|
return JSON.stringify(x);
|
|
|
|
} else {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var log_entry = date_str + " " + name.toUpperCase() +
|
|
|
|
': ' + str_args.join("");
|
|
|
|
this._memory_log.push(log_entry);
|
2013-10-22 21:19:59 +02:00
|
|
|
|
|
|
|
// Don't let the log grow without bound
|
|
|
|
if (this._memory_log.length > 1000) {
|
|
|
|
this._memory_log.shift();
|
|
|
|
}
|
|
|
|
|
2013-11-07 19:59:49 +01:00
|
|
|
if (console[name] !== undefined) {
|
2013-10-30 15:16:36 +01:00
|
|
|
return console[name].apply(console, arguments);
|
|
|
|
}
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2013-10-22 21:11:13 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var proto = {
|
|
|
|
get_log: function Logger_get_log() {
|
|
|
|
return this._memory_log;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-30 15:16:36 +01:00
|
|
|
var methods = ['debug', 'log', 'info', 'warn', 'error'];
|
2013-10-22 21:11:13 +02:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < methods.length; i++) {
|
|
|
|
proto[methods[i]] = make_logger_func(methods[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}());
|
|
|
|
|
|
|
|
var logger = new Logger();
|
|
|
|
|
|
|
|
exports.get_log = function blueslip_get_log() {
|
|
|
|
return logger.get_log();
|
|
|
|
};
|
|
|
|
|
2018-05-22 01:50:25 +02:00
|
|
|
// Format error stacks using the ErrorStackParser
|
|
|
|
// external library
|
|
|
|
function getErrorStack(stack) {
|
|
|
|
var ex = new Error();
|
|
|
|
ex.stack = stack;
|
|
|
|
return ErrorStackParser
|
|
|
|
.parse(ex)
|
|
|
|
.map(function (stackFrame) {
|
|
|
|
return stackFrame.lineNumber
|
|
|
|
+ ': ' + stackFrame.fileName
|
|
|
|
+ ' | ' + stackFrame.functionName;
|
|
|
|
}).join('\n');
|
|
|
|
}
|
|
|
|
|
2013-03-11 20:58:55 +01:00
|
|
|
var reported_errors = {};
|
2013-03-27 18:12:26 +01:00
|
|
|
var last_report_attempt = {};
|
2018-05-22 01:50:25 +02:00
|
|
|
|
2013-03-12 21:19:49 +01:00
|
|
|
function report_error(msg, stack, opts) {
|
2013-07-30 05:11:50 +02: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';
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:33:43 +02:00
|
|
|
if (page_params.debug_mode) {
|
|
|
|
// In development, we display blueslip errors in the web UI,
|
|
|
|
// to make them hard to miss.
|
2018-05-22 01:50:25 +02:00
|
|
|
stack = getErrorStack(stack);
|
2017-09-15 22:33:43 +02:00
|
|
|
exports.display_errors_on_screen(msg, stack);
|
|
|
|
}
|
|
|
|
|
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',
|
2017-10-16 22:07:19 +02:00
|
|
|
url: '/json/report/error',
|
2013-03-11 20:54:27 +01:00
|
|
|
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-10-22 21:19:59 +02:00
|
|
|
user_agent: window.navigator.userAgent,
|
|
|
|
log: logger.get_log().join("\n")},
|
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;
|
2018-08-03 21:56:42 +02:00
|
|
|
if (opts.show_ui_msg && ui_report !== undefined) {
|
2013-03-12 22:37:13 +01:00
|
|
|
// There are a few races here (and below in the error
|
|
|
|
// callback):
|
2018-08-03 21:56:42 +02:00
|
|
|
// 1) The ui_report module or something it requires might
|
2013-03-12 22:37:13 +01:00
|
|
|
// 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
|
2017-10-05 16:01:50 +02:00
|
|
|
// (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.
|
2017-03-18 21:17:41 +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-07-10 21:57:05 +02:00
|
|
|
"folks at Zulip, but, in the mean time, " +
|
2013-03-11 23:34:14 +01:00
|
|
|
"please try reloading the page.",
|
|
|
|
$("#home-error"), "alert-error");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function () {
|
2018-08-03 21:56:42 +02:00
|
|
|
if (opts.show_ui_msg && ui_report !== undefined) {
|
2017-03-18 21:17:41 +01:00
|
|
|
ui_report.message("Oops. It seems something has gone wrong. " +
|
2013-03-11 23:34:14 +01:00
|
|
|
"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-07-05 23:07:46 +02:00
|
|
|
|
2016-07-19 06:44:48 +02:00
|
|
|
if (page_params.save_stacktraces) {
|
2013-11-12 18:20:32 +01:00
|
|
|
// Save the stacktrace so it can be examined even in
|
|
|
|
// development servers. (N.B. This assumes you have set DEBUG
|
|
|
|
// = False on your development server, or else this code path
|
|
|
|
// won't execute to begin with -- useful for testing
|
|
|
|
// (un)minification.)
|
2013-07-05 23:07:46 +02:00
|
|
|
window.last_stacktrace = stack;
|
|
|
|
}
|
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
|
|
|
|
2014-02-10 22:48:49 +01:00
|
|
|
exports.exception_msg = function blueslip_exception_msg(ex) {
|
|
|
|
var message = ex.message;
|
|
|
|
if (ex.hasOwnProperty('fileName')) {
|
|
|
|
message += " at " + ex.fileName;
|
|
|
|
if (ex.hasOwnProperty('lineNumber')) {
|
|
|
|
message += ":" + ex.lineNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2019-07-09 04:09:53 +02:00
|
|
|
$(window).on('error', function (event) {
|
|
|
|
var ex = event.originalEvent.error;
|
2019-07-10 02:15:07 +02:00
|
|
|
if (!ex || ex instanceof BlueslipError) {
|
2019-07-09 04:09:53 +02:00
|
|
|
return;
|
2013-03-12 22:37:13 +01:00
|
|
|
}
|
2019-07-09 04:09:53 +02:00
|
|
|
var message = exports.exception_msg(ex);
|
|
|
|
report_error(message, ex.stack);
|
|
|
|
});
|
2013-03-12 22:37:13 +01:00
|
|
|
|
2013-10-22 19:59:13 +02:00
|
|
|
function build_arg_list(msg, more_info) {
|
|
|
|
var args = [msg];
|
2013-04-04 21:29:14 +02:00
|
|
|
if (more_info !== undefined) {
|
2013-10-22 19:59:13 +02:00
|
|
|
args.push("\nAdditional information: ", more_info);
|
2013-04-04 21:29:14 +02:00
|
|
|
}
|
2013-10-22 19:59:13 +02:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2013-10-30 15:16:36 +01:00
|
|
|
exports.debug = function blueslip_debug (msg, more_info) {
|
|
|
|
var args = build_arg_list(msg, more_info);
|
|
|
|
logger.debug.apply(logger, args);
|
|
|
|
};
|
|
|
|
|
2013-10-22 19:59:13 +02:00
|
|
|
exports.log = function blueslip_log (msg, more_info) {
|
|
|
|
var args = build_arg_list(msg, more_info);
|
2013-10-22 21:11:13 +02:00
|
|
|
logger.log.apply(logger, args);
|
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-10-22 19:59:13 +02:00
|
|
|
var args = build_arg_list(msg, more_info);
|
2013-10-22 21:11:13 +02:00
|
|
|
logger.info.apply(logger, args);
|
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-10-22 19:59:13 +02:00
|
|
|
var args = build_arg_list(msg, more_info);
|
2013-10-22 21:11:13 +02:00
|
|
|
logger.warn.apply(logger, args);
|
2013-03-25 23:26:14 +01:00
|
|
|
if (page_params.debug_mode) {
|
2013-03-11 17:25:46 +01:00
|
|
|
console.trace();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-15 22:33:43 +02:00
|
|
|
exports.display_errors_on_screen = function (error, stack) {
|
|
|
|
var $exit = "<div class='exit'></div>";
|
|
|
|
var $error = "<div class='error'>" + error + "</div>";
|
|
|
|
var $pre = "<pre>" + stack + "</pre>";
|
|
|
|
var $alert = $("<div class='alert browser-alert home-error-bar'></div>").html($error + $exit + $pre);
|
|
|
|
|
|
|
|
$(".app .alert-box").append($alert.addClass("show"));
|
|
|
|
};
|
|
|
|
|
2014-01-31 06:23:34 +01:00
|
|
|
exports.error = function blueslip_error (msg, more_info, stack) {
|
2017-09-15 22:33:43 +02:00
|
|
|
if (stack === undefined) {
|
|
|
|
stack = Error().stack;
|
|
|
|
}
|
|
|
|
var args = build_arg_list(msg, more_info);
|
|
|
|
logger.error.apply(logger, args);
|
|
|
|
report_error(msg, stack, {more_info: 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-04 21:29:14 +02:00
|
|
|
exports.fatal = function blueslip_fatal (msg, more_info) {
|
2017-09-15 22:33:43 +02:00
|
|
|
report_error(msg, Error().stack, {more_info: more_info});
|
2013-04-04 21:29:14 +02:00
|
|
|
throw new BlueslipError(msg, more_info);
|
2013-03-11 17:25:46 +01:00
|
|
|
};
|
|
|
|
|
2017-06-03 19:32:41 +02:00
|
|
|
// Produces an easy-to-read preview on an HTML element. Currently
|
|
|
|
// only used for including in error report emails; be sure to discuss
|
|
|
|
// with other developers before using it in a user-facing context
|
|
|
|
// because it is not XSS-safe.
|
|
|
|
exports.preview_node = function (node) {
|
|
|
|
if (node.constructor === jQuery) {
|
|
|
|
node = node[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
var tag = node.tagName.toLowerCase();
|
|
|
|
var className = node.className.length ? node.className : false;
|
|
|
|
var id = node.id.length ? node.id : false;
|
|
|
|
|
|
|
|
var node_preview = "<" + tag +
|
|
|
|
(id ? " id='" + id + "'" : "") +
|
|
|
|
(className ? " class='" + className + "'" : "") +
|
|
|
|
"></" + tag + ">";
|
|
|
|
|
|
|
|
return node_preview;
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.blueslip = exports;
|