Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/bug-counter.ts
1447 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { getLogger } from "./logger";
7
8
let bugCount: number = 0;
9
10
const STARS =
11
"\nBUG ****************************************************************************\n";
12
13
export function init() {
14
const log = getLogger("BUG (uncaughtException)");
15
getLogger("handler").debug("initializing uncaughtException handler");
16
17
process.on("uncaughtExceptionMonitor", (err, origin) => {
18
// sometimes we only get one output and then process terminates, despite the uncaughtException,
19
// so we make the best of it:
20
log.error(STARS, err, origin, err.stack);
21
});
22
23
const f = (err) => {
24
bugCount += 1;
25
const border = `BUG (count=${bugCount}) ${STARS}`;
26
log.error(border);
27
log.error(`Uncaught exception: ${err}`);
28
console.warn(err);
29
log.error(err.stack);
30
log.error(border);
31
};
32
process.on("uncaughtException", f);
33
process.on("unhandledRejection", f);
34
}
35
36
export default function getBugCount(): number {
37
return bugCount;
38
}
39
40
export function bad(n) {
41
if (Math.random() < n) {
42
console.log("not throwing error");
43
return;
44
}
45
console.log("throwing an error on purpose");
46
throw Error("foo");
47
}
48
49