Path: blob/master/src/packages/project/bug-counter.ts
1447 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { getLogger } from "./logger";67let bugCount: number = 0;89const STARS =10"\nBUG ****************************************************************************\n";1112export function init() {13const log = getLogger("BUG (uncaughtException)");14getLogger("handler").debug("initializing uncaughtException handler");1516process.on("uncaughtExceptionMonitor", (err, origin) => {17// sometimes we only get one output and then process terminates, despite the uncaughtException,18// so we make the best of it:19log.error(STARS, err, origin, err.stack);20});2122const f = (err) => {23bugCount += 1;24const border = `BUG (count=${bugCount}) ${STARS}`;25log.error(border);26log.error(`Uncaught exception: ${err}`);27console.warn(err);28log.error(err.stack);29log.error(border);30};31process.on("uncaughtException", f);32process.on("unhandledRejection", f);33}3435export default function getBugCount(): number {36return bugCount;37}3839export function bad(n) {40if (Math.random() < n) {41console.log("not throwing error");42return;43}44console.log("throwing an error on purpose");45throw Error("foo");46}474849