Skip to content

Commit

Permalink
hide stack trace in CanaryOnlyError (#72859)
Browse files Browse the repository at this point in the history
This error doesn't point to any particularly meaningful line of code to
the user, we just use it to have a well-known error message for when a
feature should only be used in canary. This hides the stack trace so the
error is surfaced more clearly.
  • Loading branch information
ztanner authored Nov 15, 2024
1 parent a72d79c commit b5f5495
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 b5f5495

Please sign in to comment.