2022-07-29 00:10:07 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-08-19 18:35:40 +00:00
|
|
|
import type { ReactNode, ErrorInfo } from 'react';
|
2022-07-29 00:10:07 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import * as Errors from '../types/errors';
|
|
|
|
import * as log from '../logging/log';
|
2022-12-15 00:48:36 +00:00
|
|
|
import { ToastType } from '../types/Toast';
|
2022-07-29 00:10:07 +00:00
|
|
|
|
|
|
|
export type Props = {
|
|
|
|
children: ReactNode;
|
2022-08-19 18:35:40 +00:00
|
|
|
name: string;
|
|
|
|
closeView?: () => unknown;
|
2022-07-29 00:10:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type State = {
|
|
|
|
error?: Error;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class ErrorBoundary extends React.PureComponent<Props, State> {
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = { error: undefined };
|
|
|
|
}
|
|
|
|
|
|
|
|
public static getDerivedStateFromError(error: Error): State {
|
2022-08-19 18:35:40 +00:00
|
|
|
return { error };
|
|
|
|
}
|
|
|
|
|
|
|
|
public override componentDidCatch(error: Error, errorInfo: ErrorInfo): void {
|
|
|
|
const { closeView, name } = this.props;
|
|
|
|
|
2022-07-29 00:10:07 +00:00
|
|
|
log.error(
|
2022-08-19 18:35:40 +00:00
|
|
|
`ErrorBoundary/${name}: ` +
|
|
|
|
`captured rendering error ${Errors.toLogFormat(error)}` +
|
|
|
|
`\nerrorInfo: ${errorInfo.componentStack}`
|
2022-07-29 00:10:07 +00:00
|
|
|
);
|
|
|
|
if (window.reduxActions) {
|
2023-04-10 21:38:34 +00:00
|
|
|
window.reduxActions.toast.showToast({ toastType: ToastType.Error });
|
2022-07-29 00:10:07 +00:00
|
|
|
}
|
2022-08-19 18:35:40 +00:00
|
|
|
if (closeView) {
|
|
|
|
closeView();
|
|
|
|
}
|
2022-07-29 00:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override render(): ReactNode {
|
|
|
|
const { error } = this.state;
|
|
|
|
const { children } = this.props;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
}
|