Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
871ca31e
Unverified
Commit
871ca31e
authored
Feb 24, 2025
by
Sebastian "Sebbie" Silbermann
Committed by
GitHub
Feb 24, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[app] Send errors not handled by explicit error boundaries through `reportError` (#76101)
parent
0dc87f71
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
273 additions
and
209 deletions
+273
-209
app-index.tsx
packages/next/src/client/app-index.tsx
+2
-2
intercept-console-error.ts
.../src/client/components/globals/intercept-console-error.ts
+1
-1
error-boundary-callbacks.ts
...client/react-client-callbacks/error-boundary-callbacks.ts
+27
-24
report-global-error.ts
.../src/client/react-client-callbacks/report-global-error.ts
+2
-1
owner-stack.test.ts
test/development/app-dir/owner-stack/owner-stack.test.ts
+123
-142
index.test.ts
test/development/pages-dir/client-navigation/index.test.ts
+62
-23
index.test.ts
test/e2e/app-dir/errors/index.test.ts
+54
-16
add-redbox-matchers.ts
test/lib/add-redbox-matchers.ts
+2
-0
No files found.
packages/next/src/client/app-index.tsx
View file @
871ca31e
...
@@ -244,11 +244,11 @@ function Root({ children }: React.PropsWithChildren<{}>) {
...
@@ -244,11 +244,11 @@ function Root({ children }: React.PropsWithChildren<{}>) {
return
children
return
children
}
}
const
reactRootOptions
=
{
const
reactRootOptions
:
ReactDOMClient
.
RootOptions
=
{
onRecoverableError
,
onRecoverableError
,
onCaughtError
,
onCaughtError
,
onUncaughtError
,
onUncaughtError
,
}
satisfies
ReactDOMClient
.
RootOptions
}
export
function
hydrate
()
{
export
function
hydrate
()
{
const
reactEl
=
(
const
reactEl
=
(
...
...
packages/next/src/client/components/globals/intercept-console-error.ts
View file @
871ca31e
...
@@ -3,7 +3,7 @@ import { isNextRouterError } from '../is-next-router-error'
...
@@ -3,7 +3,7 @@ import { isNextRouterError } from '../is-next-router-error'
import
{
handleClientError
}
from
'../errors/use-error-handler'
import
{
handleClientError
}
from
'../errors/use-error-handler'
import
{
parseConsoleArgs
}
from
'../../lib/console'
import
{
parseConsoleArgs
}
from
'../../lib/console'
export
const
originConsoleError
=
window
.
console
.
error
export
const
originConsoleError
=
globalThis
.
console
.
error
// Patch console.error to collect information about hydration errors
// Patch console.error to collect information about hydration errors
export
function
patchConsoleError
()
{
export
function
patchConsoleError
()
{
...
...
packages/next/src/client/react-client-callbacks/error-boundary-callbacks.ts
View file @
871ca31e
// This file is only used in app router due to the specific error state handling.
// This file is only used in app router due to the specific error state handling.
import
type
{
HydrationOptions
}
from
'react-dom/clien
t'
import
type
{
ErrorInfo
}
from
'reac
t'
import
{
getReactStitchedError
}
from
'../components/errors/stitched-error'
import
{
getReactStitchedError
}
from
'../components/errors/stitched-error'
import
{
handleClientError
}
from
'../components/errors/use-error-handler'
import
{
handleClientError
}
from
'../components/errors/use-error-handler'
import
{
isNextRouterError
}
from
'../components/is-next-router-error'
import
{
isNextRouterError
}
from
'../components/is-next-router-error'
import
{
isBailoutToCSRError
}
from
'../../shared/lib/lazy-dynamic/bailout-to-csr'
import
{
isBailoutToCSRError
}
from
'../../shared/lib/lazy-dynamic/bailout-to-csr'
import
{
reportGlobalError
}
from
'./report-global-error'
import
{
reportGlobalError
}
from
'./report-global-error'
import
{
originConsoleError
}
from
'../components/globals/intercept-console-error'
import
{
originConsoleError
}
from
'../components/globals/intercept-console-error'
import
{
AppDevOverlayErrorBoundary
}
from
'../components/react-dev-overlay/app/app-dev-overlay-error-boundary'
import
{
ErrorBoundaryHandler
,
GlobalError
as
DefaultErrorBoundary
,
}
from
'../components/error-boundary'
export
function
onCaughtError
(
err
:
unknown
,
errorInfo
:
ErrorInfo
&
{
errorBoundary
?:
React
.
Component
}
)
{
const
errorBoundaryComponent
=
errorInfo
.
errorBoundary
?.
constructor
const
isImplicitErrorBoundary
=
(
process
.
env
.
NODE_ENV
!==
'production'
&&
errorBoundaryComponent
===
AppDevOverlayErrorBoundary
)
||
(
errorBoundaryComponent
===
ErrorBoundaryHandler
&&
(
errorInfo
.
errorBoundary
!
as
InstanceType
<
typeof
ErrorBoundaryHandler
>
)
.
props
.
errorComponent
===
DefaultErrorBoundary
)
if
(
isImplicitErrorBoundary
)
{
// We don't consider errors caught unless they're caught by an explicit error
// boundary. The built-in ones are considered implicit.
// This mimics how the same app would behave without Next.js.
return
onUncaughtError
(
err
,
errorInfo
)
}
export
const
onCaughtError
:
HydrationOptions
[
'onCaughtError'
]
=
(
err
,
errorInfo
)
=>
{
// Skip certain custom errors which are not expected to be reported on client
// Skip certain custom errors which are not expected to be reported on client
if
(
isBailoutToCSRError
(
err
)
||
isNextRouterError
(
err
))
return
if
(
isBailoutToCSRError
(
err
)
||
isNextRouterError
(
err
))
return
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
const
errorBoundaryComponent
=
errorInfo
?.
errorBoundary
?.
constructor
const
errorBoundaryName
=
const
errorBoundaryName
=
// read react component displayName
// read react component displayName
(
errorBoundaryComponent
as
any
)?.
displayName
||
(
errorBoundaryComponent
as
any
)?.
displayName
||
...
@@ -57,26 +76,11 @@ export const onCaughtError: HydrationOptions['onCaughtError'] = (
...
@@ -57,26 +76,11 @@ export const onCaughtError: HydrationOptions['onCaughtError'] = (
}
}
}
}
export
const
onUncaughtError
:
HydrationOptions
[
'onUncaughtError'
]
=
(
export
function
onUncaughtError
(
err
:
unknown
,
errorInfo
:
React
.
ErrorInfo
)
{
err
,
errorInfo
)
=>
{
// Skip certain custom errors which are not expected to be reported on client
// Skip certain custom errors which are not expected to be reported on client
if
(
isBailoutToCSRError
(
err
)
||
isNextRouterError
(
err
))
return
if
(
isBailoutToCSRError
(
err
)
||
isNextRouterError
(
err
))
return
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
const
componentThatErroredFrame
=
errorInfo
?.
componentStack
?.
split
(
'
\
n'
)[
1
]
// Match chrome or safari stack trace
const
matches
=
componentThatErroredFrame
?.
match
(
/
\s
+at
(\w
+
)\s
+|
(\w
+
)
@/
)
??
[]
const
componentThatErroredName
=
matches
[
1
]
||
matches
[
2
]
||
'Unknown'
// Create error location with errored component and error boundary, to match the behavior of default React onCaughtError handler.
const
errorLocation
=
componentThatErroredName
?
`The above error occurred in the <
${
componentThatErroredName
}
> component.`
:
`The above error occurred in one of your components.`
const
stitchedError
=
getReactStitchedError
(
err
)
const
stitchedError
=
getReactStitchedError
(
err
)
// TODO: change to passing down errorInfo later
// TODO: change to passing down errorInfo later
// In development mode, pass along the component stack to the error
// In development mode, pass along the component stack to the error
...
@@ -84,8 +88,7 @@ export const onUncaughtError: HydrationOptions['onUncaughtError'] = (
...
@@ -84,8 +88,7 @@ export const onUncaughtError: HydrationOptions['onUncaughtError'] = (
;(
stitchedError
as
any
).
_componentStack
=
errorInfo
.
componentStack
;(
stitchedError
as
any
).
_componentStack
=
errorInfo
.
componentStack
}
}
// Log and report the error with location but without modifying the error stack
// TODO: Add an adendum to the overlay telling people about custom error boundaries.
originConsoleError
(
'%o
\
n
\
n%s'
,
err
,
errorLocation
)
reportGlobalError
(
stitchedError
)
reportGlobalError
(
stitchedError
)
}
else
{
}
else
{
reportGlobalError
(
err
)
reportGlobalError
(
err
)
...
...
packages/next/src/client/react-client-callbacks/report-global-error.ts
View file @
871ca31e
...
@@ -4,5 +4,6 @@ export const reportGlobalError =
...
@@ -4,5 +4,6 @@ export const reportGlobalError =
// emulating an uncaught JavaScript error.
// emulating an uncaught JavaScript error.
reportError
reportError
:
(
error
:
unknown
)
=>
{
:
(
error
:
unknown
)
=>
{
window
.
console
.
error
(
error
)
// TODO: Dispatch error event
globalThis
.
console
.
error
(
error
)
}
}
test/development/app-dir/owner-stack/owner-stack.test.ts
View file @
871ca31e
This diff is collapsed.
Click to expand it.
test/development/pages-dir/client-navigation/index.test.ts
View file @
871ca31e
/* eslint-env jest */
/* eslint-env jest */
import
{
import
{
assertNoRedbox
,
fetchViaHTTP
,
waitFor
,
check
}
from
'next-test-utils'
assertHasRedbox
,
assertNoRedbox
,
fetchViaHTTP
,
getRedboxSource
,
getRedboxHeader
,
waitFor
,
check
,
}
from
'next-test-utils'
import
webdriver
,
{
BrowserInterface
}
from
'next-webdriver'
import
webdriver
,
{
BrowserInterface
}
from
'next-webdriver'
import
path
from
'path'
import
path
from
'path'
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
nextTestSetup
}
from
'e2e-utils'
...
@@ -16,7 +8,7 @@ import { nextTestSetup } from 'e2e-utils'
...
@@ -16,7 +8,7 @@ import { nextTestSetup } from 'e2e-utils'
const
isReact18
=
parseInt
(
process
.
env
.
NEXT_TEST_REACT_VERSION
)
===
18
const
isReact18
=
parseInt
(
process
.
env
.
NEXT_TEST_REACT_VERSION
)
===
18
describe
(
'Client Navigation'
,
()
=>
{
describe
(
'Client Navigation'
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
const
{
isTurbopack
,
next
}
=
nextTestSetup
({
files
:
path
.
join
(
__dirname
,
'fixture'
),
files
:
path
.
join
(
__dirname
,
'fixture'
),
env
:
{
env
:
{
TEST_STRICT_NEXT_HEAD
:
String
(
true
),
TEST_STRICT_NEXT_HEAD
:
String
(
true
),
...
@@ -281,10 +273,16 @@ describe('Client Navigation', () => {
...
@@ -281,10 +273,16 @@ describe('Client Navigation', () => {
},
},
})
})
await
browser
.
elementByCss
(
'#empty-props'
).
click
()
await
browser
.
elementByCss
(
'#empty-props'
).
click
()
await
assertHasRedbox
(
browser
)
await
expect
(
browser
).
toDisplayRedbox
(
`
expect
(
await
getRedboxHeader
(
browser
)).
toMatch
(
{
/should resolve to an object
\.
But found "null" instead
\.
/
"count": 1,
)
"description": "Error: "EmptyInitialPropsPage.getInitialProps()" should resolve to an object. But found "null" instead.",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": null,
"stack": [],
}
`
)
expect
(
pageErrors
).
toEqual
([
expect
(
pageErrors
).
toEqual
([
expect
.
objectContaining
({
expect
.
objectContaining
({
message
:
message
:
...
@@ -1393,11 +1391,20 @@ describe('Client Navigation', () => {
...
@@ -1393,11 +1391,20 @@ describe('Client Navigation', () => {
})
})
},
},
})
})
await
assertHasRedbox
(
browser
)
await
expect
(
browser
).
toDisplayRedbox
(
`
const
text
=
await
getRedboxSource
(
browser
)
{
expect
(
text
).
toMatch
(
/An Expected error occurred/
)
"count": 1,
expect
(
text
).
toMatch
(
/pages
[\\/]
error-inside-browser-page
\.
js
\(
5:13
\)
/
)
"description": "Error: An Expected error occurred",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "pages/error-inside-browser-page.js (5:13) @ ErrorInRenderPage.render
> 5 | throw new Error('An Expected error occurred')
| ^",
"stack": [
"ErrorInRenderPage.render pages/error-inside-browser-page.js (5:13)",
],
}
`
)
expect
(
pageErrors
).
toEqual
(
expect
(
pageErrors
).
toEqual
(
isReact18
isReact18
?
[
?
[
...
@@ -1437,10 +1444,42 @@ describe('Client Navigation', () => {
...
@@ -1437,10 +1444,42 @@ describe('Client Navigation', () => {
},
},
}
}
)
)
await
assertHasRedbox
(
browser
)
const
text
=
await
getRedboxSource
(
browser
)
if
(
isTurbopack
)
{
expect
(
text
).
toMatch
(
/An Expected error occurred/
)
await
expect
(
browser
).
toDisplayRedbox
(
`
expect
(
text
).
toMatch
(
/error-in-the-browser-global-scope
\.
js
\(
2:9
\)
/
)
{
"count": 1,
"description": "Error: An Expected error occurred",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "pages/error-in-the-browser-global-scope.js (2:9) @ [project]/pages/error-in-the-browser-global-scope.js [client] (ecmascript)
> 2 | throw new Error('An Expected error occurred')
| ^",
"stack": [
"[project]/pages/error-in-the-browser-global-scope.js [client] (ecmascript) pages/error-in-the-browser-global-scope.js (2:9)",
],
}
`
)
}
else
{
await
expect
(
browser
).
toDisplayRedbox
(
`
{
"count": 1,
"description": "Error: An Expected error occurred",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "pages/error-in-the-browser-global-scope.js (2:9) @ eval
> 2 | throw new Error('An Expected error occurred')
| ^",
"stack": [
"eval pages/error-in-the-browser-global-scope.js (2:9)",
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
],
}
`
)
}
expect
(
pageErrors
).
toEqual
([
expect
(
pageErrors
).
toEqual
([
expect
.
objectContaining
({
message
:
'An Expected error occurred'
}),
expect
.
objectContaining
({
message
:
'An Expected error occurred'
}),
])
])
...
...
test/e2e/app-dir/errors/index.test.ts
View file @
871ca31e
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
assertHasRedbox
,
getRedboxHeader
,
retry
}
from
'next-test-utils'
import
{
retry
}
from
'next-test-utils'
import
stripAnsi
from
'strip-ansi'
import
stripAnsi
from
'strip-ansi'
describe
(
'app-dir - errors'
,
()
=>
{
describe
(
'app-dir - errors'
,
()
=>
{
...
@@ -174,8 +174,20 @@ describe('app-dir - errors', () => {
...
@@ -174,8 +174,20 @@ describe('app-dir - errors', () => {
await
browser
.
elementByCss
(
'#error-trigger-button'
).
click
()
await
browser
.
elementByCss
(
'#error-trigger-button'
).
click
()
if
(
isNextDev
)
{
if
(
isNextDev
)
{
await
assertHasRedbox
(
browser
)
await
expect
(
browser
).
toDisplayRedbox
(
`
expect
(
await
getRedboxHeader
(
browser
)).
toMatch
(
/this is a test/
)
{
"count": 1,
"description": "Error: this is a test",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "app/global-error-boundary/client/page.js (8:11) @ Page
> 8 | throw new Error('this is a test')
| ^",
"stack": [
"Page app/global-error-boundary/client/page.js (8:11)",
],
}
`
)
}
else
{
}
else
{
expect
(
expect
(
await
browser
.
waitForElementByCss
(
'body'
).
elementByCss
(
'h2'
).
text
()
await
browser
.
waitForElementByCss
(
'body'
).
elementByCss
(
'h2'
).
text
()
...
@@ -184,8 +196,11 @@ describe('app-dir - errors', () => {
...
@@ -184,8 +196,11 @@ describe('app-dir - errors', () => {
)
)
}
}
// FIXME(veil): Should contain thrown error.
expect
(
pageErrors
).
toEqual
([
expect
(
pageErrors
).
toEqual
([])
expect
.
objectContaining
({
message
:
'this is a test'
,
}),
])
})
})
it
(
'should display error digest for error in server component with default error boundary'
,
async
()
=>
{
it
(
'should display error digest for error in server component with default error boundary'
,
async
()
=>
{
...
@@ -199,8 +214,20 @@ describe('app-dir - errors', () => {
...
@@ -199,8 +214,20 @@ describe('app-dir - errors', () => {
})
})
if
(
isNextDev
)
{
if
(
isNextDev
)
{
await
assertHasRedbox
(
browser
)
await
expect
(
browser
).
toDisplayRedbox
(
`
expect
(
await
getRedboxHeader
(
browser
)).
toMatch
(
/custom server error/
)
{
"count": 1,
"description": "Error: custom server error",
"environmentLabel": "Server",
"label": "Unhandled Runtime Error",
"source": "app/global-error-boundary/server/page.js (2:9) @ Page
> 2 | throw Error('custom server error')
| ^",
"stack": [
"Page app/global-error-boundary/server/page.js (2:9)",
],
}
`
)
}
else
{
}
else
{
expect
(
expect
(
await
browser
.
waitForElementByCss
(
'body'
).
elementByCss
(
'h2'
).
text
()
await
browser
.
waitForElementByCss
(
'body'
).
elementByCss
(
'h2'
).
text
()
...
@@ -212,8 +239,15 @@ describe('app-dir - errors', () => {
...
@@ -212,8 +239,15 @@ describe('app-dir - errors', () => {
).
toMatch
(
/Digest:
\w
+/
)
).
toMatch
(
/Digest:
\w
+/
)
}
}
// FIXME(veil): Should contain thrown error.
expect
(
pageErrors
).
toEqual
([
expect
(
pageErrors
).
toEqual
([])
expect
.
objectContaining
({
message
:
isNextDev
?
'custom server 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.'
,
}),
])
})
})
// production tests
// production tests
...
@@ -244,13 +278,17 @@ describe('app-dir - errors', () => {
...
@@ -244,13 +278,17 @@ describe('app-dir - errors', () => {
})
})
it
(
'should hydrate empty shell to handle server-side rendering errors'
,
async
()
=>
{
it
(
'should hydrate empty shell to handle server-side rendering errors'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/ssr-error-client-component'
)
const
pageErrors
:
unknown
[]
=
[]
const
logs
=
await
browser
.
log
()
await
next
.
browser
(
'/ssr-error-client-component'
,
{
const
errors
=
logs
beforePageLoad
:
(
page
)
=>
{
.
filter
((
x
)
=>
x
.
source
===
'error'
)
page
.
on
(
'pageerror'
,
(
error
:
unknown
)
=>
{
.
map
((
x
)
=>
x
.
message
)
pageErrors
.
push
(
error
)
.
join
(
'
\
n'
)
})
expect
(
errors
).
toInclude
(
'Error during SSR'
)
},
})
expect
(
pageErrors
).
toEqual
([
expect
.
objectContaining
({
message
:
'Error during SSR'
}),
])
})
})
it
(
'should log the original RSC error trace in production'
,
async
()
=>
{
it
(
'should log the original RSC error trace in production'
,
async
()
=>
{
...
...
test/lib/add-redbox-matchers.ts
View file @
871ca31e
...
@@ -30,6 +30,7 @@ declare global {
...
@@ -30,6 +30,7 @@ declare global {
* Unintented content in the snapshot should be reported to the Next.js DX team.
* Unintented content in the snapshot should be reported to the Next.js DX team.
* `<FIXME-internal-frame>` in the snapshot would be unintended.
* `<FIXME-internal-frame>` in the snapshot would be unintended.
* `<FIXME-project-root>` in the snapshot would be unintended.
* `<FIXME-project-root>` in the snapshot would be unintended.
* `<FIXME-file-protocol>` in the snapshot would be unintended.
* Any node_modules in the snapshot would be unintended.
* Any node_modules in the snapshot would be unintended.
* Differences in the snapshot between Turbopack and Webpack would be unintended.
* Differences in the snapshot between Turbopack and Webpack would be unintended.
*
*
...
@@ -48,6 +49,7 @@ declare global {
...
@@ -48,6 +49,7 @@ declare global {
* Unintented content in the snapshot should be reported to the Next.js DX team.
* Unintented content in the snapshot should be reported to the Next.js DX team.
* `<FIXME-internal-frame>` in the snapshot would be unintended.
* `<FIXME-internal-frame>` in the snapshot would be unintended.
* `<FIXME-project-root>` in the snapshot would be unintended.
* `<FIXME-project-root>` in the snapshot would be unintended.
* `<FIXME-file-protocol>` in the snapshot would be unintended.
* Any node_modules in the snapshot would be unintended.
* Any node_modules in the snapshot would be unintended.
* Differences in the snapshot between Turbopack and Webpack would be unintended.
* Differences in the snapshot between Turbopack and Webpack would be unintended.
*
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment