feat(phone/error-handling): add top level error boundary for reload

This commit is contained in:
Taso committed 2021-07-27 17:04:29 -04:00
1 parent 908a393451
commit 3b7326dac8
3 files changed
+88 -16

No files matched your search

+19 -16
View File
@@ -32,6 +32,7 @@ import PhoneWrapper from './PhoneWrapper';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import DefaultConfig from '../../config.json'; import DefaultConfig from '../../config.json';
import { TopLevelErrorComponent } from './ui/components/TopLevelErrorComponent';
function Phone() { function Phone() {
const { t, i18n } = useTranslation(); const { t, i18n } = useTranslation();
@@ -78,22 +79,24 @@ function Phone() {
return ( return (
<div> <div>
<WindowSnackbar /> <TopLevelErrorComponent>
<PhoneWrapper> <WindowSnackbar />
<NotificationBar /> <PhoneWrapper>
<div className="PhoneAppContainer"> <NotificationBar />
<> <div className="PhoneAppContainer">
<Route exact path="/" component={HomeApp} /> <>
{callModal && <Route exact path="/call" component={CallModal} />} <Route exact path="/" component={HomeApp} />
{apps.map((App) => ( {callModal && <Route exact path="/call" component={CallModal} />}
<App.Route key={App.id} /> {apps.map((App) => (
))} <App.Route key={App.id} />
</> ))}
<NotificationAlert /> </>
<PhoneSnackbar /> <NotificationAlert />
</div> <PhoneSnackbar />
<Navigation /> </div>
</PhoneWrapper> <Navigation />
</PhoneWrapper>
</TopLevelErrorComponent>
</div> </div>
); );
} }
+5
View File
@@ -1,5 +1,10 @@
{ {
"translation": { "translation": {
"MISC": {
"TOP_LEVEL_ERR_TITLE": "Fatal Error",
"TOP_LEVEL_ERR_ACTION": "Reload",
"TOP_LEVEL_ERR_MSG": "NPWD has encountered an error it was unable to recover from. You will need to reload the NUI window. The error is shown below for developer reference."
},
"DATE_FORMAT": "MM/DD/YYYY", "DATE_FORMAT": "MM/DD/YYYY",
"DATE_TIME_FORMAT": "MM/DD/YYYY hh:mm A", "DATE_TIME_FORMAT": "MM/DD/YYYY hh:mm A",
"CALLS": { "CALLS": {
@@ -0,0 +1,64 @@
import React from 'react';
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
} from '@material-ui/core';
import { useTranslation } from 'react-i18next';
interface TopLevelErrorCompProps {
hasError: boolean;
errorMsg: string;
}
interface ErrorDialogCompProps {
isOpen: boolean;
errorMsg: string;
}
const ErrorDialogComp: React.FC<ErrorDialogCompProps> = ({ isOpen, errorMsg }) => {
const { t } = useTranslation();
const handleReloadClick = () => window.location.reload();
return (
<Dialog open={isOpen}>
<DialogTitle>{t('MISC.TOP_LEVEL_ERR_TITLE')}</DialogTitle>
<DialogContent>
<DialogContentText>
{t('MISC.TOP_LEVEL_ERR_MSG')}
<br />
<br />
<code style={{ color: 'red' }}>{errorMsg}</code>
</DialogContentText>
<DialogActions>
<Button color="primary" onClick={handleReloadClick}>
{t('MISC.TOP_LEVEL_ERR_ACTION')}
</Button>
</DialogActions>
</DialogContent>
</Dialog>
);
};
export class TopLevelErrorComponent extends React.Component<any, TopLevelErrorCompProps> {
public state = {
hasError: false,
errorMsg: '',
};
static getDerivedStateFromError(error: Error) {
return { hasError: true, errorMsg: error.message };
}
render() {
if (this.state.hasError) {
return <ErrorDialogComp isOpen={this.state.hasError} errorMsg={this.state.errorMsg} />;
}
return this.props.children;
}
}