Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase stack trace limit on the server #43800

Merged
merged 11 commits into from Dec 12, 2022
Expand Up @@ -22,9 +22,12 @@ function isHydrationError(error: Error): boolean {
)
}

try {
Error.stackTraceLimit = 50
} catch {}
if (typeof window !== 'undefined') {
try {
// Increase the number of stack frames on the client
Error.stackTraceLimit = 50
} catch {}
}

const errorQueue: Array<Error> = []
const rejectionQueue: Array<Error> = []
Expand Down
4 changes: 4 additions & 0 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -149,6 +149,10 @@ export default class DevServer extends Server {
}

constructor(options: Options) {
try {
// Increase the number of stack frames on the server
Error.stackTraceLimit = 50
} catch {}
super({ ...options, dev: true })
this.persistPatchedGlobals()
this.renderOpts.dev = true
Expand Down
68 changes: 68 additions & 0 deletions test/development/acceptance-app/ReactRefreshLogBox.test.ts
Expand Up @@ -1153,4 +1153,72 @@ describe('ReactRefreshLogBox app', () => {

await cleanup()
})

test('Call stack count is correct for server error', async () => {
const { session, browser, cleanup } = await sandbox(
next,
new Map([
[
'app/page.js',
`
export default function Page() {
throw new Error('Server error')
}
`,
],
])
)

expect(await session.hasRedbox(true)).toBe(true)

// Open full Call Stack
await browser
.elementByCss('[data-nextjs-data-runtime-error-collapsed-action]')
.click()
const callStackCount = (
await browser.elementsByCss('[data-nextjs-call-stack-frame]')
).length

// Expect more than the default amount of frames
// The default stackTraceLimit results in max 9 [data-nextjs-call-stack-frame] elements
expect(callStackCount).toBeGreaterThan(9)

await cleanup()
})

test('Call stack count is correct for client error', async () => {
const { session, browser, cleanup } = await sandbox(
next,
new Map([
[
'app/page.js',
`
'use client'
export default function Page() {
if (typeof window !== 'undefined') {
throw new Error('Client error')
}
return null
}
`,
],
])
)

expect(await session.hasRedbox(true)).toBe(true)

// Open full Call Stack
await browser
.elementByCss('[data-nextjs-data-runtime-error-collapsed-action]')
.click()
const callStackCount = (
await browser.elementsByCss('[data-nextjs-call-stack-frame]')
).length

// Expect more than the default amount of frames
// The default stackTraceLimit results in max 9 [data-nextjs-call-stack-frame] elements
expect(callStackCount).toBeGreaterThan(9)

await cleanup()
})
})