error-boundary.jsx 943 B

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