{"version":3,"sources":["../../../../src/client/components/error-boundary.tsx"],"sourcesContent":["'use client'\n\nimport React, { startTransition, type JSX } from 'react'\nimport { useUntrackedPathname } from './navigation-untracked'\nimport { isNextRouterError } from './is-next-router-error'\nimport { handleHardNavError } from './nav-failure-handler'\nimport { handleISRError } from './handle-isr-error'\nimport { isBot } from '../../shared/lib/router/utils/is-bot'\nimport {\n  AppRouterContext,\n  type AppRouterInstance,\n} from '../../shared/lib/app-router-context.shared-runtime'\n\nconst isBotUserAgent =\n  typeof window !== 'undefined' && isBot(window.navigator.userAgent)\n\nexport type ErrorInfo = {\n  error: Error\n  reset: () => void\n  unstable_retry: () => void\n}\n\nexport type ErrorComponent = React.ComponentType<ErrorInfo>\n\nexport interface ErrorBoundaryProps {\n  children?: React.ReactNode\n  errorComponent: ErrorComponent | undefined\n  errorStyles?: React.ReactNode | undefined\n  errorScripts?: React.ReactNode | undefined\n}\n\ninterface ErrorBoundaryHandlerProps extends ErrorBoundaryProps {\n  pathname: string | null\n  errorComponent: ErrorComponent\n}\n\ninterface ErrorBoundaryHandlerState {\n  error: Error | null\n  previousPathname: string | null\n}\n\nexport class ErrorBoundaryHandler extends React.Component<\n  ErrorBoundaryHandlerProps,\n  ErrorBoundaryHandlerState\n> {\n  static contextType = AppRouterContext\n  declare context: AppRouterInstance | null\n\n  constructor(props: ErrorBoundaryHandlerProps) {\n    super(props)\n    this.state = {\n      error: null,\n      previousPathname: this.props.pathname,\n    }\n  }\n\n  static getDerivedStateFromError(error: Error) {\n    if (isNextRouterError(error)) {\n      // Re-throw if an expected internal Next.js router error occurs\n      // this means it should be handled by a different boundary (such as a NotFound boundary in a parent segment)\n      throw error\n    }\n\n    return { error }\n  }\n\n  static getDerivedStateFromProps(\n    props: ErrorBoundaryHandlerProps,\n    state: ErrorBoundaryHandlerState\n  ): ErrorBoundaryHandlerState | null {\n    const { error } = state\n\n    // if we encounter an error while\n    // a navigation is pending we shouldn't render\n    // the error boundary and instead should fallback\n    // to a hard navigation to attempt recovering\n    if (process.env.__NEXT_APP_NAV_FAIL_HANDLING) {\n      if (error && handleHardNavError(error)) {\n        // clear error so we don't render anything\n        return {\n          error: null,\n          previousPathname: props.pathname,\n        }\n      }\n    }\n\n    /**\n     * Handles reset of the error boundary when a navigation happens.\n     * Ensures the error boundary does not stay enabled when navigating to a new page.\n     * Approach of setState in render is safe as it checks the previous pathname and then overrides\n     * it as outlined in https://react.dev/reference/react/useState#storing-information-from-previous-renders\n     */\n    if (props.pathname !== state.previousPathname && state.error) {\n      return {\n        error: null,\n        previousPathname: props.pathname,\n      }\n    }\n    return {\n      error: state.error,\n      previousPathname: props.pathname,\n    }\n  }\n\n  reset = () => {\n    this.setState({ error: null })\n  }\n\n  unstable_retry = () => {\n    startTransition(() => {\n      this.context?.refresh()\n      this.reset()\n    })\n  }\n\n  // Explicit type is needed to avoid the generated `.d.ts` having a wide return type that could be specific to the `@types/react` version.\n  render(): React.ReactNode {\n    //When it's bot request, segment level error boundary will keep rendering the children,\n    // the final error will be caught by the root error boundary and determine wether need to apply graceful degrade.\n    if (this.state.error && !isBotUserAgent) {\n      handleISRError({ error: this.state.error })\n\n      return (\n        <>\n          {this.props.errorStyles}\n          {this.props.errorScripts}\n          <this.props.errorComponent\n            error={this.state.error}\n            reset={this.reset}\n            unstable_retry={this.unstable_retry}\n          />\n        </>\n      )\n    }\n\n    return this.props.children\n  }\n}\n\n/**\n * Handles errors through `getDerivedStateFromError`.\n * Renders the provided error component and provides a way to `reset` the error boundary state.\n */\n\n/**\n * Renders error boundary with the provided \"errorComponent\" property as the fallback.\n * If no \"errorComponent\" property is provided it renders the children without an error boundary.\n */\nexport function ErrorBoundary({\n  errorComponent,\n  errorStyles,\n  errorScripts,\n  children,\n}: ErrorBoundaryProps & {\n  children: React.ReactNode\n}): JSX.Element {\n  // When we're rendering the missing params shell, this will return null. This\n  // is because we won't be rendering any not found boundaries or error\n  // boundaries for the missing params shell. When this runs on the client\n  // (where these errors can occur), we will get the correct pathname.\n  const pathname = useUntrackedPathname()\n\n  if (errorComponent) {\n    return (\n      <ErrorBoundaryHandler\n        pathname={pathname}\n        errorComponent={errorComponent}\n        errorStyles={errorStyles}\n        errorScripts={errorScripts}\n      >\n        {children}\n      </ErrorBoundaryHandler>\n    )\n  }\n\n  return <>{children}</>\n}\n"],"names":["React","startTransition","useUntrackedPathname","isNextRouterError","handleHardNavError","handleISRError","isBot","AppRouterContext","isBotUserAgent","window","navigator","userAgent","ErrorBoundaryHandler","Component","contextType","constructor","props","reset","setState","error","unstable_retry","context","refresh","state","previousPathname","pathname","getDerivedStateFromError","getDerivedStateFromProps","process","env","__NEXT_APP_NAV_FAIL_HANDLING","render","errorStyles","errorScripts","this","errorComponent","children","ErrorBoundary"],"mappings":"AAAA;;AAEA,OAAOA,SAASC,eAAe,QAAkB,QAAO;AACxD,SAASC,oBAAoB,QAAQ,yBAAwB;AAC7D,SAASC,iBAAiB,QAAQ,yBAAwB;AAC1D,SAASC,kBAAkB,QAAQ,wBAAuB;AAC1D,SAASC,cAAc,QAAQ,qBAAoB;AACnD,SAASC,KAAK,QAAQ,uCAAsC;AAC5D,SACEC,gBAAgB,QAEX,qDAAoD;AAE3D,MAAMC,iBACJ,OAAOC,WAAW,eAAeH,MAAMG,OAAOC,SAAS,CAACC,SAAS;AA2BnE,OAAO,MAAMC,6BAA6BZ,MAAMa,SAAS;;aAIhDC,cAAcP;;IAGrBQ,YAAYC,KAAgC,CAAE;QAC5C,KAAK,CAACA,aAuDRC,QAAQ;YACN,IAAI,CAACC,QAAQ,CAAC;gBAAEC,OAAO;YAAK;QAC9B,QAEAC,iBAAiB;YACfnB,gBAAgB;gBACd,IAAI,CAACoB,OAAO,EAAEC;gBACd,IAAI,CAACL,KAAK;YACZ;QACF;QA/DE,IAAI,CAACM,KAAK,GAAG;YACXJ,OAAO;YACPK,kBAAkB,IAAI,CAACR,KAAK,CAACS,QAAQ;QACvC;IACF;IAEA,OAAOC,yBAAyBP,KAAY,EAAE;QAC5C,IAAIh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