From c2513332870b4c288598bef8104cbcb5644f00ef Mon Sep 17 00:00:00 2001 From: Gavin Morrow Date: Wed, 18 Dec 2024 21:11:52 -0500 Subject: [PATCH] Prevent spam errors from being reported too much --- src/util/report-error.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/util/report-error.js b/src/util/report-error.js index e638146..6519414 100644 --- a/src/util/report-error.js +++ b/src/util/report-error.js @@ -4,5 +4,22 @@ */ const reportError = err => { console.error(err); - alert(`Error: ${err.message}. If this persists, please contact Gavin.`); + if (shouldAlert(err)) { + alert(`Error: ${err.message}. If this persists, please contact Gavin.`); + } +}; + +///-/// Prevent spam errors from being reported too much ///-/// +/** @type {Map } */ +let recentErrors = new Map(); +/** @param {ApiError} err @returns {bool}*/ +const shouldAlert = (err) => { + const numRecent = recentErrors.get(err.action) ?? 0; + + // Increment count + recentErrors.set(err.action, numRecent + 1); + // Decrement count after period of time + setTimeout(() => recentErrors.set(err.action, recentErrors.get(err.action) - 1), 1 * 1000); + + return numRecent <= 0; };