Keep recent warnings in session storage

This commit is contained in:
Alicia Sykes 2021-09-12 15:59:42 +01:00
parent 7b68af1bd4
commit 9ee4878fee
1 changed files with 18 additions and 3 deletions

View File

@ -1,6 +1,20 @@
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
import * as Sentry from '@sentry/vue';
import { warningMsg } from '@/utils/CoolConsole';
import { sessionStorageKeys } from '@/utils/defaults';
/* Makes the current time, like hh:mm:ss */
const makeTime = () => {
const now = new Date();
const pad = (digit) => String(digit).padStart(2, '0');
return `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
};
/* Appends recent errors to local storage, for viewing in the UI */
const appendToErrorLog = (msg) => {
let errorLog = sessionStorage.getItem(sessionStorageKeys.ERROR_LOG) || '';
errorLog += `[${makeTime()}] ${msg}\n`;
sessionStorage.setItem(sessionStorageKeys.ERROR_LOG, errorLog);
};
/**
* Function called when an error happens
@ -9,8 +23,9 @@ import { warningMsg } from '@/utils/CoolConsole';
* If you wish to use your own error logging service, put code for it here
*/
const ErrorHandler = function handler(msg) {
warningMsg(msg);
Sentry.captureMessage(`[USER-WARN] ${msg || 'Uncaptured Message'}`);
warningMsg(msg); // Print to console
appendToErrorLog(msg); // Save to local storage
Sentry.captureMessage(`[USER-WARN] ${msg}`); // Report to bug tracker (if enabled)
};
export default ErrorHandler;