Unverified Commit a94cf1c9 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fix: handle logging cicurlar object (#72078)

parent 8b0c4e46
...@@ -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.stringify(err) : err + '') return new Error(isPlainObject(err) ? safeStringify(err) : err + '')
} }
'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 }
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
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 }
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
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]"}'
)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment