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