error-boundary.tsx 792 B

123456789101112131415161718192021222324252627282930313233
  1. import { captureException } from './error-reporter'
  2. import {
  3. withErrorBoundary as rebWithErrorBoundary,
  4. FallbackProps,
  5. } from 'react-error-boundary'
  6. import { ComponentType, ErrorInfo } from 'react'
  7. function errorHandler(error: Error, errorInfo: ErrorInfo) {
  8. captureException(error, {
  9. extra: {
  10. componentStack: errorInfo.componentStack,
  11. },
  12. tags: {
  13. handler: 'react-error-boundary',
  14. },
  15. })
  16. }
  17. function DefaultFallbackComponent() {
  18. return <></>
  19. }
  20. function withErrorBoundary(
  21. WrappedComponent: ComponentType<any>,
  22. FallbackComponent?: ComponentType<FallbackProps>
  23. ) {
  24. return rebWithErrorBoundary(WrappedComponent, {
  25. onError: errorHandler,
  26. FallbackComponent: FallbackComponent || DefaultFallbackComponent,
  27. })
  28. }
  29. export default withErrorBoundary