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
2ee93c34
Unverified
Commit
2ee93c34
authored
Nov 06, 2024
by
Jiachi Liu
Committed by
GitHub
Nov 06, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Append owner stack for captured string console error (#72115)
parent
90c0a768
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
304 additions
and
13 deletions
+304
-13
stitched-error.ts
...ents/react-dev-overlay/internal/helpers/stitched-error.ts
+16
-6
use-error-handler.ts
...s/react-dev-overlay/internal/helpers/use-error-handler.ts
+2
-1
owner-stack-invalid-element-type.test.ts
...lid-element-type/owner-stack-invalid-element-type.test.ts
+7
-6
layout.tsx
...app-dir/owner-stack-react-missing-key-prop/app/layout.tsx
+8
-0
page.tsx
...p-dir/owner-stack-react-missing-key-prop/app/rsc/page.tsx
+11
-0
page.tsx
...p-dir/owner-stack-react-missing-key-prop/app/ssr/page.tsx
+13
-0
next.config.js
...app-dir/owner-stack-react-missing-key-prop/next.config.js
+10
-0
owner-stack-react-missing-key-prop.test.ts
...ssing-key-prop/owner-stack-react-missing-key-prop.test.ts
+103
-0
react-missing-key-prop.test.ts
...ack-react-missing-key-prop/react-missing-key-prop.test.ts
+134
-0
No files found.
packages/next/src/client/components/react-dev-overlay/internal/helpers/stitched-error.ts
View file @
2ee93c34
...
@@ -6,6 +6,10 @@ const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX = new RegExp(
...
@@ -6,6 +6,10 @@ const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX = new RegExp(
`(at
${
REACT_ERROR_STACK_BOTTOM_FRAME
}
)|(
${
REACT_ERROR_STACK_BOTTOM_FRAME
}
\\@)`
`(at
${
REACT_ERROR_STACK_BOTTOM_FRAME
}
)|(
${
REACT_ERROR_STACK_BOTTOM_FRAME
}
\\@)`
)
)
const
captureOwnerStack
=
(
React
as
any
).
captureOwnerStack
?
(
React
as
any
).
captureOwnerStack
:
()
=>
''
export
function
getReactStitchedError
<
T
=
unknown
>
(
err
:
T
):
Error
|
T
{
export
function
getReactStitchedError
<
T
=
unknown
>
(
err
:
T
):
Error
|
T
{
if
(
typeof
(
React
as
any
).
captureOwnerStack
!==
'function'
)
{
if
(
typeof
(
React
as
any
).
captureOwnerStack
!==
'function'
)
{
return
err
return
err
...
@@ -28,12 +32,18 @@ export function getReactStitchedError<T = unknown>(err: T): Error | T {
...
@@ -28,12 +32,18 @@ export function getReactStitchedError<T = unknown>(err: T): Error | T {
newError
.
stack
=
newStack
newError
.
stack
=
newStack
// Avoid duplicate overriding stack frames
// Avoid duplicate overriding stack frames
const
ownerStack
=
(
React
as
any
).
captureOwnerStack
()
appendOwnerStack
(
newError
)
if
(
ownerStack
&&
newStack
.
endsWith
(
ownerStack
)
===
false
)
{
newStack
+=
ownerStack
// Override stack
newError
.
stack
=
newStack
}
return
newError
return
newError
}
}
function
appendOwnerStack
(
error
:
Error
)
{
let
stack
=
error
.
stack
||
''
// Avoid duplicate overriding stack frames
const
ownerStack
=
captureOwnerStack
()
if
(
ownerStack
&&
stack
.
endsWith
(
ownerStack
)
===
false
)
{
stack
+=
ownerStack
// Override stack
error
.
stack
=
stack
}
}
packages/next/src/client/components/react-dev-overlay/internal/helpers/use-error-handler.ts
View file @
2ee93c34
...
@@ -6,6 +6,7 @@ import { formatConsoleArgs } from '../../../../lib/console'
...
@@ -6,6 +6,7 @@ import { formatConsoleArgs } from '../../../../lib/console'
import
isError
from
'../../../../../lib/is-error'
import
isError
from
'../../../../../lib/is-error'
import
{
createUnhandledError
}
from
'./console-error'
import
{
createUnhandledError
}
from
'./console-error'
import
{
enqueueConsecutiveDedupedError
}
from
'./enqueue-client-error'
import
{
enqueueConsecutiveDedupedError
}
from
'./enqueue-client-error'
import
{
getReactStitchedError
}
from
'./stitched-error'
export
type
ErrorHandler
=
(
error
:
Error
)
=>
void
export
type
ErrorHandler
=
(
error
:
Error
)
=>
void
...
@@ -22,7 +23,7 @@ export function handleClientError(
...
@@ -22,7 +23,7 @@ export function handleClientError(
if
(
!
originError
||
!
isError
(
originError
))
{
if
(
!
originError
||
!
isError
(
originError
))
{
// If it's not an error, format the args into an error
// If it's not an error, format the args into an error
const
formattedErrorMessage
=
formatConsoleArgs
(
consoleErrorArgs
)
const
formattedErrorMessage
=
formatConsoleArgs
(
consoleErrorArgs
)
error
=
createUnhandledError
(
formattedErrorMessage
)
error
=
getReactStitchedError
(
createUnhandledError
(
formattedErrorMessage
)
)
}
else
{
}
else
{
error
=
originError
error
=
originError
}
}
...
...
test/development/app-dir/owner-stack-invalid-element-type/owner-stack-invalid-element-type.test.ts
View file @
2ee93c34
...
@@ -41,9 +41,10 @@ const isOwnerStackEnabled =
...
@@ -41,9 +41,10 @@ const isOwnerStackEnabled =
11 | }"
11 | }"
`
)
`
)
}
else
{
}
else
{
// FIXME: the owner stack method names should be `Inner > Page`
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`
"at BrowserOnly (app/browser/page.js (11:11))
"at BrowserOnly (app/browser/page.js (11:11))
at Inner (app/browser/page.js (15:11))"
at Inner (app/browser/page.js (15:11))"
`
)
`
)
// FIXME: the methodName should be `@ BrowserOnly` instead of `@ Foo`
// FIXME: the methodName should be `@ BrowserOnly` instead of `@ Foo`
expect
(
source
).
toMatchInlineSnapshot
(
`
expect
(
source
).
toMatchInlineSnapshot
(
`
...
@@ -83,11 +84,11 @@ const isOwnerStackEnabled =
...
@@ -83,11 +84,11 @@ const isOwnerStackEnabled =
8 | export default function Page() {"
8 | export default function Page() {"
`
)
`
)
}
else
{
}
else
{
// FIXME: the
methodName should be `@ Page` instead of `@ Inner
`
// FIXME: the
owner stack method names should be `Page
`
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`"at Inner (app/rsc/page.js (11:8))"`
`"at Inner (app/rsc/page.js (11:8))"`
)
)
// FIXME: the methodName should be `@ Inner`
instead of `@ Foo`
// FIXME: the methodName should be `@ Inner`
expect
(
source
).
toMatchInlineSnapshot
(
`
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/rsc/page.js (5:11) @ Foo
"app/rsc/page.js (5:11) @ Foo
...
@@ -125,11 +126,11 @@ const isOwnerStackEnabled =
...
@@ -125,11 +126,11 @@ const isOwnerStackEnabled =
10 | export default function Page() {"
10 | export default function Page() {"
`
)
`
)
}
else
{
}
else
{
// FIXME: the
methodName should be `@ Inner` instead of `@ Foo
`
// FIXME: the
owner stack method names should be `Page
`
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`"at Inner (app/ssr/page.js (13:8))"`
`"at Inner (app/ssr/page.js (13:8))"`
)
)
// FIXME: the methodName should be `@ Inner`
instead of `@ Foo`
// FIXME: the methodName should be `@ Inner`
expect
(
source
).
toMatchInlineSnapshot
(
`
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/ssr/page.js (7:11) @ Foo
"app/ssr/page.js (7:11) @ Foo
...
...
test/development/app-dir/owner-stack-react-missing-key-prop/app/layout.tsx
0 → 100644
View file @
2ee93c34
import
{
ReactNode
}
from
'react'
export
default
function
Root
({
children
}:
{
children
:
ReactNode
})
{
return
(
<
html
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/development/app-dir/owner-stack-react-missing-key-prop/app/rsc/page.tsx
0 → 100644
View file @
2ee93c34
const
list
=
[
1
,
2
,
3
]
export
default
function
Page
()
{
return
(
<
div
>
{
list
.
map
((
item
,
index
)
=>
(
<
span
>
{
item
}
</
span
>
))
}
</
div
>
)
}
test/development/app-dir/owner-stack-react-missing-key-prop/app/ssr/page.tsx
0 → 100644
View file @
2ee93c34
'use client'
const
list
=
[
1
,
2
,
3
]
export
default
function
Page
()
{
return
(
<
div
>
{
list
.
map
((
item
,
index
)
=>
(
<
p
>
{
item
}
</
p
>
))
}
</
div
>
)
}
test/development/app-dir/owner-stack-react-missing-key-prop/next.config.js
0 → 100644
View file @
2ee93c34
/**
* @type {import('next').NextConfig}
*/
const
nextConfig
=
{
experimental
:
{
reactOwnerStack
:
true
,
},
}
module
.
exports
=
nextConfig
test/development/app-dir/owner-stack-react-missing-key-prop/owner-stack-react-missing-key-prop.test.ts
0 → 100644
View file @
2ee93c34
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
getRedboxSource
,
waitForAndOpenRuntimeError
,
getStackFramesContent
,
}
from
'next-test-utils'
// TODO: When owner stack is enabled by default, remove the condition and only keep one test
const
isOwnerStackEnabled
=
process
.
env
.
TEST_OWNER_STACK
!==
'false'
||
process
.
env
.
__NEXT_EXPERIMENTAL_PPR
===
'true'
;(
isOwnerStackEnabled
?
describe
:
describe
.
skip
)(
'app-dir - owner-stack-react-missing-key-prop'
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
files
:
__dirname
,
})
it
(
'should catch invalid element from on rsc component'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/rsc'
)
await
waitForAndOpenRuntimeError
(
browser
)
const
stackFramesContent
=
await
getStackFramesContent
(
browser
)
const
source
=
await
getRedboxSource
(
browser
)
if
(
process
.
env
.
TURBOPACK
)
{
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`"at Page (app/rsc/page.tsx (6:13))"`
)
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/rsc/page.tsx (7:9) @ <anonymous>
5 | <div>
6 | {list.map((item, index) => (
> 7 | <span>{item}</span>
| ^
8 | ))}
9 | </div>
10 | )"
`
)
}
else
{
// FIXME: the owner stack method names should be `Page` instead of `map`
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`"at map (app/rsc/page.tsx (6:13))"`
)
// FIXME: the methodName should be `@ <anonymous>` instead of `@ span`
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/rsc/page.tsx (7:10) @ span
5 | <div>
6 | {list.map((item, index) => (
> 7 | <span>{item}</span>
| ^
8 | ))}
9 | </div>
10 | )"
`
)
}
})
it
(
'should catch invalid element from on ssr client component'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/ssr'
)
await
waitForAndOpenRuntimeError
(
browser
)
const
stackFramesContent
=
await
getStackFramesContent
(
browser
)
const
source
=
await
getRedboxSource
(
browser
)
if
(
process
.
env
.
TURBOPACK
)
{
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`"at Page (app/ssr/page.tsx (8:13))"`
)
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/ssr/page.tsx (9:9) @ <unknown>
7 | <div>
8 | {list.map((item, index) => (
> 9 | <p>{item}</p>
| ^
10 | ))}
11 | </div>
12 | )"
`
)
}
else
{
// FIXME: the owner stack method names should be `Page` instead of `map`
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`"at map (app/ssr/page.tsx (8:13))"`
)
// FIXME: the methodName should be `@ <unknown>` instead of `@ p`
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/ssr/page.tsx (9:10) @ p
7 | <div>
8 | {list.map((item, index) => (
> 9 | <p>{item}</p>
| ^
10 | ))}
11 | </div>
12 | )"
`
)
}
})
}
)
test/development/app-dir/owner-stack-react-missing-key-prop/react-missing-key-prop.test.ts
0 → 100644
View file @
2ee93c34
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
getRedboxSource
,
waitForAndOpenRuntimeError
}
from
'next-test-utils'
// TODO: When owner stack is enabled by default, remove the condition and only keep one test
const
isOwnerStackEnabled
=
process
.
env
.
TEST_OWNER_STACK
!==
'false'
||
process
.
env
.
__NEXT_EXPERIMENTAL_PPR
===
'true'
async
function
getStackFramesContent
(
browser
)
{
const
stackFrameElements
=
await
browser
.
elementsByCss
(
'[data-nextjs-call-stack-frame]'
)
const
stackFramesContent
=
(
await
Promise
.
all
(
stackFrameElements
.
map
(
async
(
frame
)
=>
{
const
functionNameEl
=
await
frame
.
$
(
'[data-nextjs-frame-expanded]'
)
const
sourceEl
=
await
frame
.
$
(
'[data-has-source]'
)
const
functionName
=
functionNameEl
?
await
functionNameEl
.
innerText
()
:
''
const
source
=
sourceEl
?
await
sourceEl
.
innerText
()
:
''
if
(
!
functionName
)
{
return
''
}
return
`at
${
functionName
}
(
${
source
}
)`
})
)
)
.
filter
(
Boolean
)
.
join
(
'
\
n'
)
return
stackFramesContent
}
;(
isOwnerStackEnabled
?
describe
.
skip
:
describe
)(
'app-dir - react-missing-key-prop'
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
files
:
__dirname
,
})
let
nextConfig
:
string
=
''
beforeAll
(
async
()
=>
{
await
next
.
stop
()
await
next
.
patchFile
(
'next.config.js'
,
(
content
:
string
)
=>
{
nextConfig
=
content
return
content
.
replace
(
`reactOwnerStack: true`
,
`reactOwnerStack: false`
)
})
await
next
.
start
()
})
afterAll
(
async
()
=>
{
await
next
.
stop
()
// Restore original next.config.js
await
next
.
patchFile
(
'next.config.js'
,
nextConfig
)
})
it
(
'should catch invalid element from on rsc component'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/rsc'
)
await
waitForAndOpenRuntimeError
(
browser
)
const
stackFramesContent
=
await
getStackFramesContent
(
browser
)
const
source
=
await
getRedboxSource
(
browser
)
if
(
process
.
env
.
TURBOPACK
)
{
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`""`
)
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/rsc/page.tsx (5:5) @ Page
3 | export default function Page() {
4 | return (
> 5 | <div>
| ^
6 | {list.map((item, index) => (
7 | <span>{item}</span>
8 | ))}"
`
)
}
else
{
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`""`
)
// FIXME: the methodName should be `@ Page` instead of `@ div`
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/rsc/page.tsx (5:6) @ div
3 | export default function Page() {
4 | return (
> 5 | <div>
| ^
6 | {list.map((item, index) => (
7 | <span>{item}</span>
8 | ))}"
`
)
}
})
it
(
'should catch invalid element from on ssr client component'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/ssr'
)
await
waitForAndOpenRuntimeError
(
browser
)
const
stackFramesContent
=
await
getStackFramesContent
(
browser
)
const
source
=
await
getRedboxSource
(
browser
)
if
(
process
.
env
.
TURBOPACK
)
{
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`""`
)
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/ssr/page.tsx (7:5) @ Page
5 | export default function Page() {
6 | return (
> 7 | <div>
| ^
8 | {list.map((item, index) => (
9 | <p>{item}</p>
10 | ))}"
`
)
}
else
{
expect
(
stackFramesContent
).
toMatchInlineSnapshot
(
`""`
)
// FIXME: the methodName should be `@ Page` instead of `@ div`
expect
(
source
).
toMatchInlineSnapshot
(
`
"app/ssr/page.tsx (7:6) @ div
5 | export default function Page() {
6 | return (
> 7 | <div>
| ^
8 | {list.map((item, index) => (
9 | <p>{item}</p>
10 | ))}"
`
)
}
})
}
)
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