UNPKG

error-flux

Version:

Network request interceptor and logger for web applications

139 lines 7.13 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { StorageTypes } from "./../types"; import { saveUnhandledErrors, saveConsoleErrors } from "../db/index"; import store from "../state/store"; import genUUID from "../utils/gen-uuid"; import { runLowPriorityTask } from "../utils/low-priority"; import { getConsoleErrorStoreName, getUnhandledErrorStoreName, } from "../utils/store-helpers"; const errorLogs = { saveError(storeName, errors) { return __awaiter(this, void 0, void 0, function* () { runLowPriorityTask(() => __awaiter(this, void 0, void 0, function* () { const { dbName, storageType } = store.getState(); switch (storageType) { case StorageTypes.LocalStorage: try { const dbData = JSON.parse(localStorage.getItem(dbName) || "{}"); const existingErrors = dbData[storeName] || []; existingErrors.push({ id: genUUID(), errors, timestamp: new Date().toISOString(), }); dbData[storeName] = existingErrors; localStorage.setItem(dbName, JSON.stringify(dbData)); } catch (err) { console.warn("Failed to save to localStorage:", err); } break; case StorageTypes.SessionStorage: try { const dbData = JSON.parse(sessionStorage.getItem(dbName) || "{}"); const existingErrors = dbData[storeName] || []; existingErrors.push({ id: genUUID(), errors, timestamp: new Date().toISOString(), }); dbData[storeName] = existingErrors; sessionStorage.setItem(dbName, JSON.stringify(dbData)); } catch (err) { console.warn("Failed to save to sessionStorage:", err); } break; case StorageTypes.IndexedDB: if (storeName === getConsoleErrorStoreName()) { yield saveConsoleErrors(errors); } else if (storeName === getUnhandledErrorStoreName()) { yield saveUnhandledErrors(errors); } break; } })); }); }, }; const errorFluxGlobalErrorInterceptor = ({ handleOnError, handleOnUnhandledRejection, }) => { if (handleOnError) window.onerror = function (message, source, lineno, colno, error) { const errorData = [ { type: "synchronous", message, source, lineno, colno, stack: (error === null || error === void 0 ? void 0 : error.stack) || String(error), timestamp: new Date().toISOString(), }, ]; errorLogs.saveError(getConsoleErrorStoreName(), errorData); }; if (handleOnUnhandledRejection) window.addEventListener("unhandledrejection", function (event) { var _a, _b, _c, _d, _e; const errorData = [ { type: "promise", message: ((_a = event.reason) === null || _a === void 0 ? void 0 : _a.message) || "Unhandled Promise Rejection", source: ((_b = event.reason) === null || _b === void 0 ? void 0 : _b.fileName) || "unknown", lineno: ((_c = event.reason) === null || _c === void 0 ? void 0 : _c.lineNumber) || 0, colno: ((_d = event.reason) === null || _d === void 0 ? void 0 : _d.columnNumber) || 0, stack: ((_e = event.reason) === null || _e === void 0 ? void 0 : _e.stack) || String(event.reason), timestamp: new Date().toISOString(), }, ]; errorLogs.saveError(store.getState().storeName.unhandledErrors, errorData); }); const getLogs = () => { const { storeName: storeNameObj, dbName, storageType } = store.getState(); const consoleErrorsStoreName = storeNameObj.consoleErrors; const unhandledErrorsStoreName = storeNameObj.unhandledErrors; const getConsoleErrors = () => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; switch (storageType) { case StorageTypes.IndexedDB: return yield getConsoleErrors(); case StorageTypes.LocalStorage: return (((_a = JSON.parse(localStorage.getItem(dbName) || "{}")) === null || _a === void 0 ? void 0 : _a[consoleErrorsStoreName]) || []); case StorageTypes.SessionStorage: return (((_b = JSON.parse(sessionStorage.getItem(dbName) || "{}")) === null || _b === void 0 ? void 0 : _b[consoleErrorsStoreName]) || []); default: return []; } }); const getUnhandledErrors = () => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; switch (storageType) { case StorageTypes.IndexedDB: return yield getUnhandledErrors(); case StorageTypes.LocalStorage: return (((_a = JSON.parse(localStorage.getItem(dbName) || "{}")) === null || _a === void 0 ? void 0 : _a[unhandledErrorsStoreName]) || []); case StorageTypes.SessionStorage: return (((_b = JSON.parse(sessionStorage.getItem(dbName) || "{}")) === null || _b === void 0 ? void 0 : _b[unhandledErrorsStoreName]) || []); default: return []; } }); return { getConsoleErrors, getUnhandledErrors, }; }; return { getLogs, }; }; export default errorFluxGlobalErrorInterceptor; //# sourceMappingURL=global-error.js.map