Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the rate of pinging frontend error down to 1 ping every 5 seconds #92

Merged
merged 1 commit into from
Oct 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions app/static/pages/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ oppiaFoundationWebsite.constant(
// Overwrite the built-in exceptionHandler service to log errors to the backend
// (so that they can be fixed).
oppiaFoundationWebsite.factory('$exceptionHandler', ['$log', function($log) {
var MIN_TIME_BETWEEN_ERRORS_MSEC = 5000;
var timeOfLastPostedError = Date.now() - MIN_TIME_BETWEEN_ERRORS_MSEC;
return function(exception, cause) {
var messageAndSourceAndStackTrace = [
'',
Expand All @@ -127,25 +129,30 @@ oppiaFoundationWebsite.factory('$exceptionHandler', ['$log', function($log) {
String(exception.stack),
' at URL: ' + window.location.href
].join('\n');
// Catch all errors, to guard against infinite recursive loops.
try {
// We use jQuery here instead of Angular's $http, since the latter
// creates a circular dependency.
$.ajax({
type: 'POST',
url: '/ajax/frontend_errors',
data: $.param({
payload: JSON.stringify({
error: messageAndSourceAndStackTrace
}),
source: document.URL
}, true),
dataType: 'text',
async: true
});
} catch (loggingError) {
$log.warn('Error logging failed.');

// Throttle to at most 1 backend ping every MIN_TIME_BETWEEN_ERRORS_MSEC.
if (Date.now() - timeOfLastPostedError > MIN_TIME_BETWEEN_ERRORS_MSEC) {
// Catch all errors, to guard against infinite recursive loops.
try {
// We use jQuery here instead of Angular's $http, since the latter
// creates a circular dependency.
$.ajax({
type: 'POST',
url: '/ajax/frontend_errors',
data: $.param({
payload: JSON.stringify({
error: messageAndSourceAndStackTrace
}),
source: document.URL
}, true),
dataType: 'text',
async: true
});
} catch (loggingError) {
$log.warn('Error logging failed.');
}
}

$log.error.apply($log, arguments);
};
}]);