Skip to content

Instantly share code, notes, and snippets.

@MatteoGioioso
Created February 3, 2020 01:16
Show Gist options
  • Save MatteoGioioso/a7c2564f06c18e08e74956d48a4d0088 to your computer and use it in GitHub Desktop.
Save MatteoGioioso/a7c2564f06c18e08e74956d48a4d0088 to your computer and use it in GitHub Desktop.
React error boundary component with Sentry integration
import React from "react";
import * as Sentry from "@sentry/browser";
Sentry.init({
dsn: process.env.REACT_APP_SENTRY_DNS
});
export default class ErrorBoundary extends React.Component {
state = {
hasError: false,
errorInfo: "",
error: ""
};
componentDidCatch(error, errorInfo) {
this.setState({
error,
errorInfo,
hasError: true
});
Sentry.withScope(scope => {
Object.keys(errorInfo).forEach(key => {
scope.setExtra(key, errorInfo[key]);
});
Sentry.captureException(error);
});
}
render() {
return this.state.hasError ? (
<div>
Something went wrong!
</div>
) : (
this.props.children
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment