error-boundary.js 974 B

12345678910111213141516171819202122232425262728293031323334
  1. import { captureException } from './error-reporter'
  2. import { ErrorBoundary } from 'react-error-boundary'
  3. function errorHandler(error, componentStack) {
  4. captureException(error, scope => {
  5. scope.setExtra('componentStack', componentStack)
  6. scope.setTag('handler', 'react-error-boundary')
  7. return scope
  8. })
  9. }
  10. function DefaultFallbackComponent() {
  11. return <></>
  12. }
  13. function withErrorBoundary(WrappedComponent, FallbackComponent) {
  14. function ErrorBoundaryWrapper(props) {
  15. return (
  16. <ErrorBoundary
  17. FallbackComponent={FallbackComponent || DefaultFallbackComponent}
  18. onError={errorHandler}
  19. >
  20. <WrappedComponent {...props} />
  21. </ErrorBoundary>
  22. )
  23. }
  24. ErrorBoundaryWrapper.propTypes = WrappedComponent.propTypes
  25. ErrorBoundaryWrapper.displayName = `WithErrorBoundaryWrapper${
  26. WrappedComponent.displayName || WrappedComponent.name || 'Component'
  27. }`
  28. return ErrorBoundaryWrapper
  29. }
  30. export default withErrorBoundary