Question: how to integrate Bugsnag in a custom React ErrorBoundary?
See original GitHub issueI use custom error boundaries in my application. They are used in different parts of the page, have a beautiful look and a «retry» button. The proposed Bugsnag approach to wrap the whole application in the Bugsnag’s error boundary won’t work in my case because the errors are already caught by my error boundaries.
function MyApp() {
return (
<div>
<MyErrorBoundary>
Column 1
</MyErrorBoundary>
<MyErrorBoundary>
Column 2
</MyErrorBoundary>
</div>
);
}
const ErrorBoundary = bugsnagClient.getPlugin('react')
ReactDOM.render(
<ErrorBoundary> {/* Will never catch */}
<MyApp />
</ErrorBoundary>,
document.getElementById('app')
)
So the question is: what is the correct way to add a Bugsnag error reporting to my custom error boundary component?
class MyErrorBoundary extends React.PureComponent {
state = {hasError: false};
static getDerivedStateFromError(error) {
return {hasError: true};
}
componentDidCatch(error, errorInfo) {
// What to type here?
// Maybe `bugsnagClient.notify(error)`? But how to send `errorInfo` too?
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return 'My custom pretty error display with a retry button';
}
return this.props.children;
}
retry() {
this.setState({hasError: false});
}
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Capturing render errors - React - Bugsnag docs
The Bugsnag ErrorBoundary allows you to capture React render errors in your application. You can also use the FallbackComponent to display a custom...
Read more >React integration guide - JavaScript - Bugsnag docs
The Bugsnag ErrorBoundary allows you to capture React render errors in your application. You can also use the FallbackComponent to display a custom...
Read more >React 16: What's new? Error handling! - Bugsnag
An error boundary is a React component with a componentDidCatch(err, info) method. Any errors occurring in a component tree get reported up ...
Read more >React Native integration guide - Bugsnag docs
The Bugsnag ErrorBoundary allows you to capture React render errors in your application. You can also use the FallbackComponent to display a custom...
Read more >Bugsnag docs › Platforms › JavaScript › Legacy › React
Install Bugsnag and the Bugsnag / React integration from the npm registry ... Use the provided error boundary to wrap your application (requires...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@bengourley Is exposing some kind of
notifyReactError
method something Bugsnag would be open to?I feel like I’m losing control implementing this with a proxied, blackboxed Bugsnag-flavoured ErrorBoundary and would rather just do
in a vanilla one and have the component stack formatted for me, by Bugsnag, instead of relying on @Finesse’s hack above, even though it seems nice enough.
Reading the docs, it’s also quite unclear what arguments are provided to
onError
and which props are provided toFallbackComponent
. Yes, it’s partially documented in this issue’s comments, but should be much easier to find.Finally, and possibly the biggest reason for why I want to roll my own ErrorBoundary, is due to the timing of my
Bugsnag.start()
call being async, as it depends on network calls resolving, as that’s how my web client gets a hold of the API key from that environment’s variables. The official way of wiring this up just doesn’t make much sense to me.Here is a version of the report function for Bugsnag v7: