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