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
0d81c3c6
Unverified
Commit
0d81c3c6
authored
Jan 09, 2025
by
Jiachi Liu
Committed by
GitHub
Jan 09, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix presentation when onerror receives an event without error (#74643)
parent
a0eab62e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
50 additions
and
112 deletions
+50
-112
use-error-handler.ts
...erlay/_experimental/internal/helpers/use-error-handler.ts
+0
-111
use-error-handler.ts
...s/react-dev-overlay/internal/helpers/use-error-handler.ts
+5
-1
page.js
...dir/capture-console-error/app/browser/error-event/page.js
+20
-0
capture-console-error.test.ts
...p-dir/capture-console-error/capture-console-error.test.ts
+10
-0
next-test-utils.ts
test/lib/next-test-utils.ts
+15
-0
No files found.
packages/next/src/client/components/react-dev-overlay/_experimental/internal/helpers/use-error-handler.ts
deleted
100644 → 0
View file @
a0eab62e
import
{
useEffect
}
from
'react'
import
{
attachHydrationErrorState
}
from
'./attach-hydration-error-state'
import
{
isNextRouterError
}
from
'../../../../is-next-router-error'
import
{
storeHydrationErrorStateFromConsoleArgs
}
from
'./hydration-error-info'
import
{
formatConsoleArgs
}
from
'../../../../../lib/console'
import
isError
from
'../../../../../../lib/is-error'
import
{
createUnhandledError
}
from
'./console-error'
import
{
enqueueConsecutiveDedupedError
}
from
'./enqueue-client-error'
import
{
getReactStitchedError
}
from
'./stitched-error'
const
queueMicroTask
=
globalThis
.
queueMicrotask
||
((
cb
:
()
=>
void
)
=>
Promise
.
resolve
().
then
(
cb
))
export
type
ErrorHandler
=
(
error
:
Error
)
=>
void
const
errorQueue
:
Array
<
Error
>
=
[]
const
errorHandlers
:
Array
<
ErrorHandler
>
=
[]
const
rejectionQueue
:
Array
<
Error
>
=
[]
const
rejectionHandlers
:
Array
<
ErrorHandler
>
=
[]
export
function
handleClientError
(
originError
:
unknown
,
consoleErrorArgs
:
any
[],
capturedFromConsole
:
boolean
=
false
)
{
let
error
:
Error
if
(
!
originError
||
!
isError
(
originError
))
{
// If it's not an error, format the args into an error
const
formattedErrorMessage
=
formatConsoleArgs
(
consoleErrorArgs
)
error
=
createUnhandledError
(
formattedErrorMessage
)
}
else
{
error
=
capturedFromConsole
?
createUnhandledError
(
originError
)
:
originError
}
error
=
getReactStitchedError
(
error
)
storeHydrationErrorStateFromConsoleArgs
(...
consoleErrorArgs
)
attachHydrationErrorState
(
error
)
enqueueConsecutiveDedupedError
(
errorQueue
,
error
)
for
(
const
handler
of
errorHandlers
)
{
// Delayed the error being passed to React Dev Overlay,
// avoid the state being synchronously updated in the component.
queueMicroTask
(()
=>
{
handler
(
error
)
})
}
}
export
function
useErrorHandler
(
handleOnUnhandledError
:
ErrorHandler
,
handleOnUnhandledRejection
:
ErrorHandler
)
{
useEffect
(()
=>
{
// Handle queued errors.
errorQueue
.
forEach
(
handleOnUnhandledError
)
rejectionQueue
.
forEach
(
handleOnUnhandledRejection
)
// Listen to new errors.
errorHandlers
.
push
(
handleOnUnhandledError
)
rejectionHandlers
.
push
(
handleOnUnhandledRejection
)
return
()
=>
{
// Remove listeners.
errorHandlers
.
splice
(
errorHandlers
.
indexOf
(
handleOnUnhandledError
),
1
)
rejectionHandlers
.
splice
(
rejectionHandlers
.
indexOf
(
handleOnUnhandledRejection
),
1
)
}
},
[
handleOnUnhandledError
,
handleOnUnhandledRejection
])
}
function
onUnhandledError
(
event
:
WindowEventMap
[
'error'
]):
void
|
boolean
{
if
(
isNextRouterError
(
event
.
error
))
{
event
.
preventDefault
()
return
false
}
handleClientError
(
event
.
error
,
[])
}
function
onUnhandledRejection
(
ev
:
WindowEventMap
[
'unhandledrejection'
]):
void
{
const
reason
=
ev
?.
reason
if
(
isNextRouterError
(
reason
))
{
ev
.
preventDefault
()
return
}
let
error
=
reason
if
(
error
&&
!
isError
(
error
))
{
error
=
createUnhandledError
(
error
+
''
)
}
rejectionQueue
.
push
(
error
)
for
(
const
handler
of
rejectionHandlers
)
{
handler
(
error
)
}
}
export
function
handleGlobalErrors
()
{
if
(
typeof
window
!==
'undefined'
)
{
try
{
// Increase the number of stack frames on the client
Error
.
stackTraceLimit
=
50
}
catch
{}
window
.
addEventListener
(
'error'
,
onUnhandledError
)
window
.
addEventListener
(
'unhandledrejection'
,
onUnhandledRejection
)
}
}
packages/next/src/client/components/react-dev-overlay/internal/helpers/use-error-handler.ts
View file @
0d81c3c6
...
@@ -77,7 +77,11 @@ function onUnhandledError(event: WindowEventMap['error']): void | boolean {
...
@@ -77,7 +77,11 @@ function onUnhandledError(event: WindowEventMap['error']): void | boolean {
event
.
preventDefault
()
event
.
preventDefault
()
return
false
return
false
}
}
handleClientError
(
event
.
error
,
[])
// When there's an error property present, we log the error to error overlay.
// Otherwise we don't do anything as it's not logging in the console either.
if
(
event
.
error
)
{
handleClientError
(
event
.
error
,
[])
}
}
}
function
onUnhandledRejection
(
ev
:
WindowEventMap
[
'unhandledrejection'
]):
void
{
function
onUnhandledRejection
(
ev
:
WindowEventMap
[
'unhandledrejection'
]):
void
{
...
...
test/development/app-dir/capture-console-error/app/browser/error-event/page.js
0 → 100644
View file @
0d81c3c6
'use client'
export
default
function
Page
()
{
return
(
<
button
onClick
=
{()
=>
{
// Create an ErrorEvent with only a message
const
errorEvent
=
new
ErrorEvent
(
'error'
,
{
message
:
'dummy error message'
,
// Message for the event
// Omit the `error` property to ensure it is not included
})
// Dispatch the event
window
.
dispatchEvent
(
errorEvent
)
}}
>
click
to
trigger
error
event
<
/button
>
)
}
test/development/app-dir/capture-console-error/capture-console-error.test.ts
View file @
0d81c3c6
...
@@ -7,6 +7,8 @@ import {
...
@@ -7,6 +7,8 @@ import {
getRedboxTotalErrorCount
,
getRedboxTotalErrorCount
,
openRedbox
,
openRedbox
,
hasRedboxCallStack
,
hasRedboxCallStack
,
assertNoRedbox
,
assertNoConsoleErrors
,
}
from
'next-test-utils'
}
from
'next-test-utils'
async
function
getRedboxResult
(
browser
:
any
)
{
async
function
getRedboxResult
(
browser
:
any
)
{
...
@@ -315,4 +317,12 @@ describe('app-dir - capture-console-error', () => {
...
@@ -315,4 +317,12 @@ describe('app-dir - capture-console-error', () => {
`
)
`
)
}
}
})
})
it
(
'should display the error message in error event when event.error is not present'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/browser/error-event'
)
await
browser
.
elementByCss
(
'button'
).
click
()
await
assertNoRedbox
(
browser
)
await
assertNoConsoleErrors
(
browser
)
})
})
})
test/lib/next-test-utils.ts
View file @
0d81c3c6
...
@@ -1545,3 +1545,18 @@ export function createNowRouteMatches(
...
@@ -1545,3 +1545,18 @@ export function createNowRouteMatches(
return
urlSearchParams
return
urlSearchParams
}
}
export
async
function
assertNoConsoleErrors
(
browser
:
BrowserInterface
)
{
const
logs
=
await
browser
.
log
()
const
warningsAndErrors
=
logs
.
filter
((
log
)
=>
{
return
(
log
.
source
===
'warning'
||
(
log
.
source
===
'error'
&&
// These are expected when we visit 404 pages.
log
.
message
!==
'Failed to load resource: the server responded with a status of 404 (Not Found)'
)
)
})
expect
(
warningsAndErrors
).
toEqual
([])
}
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