-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(front): error boundary (#76)
- Loading branch information
Showing
9 changed files
with
148 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 42 additions & 15 deletions
57
apps/js-machine-front/src/app/components/ErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,52 @@ | ||
import * as React from 'react'; | ||
import { ErrorIndicator } from './ErrorIndicator'; | ||
import React, { ReactNode, ComponentType, ErrorInfo } from 'react'; | ||
import { ErrorBoundaryFallbackComponent } from './ErrorBoundaryFallbackComponent'; | ||
|
||
export interface ErrorBoundaryState { | ||
hasError: boolean; | ||
interface Props { | ||
children?: ReactNode; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
FallbackComponent: ComponentType<any>; | ||
onError?: (error: Error, componentStack: string) => void; | ||
} | ||
|
||
export class ErrorBoundary extends React.Component<{}, ErrorBoundaryState> { | ||
state: ErrorBoundaryState = { | ||
hasError: false, | ||
interface State { | ||
error?: Error; | ||
info?: ErrorInfo; | ||
} | ||
|
||
export class ErrorBoundary extends React.Component<Props, State> { | ||
static defaultProps: Props = { | ||
FallbackComponent: ErrorBoundaryFallbackComponent, | ||
}; | ||
|
||
componentDidCatch(): void { | ||
this.setState({ | ||
hasError: true, | ||
}); | ||
public state: State = {}; | ||
|
||
public componentDidCatch(error: Error, info: ErrorInfo): void { | ||
const { onError } = this.props; | ||
|
||
if (typeof onError === 'function') { | ||
try { | ||
onError.call(this, error, info ? info.componentStack : ''); | ||
} catch (ignoredError) { | ||
// catch error | ||
} | ||
} | ||
|
||
this.setState({ error, info }); | ||
} | ||
|
||
render(): React.ReactNode | React.FC { | ||
if (this.state.hasError) { | ||
return <ErrorIndicator />; | ||
public render(): React.ReactNode | React.FC { | ||
const { children, FallbackComponent } = this.props; | ||
const { error, info } = this.state; | ||
|
||
if (error) { | ||
return ( | ||
<FallbackComponent | ||
componentStack={info ? info.componentStack : ''} | ||
error={error} | ||
/> | ||
); | ||
} | ||
return this.props.children; | ||
|
||
return children || null; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
apps/js-machine-front/src/app/components/ErrorBoundaryFallbackComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { memo } from 'react'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
root: { | ||
height: '100vh', | ||
backgroundImage: `url('assets/error.svg')`, | ||
backgroundRepeat: 'no-repeat', | ||
backgroundPosition: 'center', | ||
backgroundColor: `rgba(0,0,0,0.6)`, | ||
}, | ||
toolbar: theme.mixins.toolbar, | ||
textContainer: { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
marginLeft: theme.spacing(8), | ||
[theme.breakpoints.down('sm')]: { | ||
marginLeft: theme.spacing(6), | ||
}, | ||
[theme.breakpoints.down('xs')]: { | ||
marginLeft: theme.spacing(4), | ||
}, | ||
}, | ||
title: { | ||
color: '#F2E14C', | ||
fontWeight: 'bold', | ||
marginBottom: '1rem', | ||
fontSize: '6rem', | ||
[theme.breakpoints.down('sm')]: { | ||
fontSize: '5rem', | ||
}, | ||
[theme.breakpoints.down('xs')]: { | ||
fontSize: '4rem', | ||
}, | ||
}, | ||
subTitle: { | ||
color: theme.palette.primary.contrastText, | ||
fontSize: '1.125rem', | ||
fontWeight: 'bold', | ||
marginBottom: '1rem', | ||
}, | ||
})); | ||
|
||
interface Props { | ||
error: Error; | ||
componentStack: string; | ||
} | ||
|
||
export const ErrorBoundaryFallbackComponent = memo( | ||
function ErrorBoundaryFallbackComponent({ error, componentStack }: Props) { | ||
const classes = useStyles(); | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<div className={classes.toolbar} /> | ||
<div className={classes.textContainer}> | ||
<span className={classes.title}> | ||
<FormattedMessage id="errorIndicator.title" /> | ||
</span> | ||
<span className={classes.subTitle}> | ||
<FormattedMessage id="errorIndicator.subTitle" /> | ||
</span> | ||
<span className={classes.subTitle}> | ||
<FormattedMessage id="errorIndicator.subTitle1" /> | ||
</span> | ||
</div> | ||
</div> | ||
); | ||
}, | ||
); |
33 changes: 0 additions & 33 deletions
33
apps/js-machine-front/src/app/components/ErrorIndicator.tsx
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
apps/js-machine-front/src/app/components/styles/error-indicator.css
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
apps/js-machine-front/src/app/i18n/translations/en-US/error-indicator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export const errorIndicator = { | ||
'errorIndicator.title': 'OOPS!', | ||
'errorIndicator.subTitle': 'Something went wrong', | ||
'errorIndicator.subTitle1': 'but we have already sent a guy from support to fix it', | ||
'errorIndicator.title': 'Ooops...', | ||
'errorIndicator.subTitle': 'Something went wrong', | ||
'errorIndicator.subTitle1': 'Try to reload page or comeback later', | ||
}; |
7 changes: 4 additions & 3 deletions
7
apps/js-machine-front/src/app/i18n/translations/ru-RU/error-indicator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export const errorIndicator = { | ||
'errorIndicator.title': 'УПС!', | ||
'errorIndicator.subTitle': 'что-то пошло не так', | ||
'errorIndicator.subTitle1': 'но мы уже отправили парня из поддержки починить это', | ||
'errorIndicator.title': 'Oops...', | ||
'errorIndicator.subTitle': 'Что-то пошло не так', | ||
'errorIndicator.subTitle1': | ||
'Попробуйте обновить страницу или вернитесь позже', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes