Created
September 21, 2022 19:16
-
-
Save ghankerson/f18b1efedf596cddca1019726d628d0d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import React, { ReactNode } from 'react'; | |
| import { Notifier } from '@airbrake/browser'; | |
| interface Props { | |
| fallbackUI?: ReactNode; | |
| } | |
| interface StateProps { | |
| hasError: boolean; | |
| } | |
| class ErrorBoundary extends React.Component<Props, StateProps> { | |
| constructor(props: Props) { | |
| super(props); | |
| this.state = { hasError: false }; | |
| this.airbrake = new Notifier({ | |
| projectId: 1, | |
| projectKey: 'FIXME' | |
| }); | |
| } | |
| static getDerivedStateFromError(error: Error): StateProps { | |
| // Update state so the next render will show the fallback UI. | |
| console.error(error); | |
| return { hasError: true }; | |
| } | |
| componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void { | |
| // React already creates a console error, similar to the commented-out code below | |
| this.airbrake.notify({ | |
| error: error, | |
| params: { | |
| info: errorInfo | |
| } | |
| }); | |
| console.error(error, errorInfo.componentStack); | |
| } | |
| render(): ReactNode { | |
| if (this.state.hasError && this.props.fallbackUI) { | |
| return this.props.fallbackUI; | |
| } | |
| return this.props.children; | |
| } | |
| } | |
| export default ErrorBoundary; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment