Skip to content

Commit

Permalink
Increase stack trace limit on the server (#43800)
Browse files Browse the repository at this point in the history
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: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
hanneslund and ijjk committed Dec 12, 2022
1 parent 6ba53d8 commit f6f1f50
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
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 @@ -150,6 +150,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()
})
})

0 comments on commit f6f1f50

Please sign in to comment.