Skip to content

Commit

Permalink
feat(error-handling) refactor global error and unhandledrejection eve…
Browse files Browse the repository at this point in the history
…nt handling

Conceptually related: jitsi/lib-jitsi-meet#2411
  • Loading branch information
saghul authored Dec 15, 2023
1 parent 82ae6a8 commit 0c1ce15
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 55 deletions.
53 changes: 0 additions & 53 deletions react/features/base/lib-jitsi-meet/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
};
}
}
19 changes: 17 additions & 2 deletions react/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down

0 comments on commit 0c1ce15

Please sign in to comment.