UNPKG

weer

Version:

Web Extensions Error Reporter catches global errors, shows notifications and opens error reporter in one click

83 lines (57 loc) 2.03 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var Utils = _interopDefault(require('./utils')); var Debug = window.debug || ((/* Logger ID */) => (/* Log message */) => { /* Ignore all */ }); const debug = Debug('weer:catcher'); const bgName = 'BG'; var errorCatchers = { installListenersOn({ hostWindow = window, nameForDebug = bgName, handleErrorMessage, } = {}, cb) { const ifInBg = hostWindow === window; const bgIsBg = `Background window can't have name other than "${bgName}". ` + `Default value is "${bgName}".`; if (ifInBg) { Utils.assert( typeof handleErrorMessage === 'function', 'Messaging from BG window to itself is not allowed,' + ' provide message handler for such cases.', ); Utils.assert(nameForDebug === 'BG', bgIsBg); } else { Utils.assert(nameForDebug !== 'BG', bgIsBg); } const listener = (errorEvent) => { debug(nameForDebug, errorEvent); const plainObj = Utils.errorEventToPlainObject(errorEvent); const msg = { to: 'error-reporter', payload: plainObj, }; if (ifInBg) { // Because self messaging is not allowed. handleErrorMessage(msg); } else { hostWindow.chrome.runtime.sendMessage(msg); } }; const ifUseCapture = true; hostWindow.addEventListener('error', listener, ifUseCapture); const rejHandler = (event) => { event.preventDefault(); debug(nameForDebug, 'rethrowing promise...'); throw event.reason; }; hostWindow.addEventListener('unhandledrejection', rejHandler, ifUseCapture); if (cb) { Utils.timeouted(cb)(); } return function uninstallListeners() { hostWindow.removeEventListener('error', listener, ifUseCapture); hostWindow.removeEventListener('unhandledrejection', rejHandler, ifUseCapture); }; }, }; module.exports = errorCatchers;