2019-03-22 00:32:48 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2024-03-07 01:28:18 +08:00
|
|
|
import logger, { generateLoggerStreams } from '/imports/startup/client/logger';
|
2024-05-14 03:58:13 +08:00
|
|
|
import apolloContextHolder from '/imports/ui/core/graphql/apolloContextHolder/apolloContextHolder';
|
2024-10-15 08:23:58 +08:00
|
|
|
import Session from '/imports/ui/services/storage/in-memory';
|
2024-05-14 03:58:13 +08:00
|
|
|
import { ApolloLink } from '@apollo/client';
|
2019-03-22 00:32:48 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
children: PropTypes.element.isRequired,
|
2024-06-29 03:58:38 +08:00
|
|
|
Fallback: PropTypes.func,
|
2024-03-07 01:28:18 +08:00
|
|
|
errorMessage: PropTypes.string,
|
2024-05-03 23:27:17 +08:00
|
|
|
logMetadata: PropTypes.shape({
|
|
|
|
logCode: PropTypes.string,
|
|
|
|
logMessage: PropTypes.string,
|
|
|
|
}),
|
2024-09-11 03:00:47 +08:00
|
|
|
// isCritical: flags this error boundary as critical for the app's lifecycle,
|
|
|
|
// which means that the app should not continue to run if this error boundary
|
|
|
|
// is triggered. If true, terminates RTC audio connections and the Apollo client.
|
|
|
|
isCritical: PropTypes.bool,
|
2024-03-07 01:28:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
Fallback: null,
|
|
|
|
errorMessage: 'Something went wrong',
|
2024-05-03 23:27:17 +08:00
|
|
|
logMetadata: {
|
|
|
|
logCode: 'Error_Boundary_wrapper',
|
|
|
|
logMessage: 'generic error boundary logger',
|
|
|
|
},
|
2024-09-11 03:00:47 +08:00
|
|
|
isCritical: false,
|
2019-03-22 00:32:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ErrorBoundary extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2024-03-07 01:28:18 +08:00
|
|
|
this.state = { error: '', errorInfo: null };
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2024-07-16 21:40:13 +08:00
|
|
|
const data = window.meetingClientSettings.public;
|
2024-03-07 01:28:18 +08:00
|
|
|
const logConfig = data?.clientLog;
|
2024-09-20 02:57:27 +08:00
|
|
|
if (logConfig && logger?.getStreams().length === 0) {
|
2024-03-07 01:28:18 +08:00
|
|
|
generateLoggerStreams(logConfig).forEach((stream) => {
|
|
|
|
logger.addStream(stream);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2024-05-03 23:27:17 +08:00
|
|
|
const { error, errorInfo } = this.state;
|
|
|
|
const { logMetadata: { logCode, logMessage } } = this.props;
|
2024-03-07 01:28:18 +08:00
|
|
|
if (error || errorInfo) {
|
2024-05-03 23:27:17 +08:00
|
|
|
logger.error({
|
|
|
|
logCode,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error?.message,
|
|
|
|
errorStack: error?.stack,
|
|
|
|
errorInfo,
|
|
|
|
},
|
|
|
|
}, logMessage);
|
2024-03-07 01:28:18 +08:00
|
|
|
}
|
2019-03-22 00:32:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidCatch(error, errorInfo) {
|
2024-09-11 03:00:47 +08:00
|
|
|
const { isCritical } = this.props;
|
2024-05-14 03:58:13 +08:00
|
|
|
|
2024-09-11 03:00:47 +08:00
|
|
|
if (isCritical) {
|
|
|
|
window.dispatchEvent(new Event('StopAudioTracks'));
|
|
|
|
const data = window.meetingClientSettings.public.media;
|
|
|
|
const mediaElement = document.querySelector(data?.mediaTag || '#remote-media');
|
|
|
|
if (mediaElement) {
|
|
|
|
mediaElement.pause();
|
|
|
|
mediaElement.srcObject = null;
|
|
|
|
}
|
|
|
|
const apolloClient = apolloContextHolder.getClient();
|
2024-05-14 03:58:13 +08:00
|
|
|
|
2024-09-11 03:00:47 +08:00
|
|
|
if (apolloClient) {
|
|
|
|
apolloClient.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ws = apolloContextHolder.getLink();
|
|
|
|
if (ws) {
|
|
|
|
// delay to termintate the connection, for user receive the end eject message
|
|
|
|
setTimeout(() => {
|
|
|
|
apolloClient.setLink(ApolloLink.empty());
|
|
|
|
ws.terminate();
|
|
|
|
}, 5000);
|
|
|
|
}
|
2024-05-14 03:58:13 +08:00
|
|
|
}
|
2024-09-11 03:00:47 +08:00
|
|
|
|
2024-10-15 08:23:58 +08:00
|
|
|
if ('cause' in error) {
|
|
|
|
Session.setItem('errorMessageDescription', error.cause);
|
|
|
|
}
|
|
|
|
|
2019-03-22 00:32:48 +08:00
|
|
|
this.setState({
|
|
|
|
error,
|
|
|
|
errorInfo,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2024-03-07 01:28:18 +08:00
|
|
|
const { error, errorInfo } = this.state;
|
|
|
|
const { children, Fallback, errorMessage } = this.props;
|
2019-03-22 00:32:48 +08:00
|
|
|
|
2024-03-07 01:28:18 +08:00
|
|
|
const fallbackElement = Fallback && error
|
2024-10-17 03:02:53 +08:00
|
|
|
? <Fallback error={error || {}} /> : <div>{errorMessage}</div>;
|
2024-03-07 01:28:18 +08:00
|
|
|
return (error
|
|
|
|
? fallbackElement
|
|
|
|
: children);
|
2019-03-22 00:32:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorBoundary.propTypes = propTypes;
|
2024-03-07 01:28:18 +08:00
|
|
|
ErrorBoundary.defaultProps = defaultProps;
|
|
|
|
|
|
|
|
export default ErrorBoundary;
|
2019-03-22 00:32:48 +08:00
|
|
|
|
2022-01-19 02:27:54 +08:00
|
|
|
export const withErrorBoundary = (WrappedComponent, FallbackComponent) => (props) => (
|
2019-03-22 00:32:48 +08:00
|
|
|
<ErrorBoundary Fallback={FallbackComponent}>
|
|
|
|
<WrappedComponent {...props} />
|
|
|
|
</ErrorBoundary>
|
|
|
|
);
|