Created
October 29, 2017 18:03
-
-
Save nickytonline/c758bcbc79e3ca435310bb847fae2c88 to your computer and use it in GitHub Desktop.
HOC with componentDidCatch
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
import React, { Component } from 'react'; | |
import Sentry from '../services/sentry'; | |
import DevError from '../components/DevError'; | |
const DEV_ENV = 'development'; | |
const CaptureError = ({ | |
ComponentToWrap, | |
ErrorComponent, | |
environment = process.env.NODE_ENV, | |
}) => | |
class extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { error: null }; | |
} | |
componentDidCatch(error, extra) { | |
Sentry.captureException(error, { extra }); | |
this.setState(previousState => ({ | |
...previousState, | |
error: { error, extra }, | |
})); | |
} | |
render() { | |
const { error } = this.state; | |
if (error !== null) { | |
return environment === DEV_ENV | |
? <DevError error={error} /> | |
: <ErrorComponent />; | |
} | |
return <ComponentToWrap {...this.props} />; | |
} | |
}; | |
export default CaptureError; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment