Skip to content

Commit

Permalink
hide stack trace in CanaryOnlyError
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Nov 15, 2024
1 parent a72d79c commit 5468469
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/next/src/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ describe('loadConfig', () => {
delete process.env.__NEXT_VERSION
})

it('should not print a stack trace when throwing an error', async () => {
const loadConfigPromise = loadConfig('', __dirname, {
customConfig: {
experimental: {
ppr: true,
},
},
})

await expect(loadConfigPromise).rejects.toThrow(
/The experimental feature "experimental.ppr" can only be enabled when using the latest canary version of Next.js./
)

try {
await loadConfigPromise
} catch (error: any) {
expect(error).toBeInstanceOf(Error)

// Check that there's no stack trace
expect(error.stack).toBeUndefined()
}
})

it('errors when using PPR if not in canary', async () => {
await expect(
loadConfig('', __dirname, {
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1283,5 +1283,8 @@ class CanaryOnlyError extends Error {
super(
`The experimental feature "${feature}" can only be enabled when using the latest canary version of Next.js.`
)
// This error is meant to interrupt the server start/build process
// but the stack trace isn't meaningful, as it points to internal code.
this.stack = undefined
}
}

0 comments on commit 5468469

Please sign in to comment.