-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap <LoadableRenderer /> with <ErrorBoundary /> (#6294)
* Wrap <LoadableRenderer /> with <ErrorBoundary /> It appears that since the introduction of <SuperChart />, errors in the visualization javascript (which are somewhat common and expected, especially as we'll support plugins) were not handled and the whole page would throw and go missing. Here I'm introducing a new <ErrorBoundary /> component that elegantly wraps other components and handles errors. It's inspired by: https://reactjs.org/docs/error-boundaries.html The default behavior of the component is to simply surface the error as an <Alert bsStyle="danger" /> and exposes the React stacktrace when clicking on the error. It's also possible to use component and pass an onError handler and not show the default message. This also fixes some minor bugs in TimeTable. * Addressing comments
- Loading branch information
1 parent
a57603a
commit 4ce475f
Showing
6 changed files
with
72 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { t } from '@superset-ui/translation'; | ||
import StackTraceMessage from './StackTraceMessage'; | ||
|
||
const propTypes = { | ||
children: PropTypes.node.isRequired, | ||
onError: PropTypes.func, | ||
showMessage: PropTypes.bool, | ||
}; | ||
const defaultProps = { | ||
onError: () => {}, | ||
showMessage: true, | ||
}; | ||
|
||
export default class ErrorBoundary extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { error: null, info: null }; | ||
} | ||
|
||
componentDidCatch(error, info) { | ||
this.props.onError(error, info); | ||
this.setState({ error, info }); | ||
} | ||
|
||
render() { | ||
const { error, info } = this.state; | ||
if (error) { | ||
const firstLine = error ? error.toString() : null; | ||
const message = ( | ||
<span> | ||
<strong>{t('Unexpected error')}</strong>{firstLine ? `: ${firstLine}` : ''} | ||
</span>); | ||
if (this.props.showMessage) { | ||
return ( | ||
<StackTraceMessage message={message} stackTrace={info ? info.componentStack : null} />); | ||
} | ||
return null; | ||
} | ||
return this.props.children; | ||
} | ||
} | ||
ErrorBoundary.propTypes = propTypes; | ||
ErrorBoundary.defaultProps = defaultProps; |
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