CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/bug-counter.ts
Views: 687
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
log.error(err.stack);
29
log.error(border);
30
};
31
process.on("uncaughtException", f);
32
process.on("unhandledRejection", f);
33
}
34
35
export default function getBugCount(): number {
36
return bugCount;
37
}
38
39
export function bad(n) {
40
if (Math.random() < n) {
41
console.log("not throwing error");
42
return;
43
}
44
console.log("throwing an error on purpose");
45
throw Error("foo");
46
}
47
48