{"version":3,"file":"errorBoundary.mjs","names":[],"sources":["../../../src/domain/error/errorBoundary.ts"],"sourcesContent":["import React from 'react'\nimport type { ErrorInfo } from 'react'\nimport { addReactError } from './addReactError'\n\nexport interface ErrorBoundaryProps {\n  fallback: ErrorBoundaryFallback\n  children: React.ReactNode\n}\n\nexport type ErrorBoundaryFallback = React.ComponentType<{ error: Error; resetError: () => void }>\n\nexport type State =\n  | {\n      didCatch: false\n      error: null\n    }\n  | {\n      didCatch: true\n      error: Error\n    }\n\nconst INITIAL_STATE: State = { didCatch: false, error: null }\n\nexport function createErrorBoundary(\n  reportError: (error: Error, errorInfo: ErrorInfo) => void,\n  displayName = 'ErrorBoundary'\n) {\n  const DatadogErrorBoundary: React.ComponentClass<ErrorBoundaryProps> = class DatadogErrorBoundary extends React.Component<\n    ErrorBoundaryProps,\n    State\n  > {\n    constructor(props: ErrorBoundaryProps) {\n      super(props)\n      this.state = INITIAL_STATE\n    }\n\n    static getDerivedStateFromError(error: Error): State {\n      return { didCatch: true, error }\n    }\n\n    resetError = () => {\n      this.setState(INITIAL_STATE)\n    }\n\n    componentDidCatch(error: Error, errorInfo: ErrorInfo) {\n      reportError(error, errorInfo)\n    }\n\n    render() {\n      if (this.state.didCatch) {\n        return React.createElement(this.props.fallback, {\n          error: this.state.error,\n          resetError: this.resetError,\n        })\n      }\n\n      return this.props.children\n    }\n  }\n\n  DatadogErrorBoundary.displayName = displayName\n\n  return DatadogErrorBoundary\n}\n\n/**\n * ErrorBoundary component to report React errors to Datadog.\n *\n * For more advanced error handling, you can use the {@link addReactError} function.\n *\n * @category Error\n * @example\n * ```ts\n * import { ErrorBoundary } from '@datadog/browser-rum-react'\n *\n * <ErrorBoundary fallback={() => null}>\n *   <Component />\n * </ErrorBoundary>\n * ```\n */\n// eslint-disable-next-line local-rules/disallow-side-effects\nexport const ErrorBoundary = createErrorBoundary(addReactError)\n"],"mappings":";;;AAqBA,MAAM,gBAAuB;CAAE,UAAU;CAAO,OAAO;AAAK;AAE5D,SAAgB,oBACd,aACA,cAAc,iBACd;CACA,MAAM,uBAAiE,MAAM,6BAA6B,MAAM,UAG9G;EACA,YAAY,OAA2B;GACrC,MAAM,KAAK;GAQM,KAAA,mBAAA;IACjB,KAAK,SAAS,aAAa;GAC7B;GATE,KAAK,QAAQ;EACf;EAEA,OAAO,yBAAyB,OAAqB;GACnD,OAAO;IAAE,UAAU;IAAM;GAAM;EACjC;EAMA,kBAAkB,OAAc,WAAsB;GACpD,YAAY,OAAO,SAAS;EAC9B;EAEA,SAAS;GACP,IAAI,KAAK,MAAM,UACb,OAAO,MAAM,cAAc,KAAK,MAAM,UAAU;IAC9C,OAAO,KAAK,MAAM;IAClB,YAAY,KAAK;GACnB,CAAC;GAGH,OAAO,KAAK,MAAM;EACpB;CACF;CAEA,qBAAqB,cAAc;CAEnC,OAAO;AACT;;;;;;;;;;;;;;;;AAkBA,MAAa,gBAAgB,oBAAoB,aAAa"}