Skip to content

Commit

Permalink
Limit the rate of pinging frontend error to 1 ping every 5 seconds (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangviet1993 authored and seanlip committed Oct 27, 2018
1 parent 8cac910 commit 61fadba
Showing 1 changed file with 25 additions and 18 deletions.
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);
};
}]);

0 comments on commit 61fadba

Please sign in to comment.