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
a94cf1c9
Unverified
Commit
a94cf1c9
authored
Oct 30, 2024
by
Jiachi Liu
Committed by
GitHub
Oct 30, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: handle logging cicurlar object (#72078)
parent
8b0c4e46
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
122 additions
and
1 deletion
+122
-1
is-error.ts
packages/next/src/lib/is-error.ts
+16
-1
page.tsx
...ment/app-dir/serialize-circular-error/app/client/page.tsx
+24
-0
layout.tsx
...velopment/app-dir/serialize-circular-error/app/layout.tsx
+8
-0
page.tsx
...development/app-dir/serialize-circular-error/app/page.tsx
+22
-0
next.config.js
...velopment/app-dir/serialize-circular-error/next.config.js
+6
-0
serialize-circular-error.test.ts
...serialize-circular-error/serialize-circular-error.test.ts
+46
-0
No files found.
packages/next/src/lib/is-error.ts
View file @
a94cf1c9
...
@@ -19,6 +19,21 @@ export default function isError(err: unknown): err is NextError {
...
@@ -19,6 +19,21 @@ export default function isError(err: unknown): err is NextError {
)
)
}
}
function
safeStringify
(
obj
:
any
)
{
const
seen
=
new
WeakSet
()
return
JSON
.
stringify
(
obj
,
(
_key
,
value
)
=>
{
// If value is an object and already seen, replace with "[Circular]"
if
(
typeof
value
===
'object'
&&
value
!==
null
)
{
if
(
seen
.
has
(
value
))
{
return
'[Circular]'
}
seen
.
add
(
value
)
}
return
value
})
}
export
function
getProperError
(
err
:
unknown
):
Error
{
export
function
getProperError
(
err
:
unknown
):
Error
{
if
(
isError
(
err
))
{
if
(
isError
(
err
))
{
return
err
return
err
...
@@ -42,5 +57,5 @@ export function getProperError(err: unknown): Error {
...
@@ -42,5 +57,5 @@ export function getProperError(err: unknown): Error {
}
}
}
}
return
new
Error
(
isPlainObject
(
err
)
?
JSON
.
s
tringify
(
err
)
:
err
+
''
)
return
new
Error
(
isPlainObject
(
err
)
?
safeS
tringify
(
err
)
:
err
+
''
)
}
}
test/development/app-dir/serialize-circular-error/app/client/page.tsx
0 → 100644
View file @
a94cf1c9
'use client'
class
C
{
other
:
D
|
null
constructor
()
{
this
.
other
=
null
}
}
class
D
{
a
:
C
constructor
(
a
:
C
)
{
this
.
a
=
a
a
.
other
=
this
}
}
const
objC
=
new
C
()
const
objD
=
new
D
(
objC
)
export
default
function
Page
()
{
// eslint-disable-next-line no-throw-literal
throw
{
objC
,
objD
}
}
test/development/app-dir/serialize-circular-error/app/layout.tsx
0 → 100644
View file @
a94cf1c9
import
{
ReactNode
}
from
'react'
export
default
function
Root
({
children
}:
{
children
:
ReactNode
})
{
return
(
<
html
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/development/app-dir/serialize-circular-error/app/page.tsx
0 → 100644
View file @
a94cf1c9
class
A
{
other
:
B
|
null
constructor
()
{
this
.
other
=
null
}
}
class
B
{
a
:
A
constructor
(
a
:
A
)
{
this
.
a
=
a
a
.
other
=
this
}
}
const
objA
=
new
A
()
const
objB
=
new
B
(
objA
)
export
default
function
Page
()
{
// eslint-disable-next-line no-throw-literal
throw
{
objA
,
objB
}
}
test/development/app-dir/serialize-circular-error/next.config.js
0 → 100644
View file @
a94cf1c9
/**
* @type {import('next').NextConfig}
*/
const
nextConfig
=
{}
module
.
exports
=
nextConfig
test/development/app-dir/serialize-circular-error/serialize-circular-error.test.ts
0 → 100644
View file @
a94cf1c9
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
assertNoRedbox
,
assertHasRedbox
,
getRedboxDescription
,
}
from
'next-test-utils'
describe
(
'serialize-circular-error'
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
files
:
__dirname
,
})
it
(
'should serialize the object from server component in console correctly'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/'
)
await
assertHasRedbox
(
browser
)
const
description
=
await
getRedboxDescription
(
browser
)
// React cannot serialize thrown objects with circular references
expect
(
description
).
toBe
(
'[ Server ] Error: An error occurred but serializing the error message failed.'
)
const
output
=
next
.
cliOutput
expect
(
output
).
toContain
(
'Error: {"objA":{"other":{"a":"[Circular]"}},"objB":"[Circular]"}'
)
})
it
(
'should serialize the object from client component in console correctly'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/client'
)
// It's not a valid error object, it will display the global-error instead of the error overlay
// TODO: handle the error object in the client-side
await
assertNoRedbox
(
browser
)
const
bodyText
=
await
browser
.
elementByCss
(
'body'
).
text
()
expect
(
bodyText
).
toContain
(
'Application error: a client-side exception has occurred (see the browser console for more information).'
)
const
output
=
next
.
cliOutput
expect
(
output
).
toContain
(
'Error: {"objC":{"other":{"a":"[Circular]"}},"objD":"[Circular]"}'
)
})
})
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