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
1fb83619
Unverified
Commit
1fb83619
authored
Oct 29, 2024
by
Jiachi Liu
Committed by
GitHub
Oct 29, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Compare error stack to dedupe error (#71798)
parent
14b92e6e
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
82 additions
and
24 deletions
+82
-24
intercept-console-error.ts
.../src/client/components/globals/intercept-console-error.ts
+9
-2
capture-stack-trace.ts
...react-dev-overlay/internal/helpers/capture-stack-trace.ts
+12
-0
enqueue-client-error.ts
...eact-dev-overlay/internal/helpers/enqueue-client-error.ts
+2
-2
stitched-error.ts
...ents/react-dev-overlay/internal/helpers/stitched-error.ts
+0
-1
page.js
...dir/capture-console-error/app/browser/render-loop/page.js
+8
-0
capture-console-error.test.ts
...p-dir/capture-console-error/capture-console-error.test.ts
+50
-18
next-test-utils.ts
test/lib/next-test-utils.ts
+1
-1
No files found.
packages/next/src/client/components/globals/intercept-console-error.ts
View file @
1fb83619
import
isError
from
'../../../lib/is-error'
import
{
isNextRouterError
}
from
'../is-next-router-error'
import
{
captureStackTrace
}
from
'../react-dev-overlay/internal/helpers/capture-stack-trace'
import
{
handleClientError
}
from
'../react-dev-overlay/internal/helpers/use-error-handler'
export
const
originConsoleError
=
window
.
console
.
error
...
...
@@ -10,13 +11,14 @@ export function patchConsoleError() {
if
(
typeof
window
===
'undefined'
)
{
return
}
window
.
console
.
error
=
(...
args
:
any
[])
=>
{
window
.
console
.
error
=
function
error
(...
args
:
any
[])
{
let
maybeError
:
unknown
let
isReplayedError
=
false
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
const
replayedError
=
matchReplayedError
(...
args
)
if
(
replayedError
)
{
isReplayedError
=
true
maybeError
=
replayedError
}
else
{
// See https://github.com/facebook/react/blob/d50323eb845c5fde0d720cae888bf35dedd05506/packages/react-reconciler/src/ReactFiberErrorLogger.js#L78
...
...
@@ -28,6 +30,11 @@ export function patchConsoleError() {
if
(
!
isNextRouterError
(
maybeError
))
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
// Create an origin stack that pointing to the origin location of the error
if
(
!
isReplayedError
&&
isError
(
maybeError
))
{
captureStackTrace
(
maybeError
)
}
handleClientError
(
// replayed errors have their own complex format string that should be used,
// but if we pass the error directly, `handleClientError` will ignore it
...
...
packages/next/src/client/components/react-dev-overlay/internal/helpers/capture-stack-trace.ts
0 → 100644
View file @
1fb83619
// Polyfill for `Error.captureStackTrace` in browsers
export
function
captureStackTrace
(
obj
:
any
)
{
const
container
=
new
Error
()
Object
.
defineProperty
(
obj
,
'stack'
,
{
configurable
:
true
,
get
()
{
const
{
stack
}
=
container
Object
.
defineProperty
(
this
,
'stack'
,
{
value
:
stack
})
return
stack
},
})
}
packages/next/src/client/components/react-dev-overlay/internal/helpers/enqueue-client-error.ts
View file @
1fb83619
...
...
@@ -7,8 +7,8 @@ export function enqueueConsecutiveDedupedError(
)
{
const
isFront
=
isHydrationError
(
error
)
const
previousError
=
isFront
?
queue
[
0
]
:
queue
[
queue
.
length
-
1
]
//
Only check message to see if it's the same error, as message is representative display in the console.
if
(
previousError
&&
previousError
.
message
===
error
.
message
)
{
//
Compare the error stack to dedupe the consecutive errors
if
(
previousError
&&
previousError
.
stack
===
error
.
stack
)
{
return
}
// TODO: change all to push error into errorQueue,
...
...
packages/next/src/client/components/react-dev-overlay/internal/helpers/stitched-error.ts
View file @
1fb83619
...
...
@@ -10,7 +10,6 @@ export function getReactStitchedError<T = unknown>(err: T): Error | T {
if
(
typeof
(
React
as
any
).
captureOwnerStack
!==
'function'
)
{
return
err
}
const
isErrorInstance
=
isError
(
err
)
const
originStack
=
isErrorInstance
?
err
.
stack
||
''
:
''
const
originMessage
=
isErrorInstance
?
err
.
message
:
''
...
...
test/development/app-dir/capture-console-error/app/browser/render-loop/page.js
0 → 100644
View file @
1fb83619
'use client'
export
default
function
Page
()
{
for
(
let
i
=
0
;
i
<
3
;
i
++
)
{
console
.
error
(
'trigger an console.error in loop of render'
)
}
return
<
p
>
render
<
/p
>
}
test/development/app-dir/capture-console-error/capture-console-error.test.ts
View file @
1fb83619
...
...
@@ -55,11 +55,15 @@ describe('app-dir - capture-console-error', () => {
10 | click to error",
}
`
)
}
else
if
(
isReactExperimental
)
{
}
else
{
expect
(
result
).
toMatchInlineSnapshot
(
`
{
"callStacks": "button
app/browser/event/page.js (5:6)",
"callStacks":
${
isReactExperimental
?
`"button
app/browser/event/page.js (5:6)"`
:
`""`
}
,
"count": 1,
"description": "trigger an console <error>",
"source": "app/browser/event/page.js (7:17) @ error
...
...
@@ -73,27 +77,55 @@ describe('app-dir - capture-console-error', () => {
10 | click to error",
}
`
)
}
})
it
(
'should capture browser console error in render and dedupe if necessary'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/browser/render'
)
await
waitForAndOpenRuntimeError
(
browser
)
await
assertHasRedbox
(
browser
)
const
result
=
await
getRedboxResult
(
browser
)
if
(
process
.
env
.
TURBOPACK
)
{
expect
(
result
).
toMatchInlineSnapshot
(
`
{
"callStacks": "",
"count":
${
isReactExperimental
?
1
:
2
}
,
"description": "trigger an console.error in render",
"source": "app/browser/render/page.js (4:11) @ Page
2 |
3 | export default function Page() {
> 4 | console.error('trigger an console.error in render')
| ^
5 | return <p>render</p>
6 | }
7 |",
}
`
)
}
else
{
expect
(
result
).
toMatchInlineSnapshot
(
`
{
"callStacks": "",
"count":
1
,
"description": "trigger an console
<error>
",
"source": "app/browser/
event/page.js (7:17
) @ error
"count":
${
isReactExperimental
?
1
:
2
}
,
"description": "trigger an console
.error in render
",
"source": "app/browser/
render/page.js (4:11
) @ error
5 | <button
6 | onClick={() =>
{
>
7 | console.error('trigger an console <%s>', 'erro
r')
|
^
8 | }}
9 | >
10 | click to error
",
2 |
3 | export default function Page()
{
>
4 | console.error('trigger an console.error in rende
r')
|
^
5 | return <p>render</p>
6 | }
7 |
",
}
`
)
}
})
it
(
'should capture browser console error in render and dedupe
if necessary
'
,
async
()
=>
{
it
(
'should capture browser console error in render and dedupe
when multi same errors logged
'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/browser/render'
)
await
waitForAndOpenRuntimeError
(
browser
)
...
...
@@ -105,7 +137,7 @@ describe('app-dir - capture-console-error', () => {
expect
(
result
).
toMatchInlineSnapshot
(
`
{
"callStacks": "",
"count":
1
,
"count":
${
isReactExperimental
?
1
:
2
}
,
"description": "trigger an console.error in render",
"source": "app/browser/render/page.js (4:11) @ Page
...
...
@@ -122,7 +154,7 @@ describe('app-dir - capture-console-error', () => {
expect
(
result
).
toMatchInlineSnapshot
(
`
{
"callStacks": "",
"count":
1
,
"count":
${
isReactExperimental
?
1
:
2
}
,
"description": "trigger an console.error in render",
"source": "app/browser/render/page.js (4:11) @ error
...
...
@@ -150,7 +182,7 @@ describe('app-dir - capture-console-error', () => {
expect
(
result
).
toMatchInlineSnapshot
(
`
{
"callStacks": "",
"count":
1
,
"count":
${
isReactExperimental
?
1
:
2
}
,
"description": "ssr console error:client",
"source": "app/ssr/page.js (4:11) @ Page
...
...
@@ -167,7 +199,7 @@ describe('app-dir - capture-console-error', () => {
expect
(
result
).
toMatchInlineSnapshot
(
`
{
"callStacks": "",
"count":
1
,
"count":
${
isReactExperimental
?
1
:
2
}
,
"description": "ssr console error:client",
"source": "app/ssr/page.js (4:11) @ error
...
...
test/lib/next-test-utils.ts
View file @
1fb83619
...
...
@@ -1255,7 +1255,7 @@ export async function toggleCollapseComponentStack(
export
async
function
getRedboxCallStack
(
browser
:
BrowserInterface
):
Promise
<
string
>
{
):
Promise
<
string
|
null
>
{
await
browser
.
waitForElementByCss
(
'[data-nextjs-call-stack-frame]'
,
30000
)
const
callStackFrameElements
=
await
browser
.
elementsByCss
(
...
...
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