-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error boundary to catch errors in visualizations (#4518)
* Add error boundary to catch errors in visualizations * Fix: Funnel crash when step column is date/time * CR1 * CR2
- Loading branch information
1 parent
260bfca
commit fc9e8fe
Showing
5 changed files
with
178 additions
and
49 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,71 @@ | ||
import { isFunction } from "lodash"; | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import debug from "debug"; | ||
import Alert from "antd/lib/alert"; | ||
|
||
const logger = debug("redash:errors"); | ||
|
||
export const ErrorBoundaryContext = React.createContext({ | ||
handleError: error => { | ||
throw error; | ||
}, | ||
reset: () => {}, | ||
}); | ||
|
||
export function ErrorMessage({ children }) { | ||
return <Alert message={children} type="error" showIcon />; | ||
} | ||
|
||
ErrorMessage.propTypes = { | ||
children: PropTypes.node, | ||
}; | ||
|
||
ErrorMessage.defaultProps = { | ||
children: "Something went wrong.", | ||
}; | ||
|
||
export default class ErrorBoundary extends React.Component { | ||
static propTypes = { | ||
children: PropTypes.node, | ||
renderError: PropTypes.func, // error => ReactNode | ||
}; | ||
|
||
static defaultProps = { | ||
children: null, | ||
renderError: null, | ||
}; | ||
|
||
state = { error: null }; | ||
|
||
handleError = error => { | ||
this.setState(this.constructor.getDerivedStateFromError(error)); | ||
this.componentDidCatch(error, null); | ||
}; | ||
|
||
reset = () => { | ||
this.setState({ error: null }); | ||
}; | ||
|
||
static getDerivedStateFromError(error) { | ||
return { error }; | ||
} | ||
|
||
componentDidCatch(error, errorInfo) { | ||
logger(error, errorInfo); | ||
} | ||
|
||
render() { | ||
const { renderError, children } = this.props; | ||
const { error } = this.state; | ||
|
||
if (error) { | ||
if (isFunction(renderError)) { | ||
return renderError(error); | ||
} | ||
return <ErrorMessage />; | ||
} | ||
|
||
return <ErrorBoundaryContext.Provider value={this}>{children}</ErrorBoundaryContext.Provider>; | ||
} | ||
} |
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
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