signal-desktop/ts/components/ErrorBoundary.tsx

59 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-07-29 00:10:07 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
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';
import { ToastType } from '../types/Toast';
2022-07-29 00:10:07 +00:00
export type Props = {
children: ReactNode;
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 {
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(
`ErrorBoundary/${name}: ` +
`captured rendering error ${Errors.toLogFormat(error)}` +
`\nerrorInfo: ${errorInfo.componentStack}`
2022-07-29 00:10:07 +00:00
);
if (window.reduxActions) {
window.reduxActions.toast.showToast({ toastType: ToastType.Error });
2022-07-29 00:10:07 +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;
}
}