blob: aa6dc0a55464fe649f478ea229f8af8d96695989 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001/* Copyright 2016 The Chromium Authors. All Rights Reserved.
2 *
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file or at
5 * https://developers.google.com/open-source/licenses/bsd
6 */
7
8(function(window) {
9 'use strict';
10
11 // This code sets up a reporting mechanism for uncaught javascript errors
12 // to the server. It reports at most every THRESHOLD_MS milliseconds and
13 // each report contains error signatures with counts.
14
15 let errBuff = {};
16 let THRESHOLD_MS = 2000;
17
18 function throttle(fn) {
19 let last, timer;
20 return function() {
21 let now = Date.now();
22 if (last && now < last + THRESHOLD_MS) {
23 clearTimeout(timer);
24 timer = setTimeout(function() {
25 last = now;
26 fn.apply();
27 }, THRESHOLD_MS + last - now);
28 } else {
29 last = now;
30 fn.apply();
31 }
32 };
33 }
34 let flushErrs = throttle(function() {
35 let data = {errors: JSON.stringify(errBuff)};
36 CS_doPost('/_/clientmon.do', null, data);
37 errBuff = {};
38 });
39
40 window.addEventListener('error', function(evt) {
41 let signature = evt.message;
42 if (evt.error instanceof Error) {
43 signature += '\n' + evt.error.stack;
44 }
45 if (!errBuff[signature]) {
46 errBuff[signature] = 0;
47 }
48 errBuff[signature] += 1;
49 flushErrs();
50 });
51})(window);