Unverified Commit 922f0e92 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[dev-overlay] do not attach hydration info to non hydration errors (#76349)

parent 5b420d2b
...@@ -9,12 +9,18 @@ import { ...@@ -9,12 +9,18 @@ import {
} from './hydration-error-info' } from './hydration-error-info'
export function attachHydrationErrorState(error: Error) { export function attachHydrationErrorState(error: Error) {
const reactHydrationDiffSegments = getReactHydrationDiffSegments(
error.message
)
let parsedHydrationErrorState: typeof hydrationErrorState = {} let parsedHydrationErrorState: typeof hydrationErrorState = {}
const isHydrationWarning = testReactHydrationWarning(error.message) const isHydrationWarning = testReactHydrationWarning(error.message)
const isHydrationRuntimeError = isHydrationError(error) const isHydrationRuntimeError = isHydrationError(error)
// If it's not hydration warnings or errors, skip
if (!(isHydrationRuntimeError || isHydrationWarning)) {
return
}
const reactHydrationDiffSegments = getReactHydrationDiffSegments(
error.message
)
// If the reactHydrationDiffSegments exists // If the reactHydrationDiffSegments exists
// and the diff (reactHydrationDiffSegments[1]) exists // and the diff (reactHydrationDiffSegments[1]) exists
// e.g. the hydration diff log error. // e.g. the hydration diff log error.
...@@ -72,5 +78,6 @@ export function attachHydrationErrorState(error: Error) { ...@@ -72,5 +78,6 @@ export function attachHydrationErrorState(error: Error) {
hydrationErrorState.reactOutputComponentDiff hydrationErrorState.reactOutputComponentDiff
} }
} }
// If it's a hydration error, store the hydration error state into the error object
;(error as any).details = parsedHydrationErrorState ;(error as any).details = parsedHydrationErrorState
} }
'use client'
import { useEffect } from 'react'
export default function Page() {
useEffect(() => {
throw new Error('runtime error')
}, [])
return (
<p>
sneaky <p>very sneaky</p>
</p>
)
}
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
goToNextErrorView, goToNextErrorView,
getRedboxDescriptionWarning, getRedboxDescriptionWarning,
getHighlightedDiffLines, getHighlightedDiffLines,
assertHasRedbox,
} from 'next-test-utils' } from 'next-test-utils'
import { BrowserInterface } from 'next-webdriver' import { BrowserInterface } from 'next-webdriver'
...@@ -131,6 +132,47 @@ describe('hydration-error-count', () => { ...@@ -131,6 +132,47 @@ describe('hydration-error-count', () => {
} }
`) `)
}) })
it('should display runtime error separately from hydration errors', async () => {
const browser = await next.browser('/hydration-with-runtime-errors')
await assertHasRedbox(browser)
expect(await getToastErrorCount(browser)).toBe(3)
// Move to the last hydration error
await goToNextErrorView(browser)
expect(browser).toDisplayRedbox(`
{
"count": 3,
"description": "In HTML, <p> cannot be a descendant of <p>.
This will cause a hydration error.",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "app/hydration-with-runtime-errors/page.tsx (12:14) @ Page
> 12 | sneaky <p>very sneaky</p>
| ^",
"stack": [
"p <anonymous> (0:0)",
"Page app/hydration-with-runtime-errors/page.tsx (12:14)",
],
}
`)
await goToNextErrorView(browser)
expect(browser).toDisplayRedbox(`
{
"count": 3,
"description": "Error: runtime error",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "app/hydration-with-runtime-errors/page.tsx (7:11) @ Page.useEffect
> 7 | throw new Error('runtime error')
| ^",
"stack": [
"Page.useEffect app/hydration-with-runtime-errors/page.tsx (7:11)",
],
}
`)
})
}) })
async function getHydrationErrorSnapshot(browser: BrowserInterface) { async function getHydrationErrorSnapshot(browser: BrowserInterface) {
......
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