react-app-error-boundary
Version:
Allows to opt-out of react-error-overlay in your react-app
66 lines (65 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.suppressCaughtOverlayError = exports.setupReactAppOverlayErrorHandler = void 0;
var INTERCEPT_ENABLED = process.env.NODE_ENV === 'development';
var DEFAULT_CRA_OVERLAY_SELECTOR = 'iframe[style*="position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; border: none; z-index: 2147483647;"]';
var STYLES_ID = 'suppress-cra-overlay-styles';
// ---
function getStyles() {
return document.getElementById(STYLES_ID);
}
function createStyles(craOverlaySelector) {
if (getStyles() !== null)
return;
// Use styles instead of accessing overlay element directly, because overlay is created dynamically,
// and won't exist at moment when our handler catches error
var s = document.createElement('style');
s.id = STYLES_ID;
s.innerHTML = "".concat(craOverlaySelector, " { display: none; !important }");
document.head.appendChild(s);
}
function allowCRAOverlay(allow) {
// `style` elements DO support `disabled` prop. TS doesn't seem to know about it.
var styles = getStyles();
if (!styles)
return;
styles.disabled = allow;
}
// ---
var CAPTURED_ERROR_FLAG_NAME = '@@/CRA_CAPTURED';
var SUPPRESSED_ERROR_FLAG_NAME = '@@/CRA_OVERLAY_IGNORE';
function setupReactAppOverlayErrorHandler(options) {
if (options === void 0) { options = {}; }
if (!INTERCEPT_ENABLED)
return;
var _a = options.craOverlaySelector, craOverlaySelector = _a === void 0 ? DEFAULT_CRA_OVERLAY_SELECTOR : _a;
createStyles(craOverlaySelector);
window.addEventListener('error', function (e) {
var error = e.error;
if (error === undefined || error === null)
return;
if (!error[CAPTURED_ERROR_FLAG_NAME]) {
error[CAPTURED_ERROR_FLAG_NAME] = true;
allowCRAOverlay(false);
// Suppress logging error to console by browser
e.preventDefault();
// Revisit this error after the error boundary element processed it
setTimeout(function () {
// can be set by the error boundary error handler
if (!error[SUPPRESSED_ERROR_FLAG_NAME]) {
// but if it wasn't caught by a boundary, release it back to the wild
allowCRAOverlay(true);
throw error;
}
});
}
});
}
exports.setupReactAppOverlayErrorHandler = setupReactAppOverlayErrorHandler;
function suppressCaughtOverlayError(error) {
var _a;
if (!INTERCEPT_ENABLED)
return;
Object.assign(error, (_a = {}, _a[SUPPRESSED_ERROR_FLAG_NAME] = true, _a));
}
exports.suppressCaughtOverlayError = suppressCaughtOverlayError;