Commit 5cd92063 authored by devjiwonchoi's avatar devjiwonchoi

Revert componentStack and ownerStack from error.js

The ownerStack captured in the error boundary returns the error
boundary's owner stack instead of the errored component's.
Remove componentStack and ownerStack from error.js props to
address them separately with a correct capture strategy.
parent 02aca5c9
...@@ -8,9 +8,6 @@ export type GlobalErrorComponent = React.ComponentType<{ ...@@ -8,9 +8,6 @@ export type GlobalErrorComponent = React.ComponentType<{
error: any error: any
reset: () => void reset: () => void
unstable_retry: () => void unstable_retry: () => void
componentStack: React.ErrorInfo['componentStack']
/** Only available in dev builds; `null` otherwise. */
ownerStack: ReturnType<typeof React.captureOwnerStack>
}> }>
function DefaultGlobalError({ error }: { error: any }) { function DefaultGlobalError({ error }: { error: any }) {
......
...@@ -18,9 +18,6 @@ export type ErrorComponent = React.ComponentType<{ ...@@ -18,9 +18,6 @@ export type ErrorComponent = React.ComponentType<{
error: Error error: Error
reset: () => void reset: () => void
unstable_retry: () => void unstable_retry: () => void
componentStack: React.ErrorInfo['componentStack']
/** Only available in dev builds; `null` otherwise. */
ownerStack: ReturnType<typeof React.captureOwnerStack>
}> }>
export interface ErrorBoundaryProps { export interface ErrorBoundaryProps {
...@@ -38,8 +35,6 @@ interface ErrorBoundaryHandlerProps extends ErrorBoundaryProps { ...@@ -38,8 +35,6 @@ interface ErrorBoundaryHandlerProps extends ErrorBoundaryProps {
interface ErrorBoundaryHandlerState { interface ErrorBoundaryHandlerState {
error: Error | null error: Error | null
previousPathname: string | null previousPathname: string | null
componentStack: React.ErrorInfo['componentStack']
ownerStack: ReturnType<typeof React.captureOwnerStack>
} }
export class ErrorBoundaryHandler extends React.Component< export class ErrorBoundaryHandler extends React.Component<
...@@ -54,8 +49,6 @@ export class ErrorBoundaryHandler extends React.Component< ...@@ -54,8 +49,6 @@ export class ErrorBoundaryHandler extends React.Component<
this.state = { this.state = {
error: null, error: null,
previousPathname: this.props.pathname, previousPathname: this.props.pathname,
componentStack: undefined,
ownerStack: null,
} }
} }
...@@ -66,18 +59,7 @@ export class ErrorBoundaryHandler extends React.Component< ...@@ -66,18 +59,7 @@ export class ErrorBoundaryHandler extends React.Component<
throw error throw error
} }
let ownerStack: string | null = null return { error }
if ('captureOwnerStack' in React) {
ownerStack = React.captureOwnerStack()
}
return { error, ownerStack }
}
componentDidCatch(_error: Error, errorInfo: React.ErrorInfo): void {
this.setState({
componentStack: errorInfo.componentStack,
})
} }
static getDerivedStateFromProps( static getDerivedStateFromProps(
...@@ -96,8 +78,6 @@ export class ErrorBoundaryHandler extends React.Component< ...@@ -96,8 +78,6 @@ export class ErrorBoundaryHandler extends React.Component<
return { return {
error: null, error: null,
previousPathname: props.pathname, previousPathname: props.pathname,
componentStack: undefined,
ownerStack: null,
} }
} }
} }
...@@ -112,24 +92,16 @@ export class ErrorBoundaryHandler extends React.Component< ...@@ -112,24 +92,16 @@ export class ErrorBoundaryHandler extends React.Component<
return { return {
error: null, error: null,
previousPathname: props.pathname, previousPathname: props.pathname,
componentStack: undefined,
ownerStack: null,
} }
} }
return { return {
error: state.error, error: state.error,
previousPathname: props.pathname, previousPathname: props.pathname,
componentStack: state.componentStack,
ownerStack: state.ownerStack,
} }
} }
reset = () => { reset = () => {
this.setState({ this.setState({ error: null })
error: null,
componentStack: undefined,
ownerStack: null,
})
} }
unstable_retry = () => { unstable_retry = () => {
...@@ -153,8 +125,6 @@ export class ErrorBoundaryHandler extends React.Component< ...@@ -153,8 +125,6 @@ export class ErrorBoundaryHandler extends React.Component<
error={this.state.error} error={this.state.error}
reset={this.reset} reset={this.reset}
unstable_retry={this.unstable_retry} unstable_retry={this.unstable_retry}
componentStack={this.state.componentStack}
ownerStack={this.state.ownerStack}
/> />
</> </>
) )
......
...@@ -17,9 +17,6 @@ type AppDevOverlayErrorBoundaryProps = { ...@@ -17,9 +17,6 @@ type AppDevOverlayErrorBoundaryProps = {
type AppDevOverlayErrorBoundaryState = { type AppDevOverlayErrorBoundaryState = {
reactError: unknown reactError: unknown
componentStack: React.ErrorInfo['componentStack']
/** Only available in dev builds; `null` otherwise. */
ownerStack: ReturnType<typeof React.captureOwnerStack>
} }
function ErroredHtml({ function ErroredHtml({
...@@ -27,15 +24,11 @@ function ErroredHtml({ ...@@ -27,15 +24,11 @@ function ErroredHtml({
error, error,
reset, reset,
unstable_retry, unstable_retry,
componentStack,
ownerStack,
}: { }: {
globalError: GlobalErrorState globalError: GlobalErrorState
error: unknown error: unknown
reset: () => void reset: () => void
unstable_retry: () => void unstable_retry: () => void
componentStack: React.ErrorInfo['componentStack']
ownerStack: ReturnType<typeof React.captureOwnerStack>
}) { }) {
if (!error) { if (!error) {
return ( return (
...@@ -52,8 +45,6 @@ function ErroredHtml({ ...@@ -52,8 +45,6 @@ function ErroredHtml({
error={error} error={error}
reset={reset} reset={reset}
unstable_retry={unstable_retry} unstable_retry={unstable_retry}
componentStack={componentStack}
ownerStack={ownerStack}
/> />
</ErrorBoundary> </ErrorBoundary>
) )
...@@ -68,34 +59,23 @@ export class AppDevOverlayErrorBoundary extends PureComponent< ...@@ -68,34 +59,23 @@ export class AppDevOverlayErrorBoundary extends PureComponent<
state: AppDevOverlayErrorBoundaryState = { state: AppDevOverlayErrorBoundaryState = {
reactError: null, reactError: null,
componentStack: undefined,
ownerStack: null,
} }
static getDerivedStateFromError(error: Error) { static getDerivedStateFromError(error: Error) {
RuntimeErrorHandler.hadRuntimeError = true RuntimeErrorHandler.hadRuntimeError = true
let ownerStack: string | null = null
if ('captureOwnerStack' in React) {
ownerStack = React.captureOwnerStack()
}
return { return {
reactError: error, reactError: error,
ownerStack,
} }
} }
componentDidCatch(err: Error, errorInfo: React.ErrorInfo) { componentDidCatch(err: Error) {
if ( if (
process.env.NODE_ENV === 'development' && process.env.NODE_ENV === 'development' &&
err.message === SEGMENT_EXPLORER_SIMULATED_ERROR_MESSAGE err.message === SEGMENT_EXPLORER_SIMULATED_ERROR_MESSAGE
) { ) {
return return
} }
this.setState({
componentStack: errorInfo.componentStack,
})
dispatcher.openErrorOverlay() dispatcher.openErrorOverlay()
} }
...@@ -107,16 +87,12 @@ export class AppDevOverlayErrorBoundary extends PureComponent< ...@@ -107,16 +87,12 @@ export class AppDevOverlayErrorBoundary extends PureComponent<
} }
reset = () => { reset = () => {
this.setState({ this.setState({ reactError: null })
reactError: null,
componentStack: undefined,
ownerStack: null,
})
} }
render() { render() {
const { children, globalError } = this.props const { children, globalError } = this.props
const { reactError, componentStack, ownerStack } = this.state const { reactError } = this.state
const fallback = ( const fallback = (
<ErroredHtml <ErroredHtml
...@@ -124,8 +100,6 @@ export class AppDevOverlayErrorBoundary extends PureComponent< ...@@ -124,8 +100,6 @@ export class AppDevOverlayErrorBoundary extends PureComponent<
error={reactError} error={reactError}
reset={this.reset} reset={this.reset}
unstable_retry={this.unstable_retry} unstable_retry={this.unstable_retry}
componentStack={componentStack}
ownerStack={ownerStack}
/> />
) )
......
...@@ -2,18 +2,10 @@ ...@@ -2,18 +2,10 @@
import styles from './style.module.css' import styles from './style.module.css'
export default function ErrorBoundary({ export default function ErrorBoundary({ error, reset, unstable_retry }) {
error,
reset,
unstable_retry,
componentStack,
ownerStack,
}) {
return ( return (
<> <>
<p id="error-boundary-message">An error occurred: {error.message}</p> <p id="error-boundary-message">An error occurred: {error.message}</p>
<p id="error-component-stack">{componentStack}</p>
<p id="error-owner-stack">{ownerStack}</p>
<button id="reset" onClick={() => reset()} className={styles.button}> <button id="reset" onClick={() => reset()} className={styles.button}>
Try again Try again
</button> </button>
......
'use client' 'use client'
export default function ErrorBoundary({ export default function ErrorBoundary({ error, reset, unstable_retry }) {
error,
reset,
unstable_retry,
componentStack,
ownerStack,
}) {
return ( return (
<> <>
<p id="error-boundary-message">{error.message}</p> <p id="error-boundary-message">{error.message}</p>
<p id="error-boundary-digest">{error.digest}</p> <p id="error-boundary-digest">{error.digest}</p>
<p id="error-component-stack">{componentStack}</p>
<p id="error-owner-stack">{ownerStack}</p>
<button id="reset" onClick={() => reset()}> <button id="reset" onClick={() => reset()}>
Try again Try again
</button> </button>
......
...@@ -2,15 +2,6 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -2,15 +2,6 @@ import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
/**
* Normalize a React component stack string into an array of component names.
* Strips URLs, line numbers, and other dynamic parts.
*/
function normalizeComponentStack(stack: string): string[] {
// Match "at ComponentName" patterns, optionally with [Server] tag
return stack.match(/at \w+(?:\s+\[Server\])?/g) || []
}
describe('app-dir - errors', () => { describe('app-dir - errors', () => {
const { next, isNextDev, isNextStart, skipped } = nextTestSetup({ const { next, isNextDev, isNextStart, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
...@@ -254,83 +245,6 @@ describe('app-dir - errors', () => { ...@@ -254,83 +245,6 @@ describe('app-dir - errors', () => {
]) ])
}) })
it('should pass componentStack and ownerStack to error component', async () => {
const browser = await next.browser('/client-component')
await browser
.elementByCss('#error-trigger-button')
.click()
.waitForElementByCss('#error-component-stack')
if (isNextDev) {
expect(
normalizeComponentStack(
await browser.elementByCss('#error-component-stack').text()
)
).toMatchInlineSnapshot(`
[
"at Page",
"at ClientPageRoot",
"at SegmentViewNode",
"at InnerLayoutRouter",
"at RedirectErrorBoundary",
"at RedirectBoundary",
"at HTTPAccessFallbackBoundary",
"at LoadingBoundary",
"at ErrorBoundaryHandler",
"at ErrorBoundary",
"at InnerScrollAndFocusHandlerOld",
"at ScrollAndFocusHandler",
"at RenderFromTemplateContext",
"at SegmentStateProvider",
"at OuterLayoutRouter",
"at InnerLayoutRouter",
"at RedirectErrorBoundary",
"at RedirectBoundary",
"at HTTPAccessFallbackErrorBoundary",
"at HTTPAccessFallbackBoundary",
"at LoadingBoundary",
"at ErrorBoundary",
"at InnerScrollAndFocusHandlerOld",
"at ScrollAndFocusHandler",
"at RenderFromTemplateContext",
"at SegmentStateProvider",
"at OuterLayoutRouter",
"at body",
"at html",
"at RootLayout [Server]",
"at SegmentViewNode",
"at __next_root_layout_boundary__",
"at RedirectErrorBoundary",
"at RedirectBoundary",
"at HTTPAccessFallbackErrorBoundary",
"at HTTPAccessFallbackBoundary",
"at DevRootHTTPAccessFallbackBoundary",
"at AppDevOverlayErrorBoundary",
"at HotReload",
"at Router",
"at ErrorBoundaryHandler",
"at ErrorBoundary",
"at RootErrorBoundary",
"at AppRouter",
"at ServerRoot",
"at Root",
]
`)
expect(
normalizeComponentStack(
await browser.elementByCss('#error-owner-stack').text()
)
).toMatchInlineSnapshot(`
[
"at ErrorBoundary",
"at OuterLayoutRouter",
]
`)
}
})
// production tests // production tests
if (isNextStart) { if (isNextStart) {
it('should allow resetting error boundary', async () => { it('should allow resetting error boundary', async () => {
...@@ -425,88 +339,6 @@ describe('app-dir - errors', () => { ...@@ -425,88 +339,6 @@ describe('app-dir - errors', () => {
: 'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.' : 'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.'
) )
if (isNextDev) {
expect(
normalizeComponentStack(
await browser.elementByCss('#error-component-stack').text()
)
).toMatchInlineSnapshot(`
[
"at Content",
"at div",
"at Container [Server]",
"at Suspense",
"at Page",
"at SegmentViewNode",
"at InnerLayoutRouter",
"at RedirectErrorBoundary",
"at RedirectBoundary",
"at HTTPAccessFallbackBoundary",
"at LoadingBoundary",
"at ErrorBoundary",
"at InnerScrollAndFocusHandlerOld",
"at ScrollAndFocusHandler",
"at RenderFromTemplateContext",
"at SegmentStateProvider",
"at OuterLayoutRouter",
"at InnerLayoutRouter",
"at RedirectErrorBoundary",
"at RedirectBoundary",
"at HTTPAccessFallbackBoundary",
"at LoadingBoundary",
"at ErrorBoundaryHandler",
"at ErrorBoundary",
"at InnerScrollAndFocusHandlerOld",
"at ScrollAndFocusHandler",
"at RenderFromTemplateContext",
"at SegmentStateProvider",
"at OuterLayoutRouter",
"at InnerLayoutRouter",
"at RedirectErrorBoundary",
"at RedirectBoundary",
"at HTTPAccessFallbackErrorBoundary",
"at HTTPAccessFallbackBoundary",
"at LoadingBoundary",
"at ErrorBoundary",
"at InnerScrollAndFocusHandlerOld",
"at ScrollAndFocusHandler",
"at RenderFromTemplateContext",
"at SegmentStateProvider",
"at OuterLayoutRouter",
"at body",
"at html",
"at RootLayout [Server]",
"at SegmentViewNode",
"at __next_root_layout_boundary__",
"at RedirectErrorBoundary",
"at RedirectBoundary",
"at HTTPAccessFallbackErrorBoundary",
"at HTTPAccessFallbackBoundary",
"at DevRootHTTPAccessFallbackBoundary",
"at AppDevOverlayErrorBoundary",
"at HotReload",
"at Router",
"at ErrorBoundaryHandler",
"at ErrorBoundary",
"at RootErrorBoundary",
"at AppRouter",
"at ServerRoot",
"at Root",
]
`)
expect(
normalizeComponentStack(
await browser.elementByCss('#error-owner-stack').text()
)
).toMatchInlineSnapshot(`
[
"at ErrorBoundary",
"at OuterLayoutRouter",
]
`)
}
// Signal the server to stop throwing before retrying // Signal the server to stop throwing before retrying
await next.fetch('/server-component/recover/set-recover') await next.fetch('/server-component/recover/set-recover')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment