overleaf/services/web/frontend/js/infrastructure/error-boundary.js
Shane Kilkelly 95352894a5 Merge pull request #3137 from overleaf/msm-react-error-boundary
Add react-error-boundary to handle react errors in scope

GitOrigin-RevId: 9bd8261057122fce08d8cea3b19b2c71e1d949f4
2020-09-17 02:04:33 +00:00

31 lines
857 B
JavaScript

import React from 'react'
import { ErrorBoundary } from 'react-error-boundary'
function errorHandler(error, componentStack) {
if (window.Raven) {
Raven.captureException(error, {
extra: { componentStack },
tags: { mechanism: 'react-error-boundary' }
})
}
}
function withErrorBoundary(WrappedComponent, FallbackComponent) {
function ErrorBoundaryWrapper(props) {
return (
<ErrorBoundary
fallbackRender={FallbackComponent || null}
onError={errorHandler}
>
<WrappedComponent {...props} />
</ErrorBoundary>
)
}
ErrorBoundaryWrapper.propTypes = WrappedComponent.propTypes
ErrorBoundaryWrapper.displayName = `WithErrorBoundaryWrapper${WrappedComponent.displayName ||
WrappedComponent.name ||
'Component'}`
return ErrorBoundaryWrapper
}
export default withErrorBoundary