error-boundary.tsx 853 B

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