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

Check if server error already had its message updated #46367

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/next/src/lib/format-server-error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { formatServerError } from './format-server-error'

describe('formatServerError', () => {
it('should not append message several times', () => {
const err = new Error(
'Class extends value undefined is not a constructor or null'
)

// Before
expect(err.message).toMatchInlineSnapshot(
`"Class extends value undefined is not a constructor or null"`
)

// After
formatServerError(err)
expect(err.message).toMatchInlineSnapshot(`
"Class extends value undefined is not a constructor or null

This might be caused by a React Class Component being rendered in a Server Component, React Class Components only works in Client Components. Read more: https://nextjs.org/docs/messages/class-component-in-server-component"
`)

// After second
formatServerError(err)
expect(err.message).toMatchInlineSnapshot(`
"Class extends value undefined is not a constructor or null

This might be caused by a React Class Component being rendered in a Server Component, React Class Components only works in Client Components. Read more: https://nextjs.org/docs/messages/class-component-in-server-component"
`)
})
})
8 changes: 7 additions & 1 deletion packages/next/src/lib/format-server-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ export function formatServerError(error: Error): void {
'Class extends value undefined is not a constructor or null'
)
) {
const addedMessage =
'This might be caused by a React Class Component being rendered in a Server Component, React Class Components only works in Client Components. Read more: https://nextjs.org/docs/messages/class-component-in-server-component'

// If this error instance already has the message, don't add it again
if (error.message.includes(addedMessage)) return

setMessage(
error,
`${error.message}

This might be caused by a React Class Component being rendered in a Server Component, React Class Components only works in Client Components. Read more: https://nextjs.org/docs/messages/class-component-in-server-component`
${addedMessage}`
)
return
}
Expand Down