From 7c39454a9d9664fea90109b58f8ce23163717c43 Mon Sep 17 00:00:00 2001 From: Dragory Date: Tue, 15 Jan 2019 01:05:12 +0200 Subject: [PATCH] Only tolerate recent plugin errors (not any errors) --- src/index.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 08cc6412..f6e45857 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,22 +4,33 @@ import yaml from "js-yaml"; import _fs from "fs"; const fs = _fs.promises; +import {Knub, logger, PluginError} from "knub"; import { SimpleError } from "./SimpleError"; require("dotenv").config(); -let recentErrors = 0; +let recentPluginErrors = 0; const RECENT_ERROR_EXIT_THRESHOLD = 5; -setInterval(() => (recentErrors = Math.max(0, recentErrors - 1)), 2500); +setInterval(() => (recentPluginErrors = Math.max(0, recentPluginErrors - 1)), 2500); process.on("unhandledRejection", (reason, p) => { console.error(reason); - if (++recentErrors >= RECENT_ERROR_EXIT_THRESHOLD) process.exit(1); + + if (reason instanceof PluginError) { + if (++recentPluginErrors >= RECENT_ERROR_EXIT_THRESHOLD) process.exit(1); + } else { + process.exit(1); + } }); process.on("uncaughtException", err => { console.error(err); - if (++recentErrors >= RECENT_ERROR_EXIT_THRESHOLD) process.exit(1); + + if (err instanceof PluginError) { + if (++recentPluginErrors >= RECENT_ERROR_EXIT_THRESHOLD) process.exit(1); + } else { + process.exit(1); + } }); // Verify required Node.js version @@ -37,7 +48,6 @@ import moment from "moment-timezone"; moment.tz.setDefault("UTC"); import { Client } from "eris"; -import { Knub, logger } from "knub"; import { connect } from "./data/db"; // Global plugins