-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: error boundary message panel (#587)
* feat: error boundary message panel * chore: add isChrome test
- Loading branch information
zuozhuo
authored
Apr 23, 2022
1 parent
36449c5
commit 7b0c375
Showing
4 changed files
with
104 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable react/destructuring-assignment, react/state-in-constructor */ | ||
import React from 'react'; | ||
|
||
import { Box, Button, Typography } from '@onekeyhq/components'; | ||
import platformEnv from '@onekeyhq/shared/src/platformEnv'; | ||
|
||
type ErrorBoundaryProps = { | ||
onError?: (error: Error, componentStack: string | null) => void; | ||
}; | ||
type ErrorBoundaryState = { error: Error | null }; | ||
|
||
class ErrorBoundary extends React.PureComponent< | ||
ErrorBoundaryProps, | ||
ErrorBoundaryState | ||
> { | ||
state: { error: Error | null } = { error: null }; | ||
|
||
componentDidCatch( | ||
error: Error, | ||
// Loosely typed because it depends on the React version and was | ||
// accidentally excluded in some versions. | ||
errorInfo?: { componentStack?: string | null }, | ||
) { | ||
this.props?.onError?.(error, errorInfo?.componentStack || null); | ||
this.setState({ error }); | ||
} | ||
|
||
render() { | ||
if (platformEnv.isDev && this.state.error) { | ||
return ( | ||
// The component has to be unmounted or else it would continue to error | ||
<Box flex="1" bg="background-default" px={4} py={8}> | ||
<Button | ||
onPress={() => { | ||
if (platformEnv.isBrowser) { | ||
window.location.href = '#/'; | ||
window.location.reload(); | ||
} | ||
}} | ||
> | ||
Back to HOME | ||
</Button> | ||
<Typography.PageHeading> | ||
Error: {this.state.error?.name} | ||
</Typography.PageHeading> | ||
<Typography.Body1Strong> | ||
{this.state.error?.message} | ||
</Typography.Body1Strong> | ||
<Typography.Caption>{this.state.error?.stack}</Typography.Caption> | ||
</Box> | ||
); | ||
} | ||
return this.props.children; | ||
} | ||
} | ||
|
||
export { ErrorBoundary }; |
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
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
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