Unverified Commit f6f1f502 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Increase stack trace limit on the server (#43800)

Increases `Error.stackTraceLimit` on the server to match the client.
Adds `if (typeof window !== 'undefined') {` in `use-error-handler`,
otherwise it also affects the server - but only after that file is
compiled.

Closes NEXT-125

 ### Before

![image](https://user-images.githubusercontent.com/25056922/206123948-0c92009a-e5e8-4519-9862-1c1b83d88168.png)

### After

![image](https://user-images.githubusercontent.com/25056922/206124053-ba792463-76d4-457c-ac3b-d3e5b95b7bf9.png)


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 6ba53d8f
...@@ -22,9 +22,12 @@ function isHydrationError(error: Error): boolean { ...@@ -22,9 +22,12 @@ function isHydrationError(error: Error): boolean {
) )
} }
try { if (typeof window !== 'undefined') {
Error.stackTraceLimit = 50 try {
} catch {} // Increase the number of stack frames on the client
Error.stackTraceLimit = 50
} catch {}
}
const errorQueue: Array<Error> = [] const errorQueue: Array<Error> = []
const rejectionQueue: Array<Error> = [] const rejectionQueue: Array<Error> = []
......
...@@ -150,6 +150,10 @@ export default class DevServer extends Server { ...@@ -150,6 +150,10 @@ export default class DevServer extends Server {
} }
constructor(options: Options) { constructor(options: Options) {
try {
// Increase the number of stack frames on the server
Error.stackTraceLimit = 50
} catch {}
super({ ...options, dev: true }) super({ ...options, dev: true })
this.persistPatchedGlobals() this.persistPatchedGlobals()
this.renderOpts.dev = true this.renderOpts.dev = true
......
...@@ -1153,4 +1153,72 @@ describe('ReactRefreshLogBox app', () => { ...@@ -1153,4 +1153,72 @@ describe('ReactRefreshLogBox app', () => {
await cleanup() 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()
})
}) })
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