Unverified Commit 7022a5fc authored by vercel[bot]'s avatar vercel[bot] Committed by GitHub

devtools: refine runtime error HMR protocol

Co-Authored-By: 's avatarMarcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
parent 12605194
......@@ -522,7 +522,7 @@ export function processMessage(
return
}
case HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE: {
case HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS: {
// Runtime error state is consumed by external HMR observers.
return
}
......
......@@ -403,7 +403,7 @@ function processMessage(message: HmrMessageSentToBrowser) {
dispatcher.onDevToolsConfig(message.data)
break
case HMR_MESSAGE_SENT_TO_BROWSER.CACHE_INDICATOR:
case HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE:
case HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS:
case HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_INSIGHTS_UPDATE:
case HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK:
case HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER:
......
......@@ -28,7 +28,7 @@ export function createRuntimeErrorStateReporter(
lastSerializedState = serializedState
const update: RuntimeErrorStateUpdate = {
event: HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERROR_STATE,
event: HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERRORS,
pathname,
errorState,
}
......
......@@ -945,6 +945,7 @@ export async function hydrate(opts?: { beforeRender?: () => Promise<void> }) {
// no longer rewrite the stack trace to a Node error.
else {
setTimeout(() => {
markErrorAsFatal(initialErr)
throw initialErr
})
}
......
......@@ -128,7 +128,7 @@ export function pageBootstrap(assetPrefix: string) {
case HMR_MESSAGE_SENT_TO_BROWSER.DEVTOOLS_CONFIG:
case HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK:
case HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_CURRENT_ERROR_STATE:
case HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE:
case HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS:
case HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_PAGE_METADATA:
case HMR_MESSAGE_SENT_TO_BROWSER.CACHE_INDICATOR:
case HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_INSIGHTS_UPDATE:
......
......@@ -114,8 +114,9 @@ function serializeRuntimeErrors(
errors: OverlayState['errors']
): SerializedRuntimeErrorState['errors'] {
// Serialize Error properties because they are non-enumerable.
return errors.map((errorEvent) => ({
return errors.map(({ isFatal, ...errorEvent }) => ({
...errorEvent,
fatal: isFatal,
error: errorEvent.error
? {
name: errorEvent.error.name,
......
......@@ -266,7 +266,7 @@ export class WebpackHotMiddleware {
return
}
if (message.type === HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE) {
if (message.type === HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS) {
if (message.errors.length === 0) {
this.runtimeErrorStates.delete(message.clientId)
} else {
......
......@@ -1610,7 +1610,7 @@ export async function createHotReloaderTurbopack(
break
}
case HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERROR_STATE: {
case HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERRORS: {
void runtimeErrorStateHandler.handle(parsedData).catch(() => {})
break
}
......@@ -1719,7 +1719,7 @@ export async function createHotReloaderTurbopack(
},
send(action) {
if (action.type === HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE) {
if (action.type === HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS) {
if (action.errors.length === 0) {
runtimeErrorStates.delete(action.clientId)
} else {
......
......@@ -42,7 +42,7 @@ export const enum HMR_MESSAGE_SENT_TO_BROWSER {
DEV_INDICATOR = 'devIndicator',
DEVTOOLS_CONFIG = 'devtoolsConfig',
REQUEST_CURRENT_ERROR_STATE = 'requestCurrentErrorState',
RUNTIME_ERROR_STATE = 'runtime-error-state',
RUNTIME_ERRORS = 'runtimeErrors',
REQUEST_PAGE_METADATA = 'requestPageMetadata',
REQUEST_INSIGHTS_UPDATE = 'requestInsightsUpdate',
......@@ -54,7 +54,7 @@ export const enum HMR_MESSAGE_SENT_TO_BROWSER {
export const enum HMR_MESSAGE_SENT_TO_SERVER {
// JSON messages:
MCP_ERROR_STATE_RESPONSE = 'mcp-error-state-response',
RUNTIME_ERROR_STATE = 'runtime-error-state',
RUNTIME_ERRORS = 'runtimeErrors',
MCP_PAGE_METADATA_RESPONSE = 'mcp-page-metadata-response',
PING = 'ping',
}
......@@ -215,11 +215,11 @@ export interface RuntimeErrorStateError {
column1: number | null
}[]
type: 'runtime' | 'recoverable' | 'console'
isFatal: boolean
fatal: boolean
}
export interface RuntimeErrorStateUpdate {
event: HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERROR_STATE
event: HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERRORS
pathname: string
errorState: {
errors: readonly RuntimeErrorStateError[]
......@@ -228,7 +228,7 @@ export interface RuntimeErrorStateUpdate {
}
export interface RuntimeErrorStateMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS
clientId: string
pathname: string
errors: FormattedRuntimeError[]
......
......@@ -1113,7 +1113,7 @@ export function onDemandEntryHandler({
handlePing(parsedData.page)
}
} else if (
parsedData.event === HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERROR_STATE
parsedData.event === HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERRORS
) {
void runtimeErrorStateHandler.handle(parsedData).catch(() => {})
} else if (
......
......@@ -48,7 +48,7 @@ function runtimeError(
},
frames: [],
type: 'runtime',
isFatal: fatal,
fatal,
}
}
......@@ -57,7 +57,7 @@ function update(
errors: readonly RuntimeErrorStateError[] = []
): RuntimeErrorStateUpdate {
return {
event: HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERROR_STATE,
event: HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERRORS,
pathname,
errorState: {
errors,
......@@ -122,7 +122,7 @@ describe('runtime error state handler', () => {
expect(send).toHaveBeenCalledTimes(1)
expect(send).toHaveBeenCalledWith({
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE,
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS,
clientId: TEST_CLIENT_ID,
pathname: '/new',
errors: [formattedError('new')],
......@@ -157,7 +157,7 @@ describe('runtime error state handler', () => {
expect(send).toHaveBeenCalledTimes(2)
expect(send).toHaveBeenLastCalledWith({
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE,
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS,
clientId: TEST_CLIENT_ID,
pathname: '/disconnected',
errors: [],
......@@ -183,7 +183,7 @@ describe('runtime error state handler', () => {
expect(format).toHaveBeenCalledTimes(2)
expect(send).toHaveBeenCalledWith({
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE,
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS,
clientId: TEST_CLIENT_ID,
pathname: '/retry',
errors: [formattedError('retry')],
......@@ -193,7 +193,7 @@ describe('runtime error state handler', () => {
it('formats each unchanged error only once across full snapshots', async () => {
const format = jest.fn(async (errors: readonly RuntimeErrorStateError[]) =>
errors.map((error) =>
formattedError(error.error?.message || 'Unknown error', error.isFatal)
formattedError(error.error?.message || 'Unknown error', error.fatal)
)
)
const send = jest.fn()
......@@ -209,7 +209,7 @@ describe('runtime error state handler', () => {
expect(format.mock.calls[0][0]).toEqual([first])
expect(format.mock.calls[1][0]).toEqual([second])
expect(send).toHaveBeenLastCalledWith({
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE,
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS,
clientId: TEST_CLIENT_ID,
pathname: '/replay',
errors: [formattedError('first'), formattedError('second')],
......@@ -245,7 +245,7 @@ describe('runtime error state handler', () => {
expect(send).toHaveBeenCalledTimes(1)
expect(send).toHaveBeenCalledWith({
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE,
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS,
clientId: TEST_CLIENT_ID,
pathname: '/second',
errors: [formattedError('first'), formattedError('second')],
......@@ -258,7 +258,7 @@ describe('runtime error state handler', () => {
async (errors: readonly RuntimeErrorStateError[]) => {
formattedCount += errors.length
return errors.map((error) =>
formattedError(error.error?.message || 'Unknown error', error.isFatal)
formattedError(error.error?.message || 'Unknown error', error.fatal)
)
}
)
......@@ -277,7 +277,7 @@ describe('runtime error state handler', () => {
it('reformats a promoted error whose fatality changed', async () => {
const format = jest.fn(async (errors: readonly RuntimeErrorStateError[]) =>
errors.map((error) =>
formattedError(error.error?.message || 'Unknown error', error.isFatal)
formattedError(error.error?.message || 'Unknown error', error.fatal)
)
)
const handler = createRuntimeErrorStateHandler(jest.fn(), format)
......@@ -451,7 +451,7 @@ describe('runtime error state handler', () => {
await expect(
handler.handle({
event: HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERROR_STATE,
event: HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERRORS,
})
).resolves.toBeUndefined()
await expect(
......
......@@ -134,7 +134,7 @@ export async function formatRuntimeErrors(
type: error.type,
errorName,
message,
fatal: error.isFatal,
fatal: error.fatal,
stack,
})
}
......@@ -164,7 +164,7 @@ function isRuntimeErrorStateError(
(value.type !== 'runtime' &&
value.type !== 'recoverable' &&
value.type !== 'console') ||
typeof value.isFatal !== 'boolean' ||
typeof value.fatal !== 'boolean' ||
!Array.isArray(value.frames)
) {
return false
......@@ -204,7 +204,7 @@ export function isRuntimeErrorStateUpdate(
): value is RuntimeErrorStateUpdate {
if (
!isRecord(value) ||
value.event !== HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERROR_STATE ||
value.event !== HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERRORS ||
typeof value.pathname !== 'string' ||
!value.pathname.startsWith('/') ||
value.pathname.includes('?') ||
......@@ -314,7 +314,7 @@ export function createRuntimeErrorStateHandler(
}
const message: RuntimeErrorStateMessage = {
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERROR_STATE,
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS,
clientId,
pathname: update.pathname,
errors,
......
const Page =
typeof window === 'undefined'
? function Page() {
return <p id="invalid-default-server-render">Server render</p>
}
: 42
export default Page
......@@ -12,7 +12,7 @@ describe('mcp-server get_errors tool', () => {
})
interface RuntimeErrorStateMessage {
type: 'runtime-error-state'
type: 'runtimeErrors'
clientId: string
pathname: string
errors: Array<{
......@@ -54,7 +54,7 @@ describe('mcp-server get_errors tool', () => {
if (typeof message === 'string') {
try {
const parsed = JSON.parse(message)
if (parsed.type === 'runtime-error-state') {
if (parsed.type === 'runtimeErrors') {
messages.push(parsed)
}
} catch {
......@@ -84,7 +84,7 @@ describe('mcp-server get_errors tool', () => {
socket.on('message', (data) => {
try {
const parsed = JSON.parse(data.toString())
if (parsed.type === 'runtime-error-state') {
if (parsed.type === 'runtimeErrors') {
messages.push(parsed)
} else if (parsed.type === 'sync') {
resolveInitialSync()
......@@ -228,9 +228,7 @@ describe('mcp-server get_errors tool', () => {
expect(JSON.stringify(collected.messages)).not.toContain('query-secret')
expect(JSON.stringify(collected.messages)).not.toContain('fragment-secret')
collected.send(
JSON.stringify({ event: 'runtime-error-state', clientId: 42 })
)
collected.send(JSON.stringify({ event: 'runtimeErrors', clientId: 42 }))
await browser.loadPage(`${next.url}/caught-runtime-error`, {
beforePageLoad: collected.beforePageLoad,
......@@ -773,6 +771,28 @@ describe('mcp-server get_errors tool', () => {
).toBe('Page remains rendered')
})
it('should treat an invalid Pages Router default export as fatal', async () => {
const pathname = '/pages-invalid-default-export'
const message =
'The default export is not a React Component in page: "/pages-invalid-default-export"'
const collected = collectRuntimeErrorStateMessages()
await next.browser(pathname, {
beforePageLoad: collected.beforePageLoad,
})
await waitForPushedRuntimeError(collected.messages, {
pathname,
message,
fatal: true,
})
await waitForRuntimeError({
url: pathname,
message,
fatal: true,
})
})
it('should treat an unrecoverable Pages Router module error as fatal', async () => {
const collected = collectRuntimeErrorStateMessages()
const browser = await next.browser('/pages-module-runtime-error', {
......
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