Skip to content

Commit

Permalink
Prevent spam errors from being reported too much
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinmorrow committed Dec 19, 2024
1 parent 4e3d921 commit c251333
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/util/report-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof typeof ApiError.MESSAGES, number> } */
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;
};

0 comments on commit c251333

Please sign in to comment.