From 0c1ce152fe46ff098c98d0a44e0c5a4fb7b28920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Fri, 15 Dec 2023 23:56:19 +0100 Subject: [PATCH] feat(error-handling) refactor global error and unhandledrejection event handling Conceptually related: https://github.com/jitsi/lib-jitsi-meet/pull/2411 --- .../base/lib-jitsi-meet/middleware.ts | 53 ------------------- react/index.web.js | 19 ++++++- 2 files changed, 17 insertions(+), 55 deletions(-) diff --git a/react/features/base/lib-jitsi-meet/middleware.ts b/react/features/base/lib-jitsi-meet/middleware.ts index 397db6da7bd1..1e66134df6d5 100644 --- a/react/features/base/lib-jitsi-meet/middleware.ts +++ b/react/features/base/lib-jitsi-meet/middleware.ts @@ -7,7 +7,6 @@ import { PARTICIPANT_LEFT } from '../participants/actionTypes'; import MiddlewareRegistry from '../redux/MiddlewareRegistry'; import JitsiMeetJS from './_'; -import { LIB_WILL_INIT } from './actionTypes'; import { disposeLib, initLib } from './actions'; /** @@ -22,14 +21,6 @@ import { disposeLib, initLib } from './actions'; */ MiddlewareRegistry.register(store => next => action => { switch (action.type) { - case LIB_WILL_INIT: - // Moved from conference.js init method. It appears the error handlers - // are not used for mobile. - if (typeof APP !== 'undefined') { - _setErrorHandlers(); - } - break; - case SET_NETWORK_INFO: JitsiMeetJS.setNetworkInfo({ isOnline: action.isOnline @@ -81,47 +72,3 @@ function _setConfig({ dispatch, getState }: IStore, next: Function, action: AnyA return result; } - -/** - * Attaches our custom error handlers to the window object. - * - * @returns {void} - */ -function _setErrorHandlers() { - // attaches global error handler, if there is already one, respect it - if (JitsiMeetJS.getGlobalOnErrorHandler) { - const oldOnErrorHandler = window.onerror; - - // TODO: Don't remove this ignore. The build fails on macOS and we don't know yet why. - - // @ts-ignore - window.onerror = (message, source, lineno, colno, error) => { // eslint-disable-line max-params - const errMsg = message || error?.message; - const stack = error?.stack; - - JitsiMeetJS.getGlobalOnErrorHandler(errMsg, source, lineno, colno, stack); - - if (oldOnErrorHandler) { - oldOnErrorHandler(message, source, lineno, colno, error); - } - }; - - const oldOnUnhandledRejection = window.onunhandledrejection; - - window.onunhandledrejection = function(event) { - let message = event.reason; - let stack: string | undefined = 'n/a'; - - if (event.reason instanceof Error) { - ({ message, stack } = event.reason); - } - - JitsiMeetJS.getGlobalOnErrorHandler(message, null, null, null, stack); - - if (oldOnUnhandledRejection) { - // @ts-ignore - oldOnUnhandledRejection(event); - } - }; - } -} diff --git a/react/index.web.js b/react/index.web.js index 1b3f8328a319..d97c6a5d05ef 100644 --- a/react/index.web.js +++ b/react/index.web.js @@ -9,12 +9,27 @@ import DialInSummaryApp from './features/invite/components/dial-in-summary/web/D import PrejoinApp from './features/prejoin/components/web/PrejoinApp'; const logger = getLogger('index.web'); -const OS = Platform.OS; + +// Add global loggers. +window.addEventListener('error', ev => { + logger.error( + `UnhandledError: ${ev.message}`, + `Script: ${ev.filename}`, + `Line: ${ev.lineno}`, + `Column: ${ev.colno}`, + 'StackTrace: ', ev.error?.stack); +}); + +window.addEventListener('unhandledrejection', ev => { + logger.error( + `UnhandledPromiseRejection: ${ev.reason}`, + 'StackTrace: ', ev.reason?.stack); +}); // Workaround for the issue when returning to a page with the back button and // the page is loaded from the 'back-forward' cache on iOS which causes nothing // to be rendered. -if (OS === 'ios') { +if (Platform.OS === 'ios') { window.addEventListener('pageshow', event => { // Detect pages loaded from the 'back-forward' cache // (https://webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/)