Unverified Commit 81ab1ffc authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[dev-overlay] Inject `isRecoverableError` implementation (#80003)

Same as with `get*Stack` implementation
parent d14df6cc
...@@ -45,6 +45,7 @@ import reportHmrLatency from '../utils/report-hmr-latency' ...@@ -45,6 +45,7 @@ import reportHmrLatency from '../utils/report-hmr-latency'
import { TurbopackHmr } from '../utils/turbopack-hot-reloader-common' import { TurbopackHmr } from '../utils/turbopack-hot-reloader-common'
import { NEXT_HMR_REFRESH_HASH_COOKIE } from '../../app-router-headers' import { NEXT_HMR_REFRESH_HASH_COOKIE } from '../../app-router-headers'
import { getComponentStack, getOwnerStack } from '../../errors/stitched-error' import { getComponentStack, getOwnerStack } from '../../errors/stitched-error'
import { isRecoverableError } from '../../../react-client-callbacks/on-recoverable-error'
export interface Dispatcher { export interface Dispatcher {
onBuildOk(): void onBuildOk(): void
...@@ -483,7 +484,8 @@ export default function HotReload({ ...@@ -483,7 +484,8 @@ export default function HotReload({
const [state, dispatch] = useErrorOverlayReducer( const [state, dispatch] = useErrorOverlayReducer(
'app', 'app',
getComponentStack, getComponentStack,
getOwnerStack getOwnerStack,
isRecoverableError
) )
const dispatcher = useMemo<Dispatcher>(() => { const dispatcher = useMemo<Dispatcher>(() => {
......
...@@ -3,12 +3,14 @@ import * as Bus from './bus' ...@@ -3,12 +3,14 @@ import * as Bus from './bus'
import { useErrorOverlayReducer } from '../shared' import { useErrorOverlayReducer } from '../shared'
import { Router } from '../../../router' import { Router } from '../../../router'
import { getComponentStack, getOwnerStack } from '../../errors/stitched-error' import { getComponentStack, getOwnerStack } from '../../errors/stitched-error'
import { isRecoverableError } from '../../../react-client-callbacks/on-recoverable-error'
export const usePagesDevOverlay = () => { export const usePagesDevOverlay = () => {
const [state, dispatch] = useErrorOverlayReducer( const [state, dispatch] = useErrorOverlayReducer(
'pages', 'pages',
getComponentStack, getComponentStack,
getOwnerStack getOwnerStack,
isRecoverableError
) )
React.useEffect(() => { React.useEffect(() => {
......
...@@ -7,6 +7,7 @@ import type { DebugInfo } from './types' ...@@ -7,6 +7,7 @@ import type { DebugInfo } from './types'
import type { DevIndicatorServerState } from '../../../server/dev/dev-indicator-server-state' import type { DevIndicatorServerState } from '../../../server/dev/dev-indicator-server-state'
import type { HMR_ACTION_TYPES } from '../../../server/dev/hot-reloader-types' import type { HMR_ACTION_TYPES } from '../../../server/dev/hot-reloader-types'
import { parseStack } from './utils/parse-stack' import { parseStack } from './utils/parse-stack'
import { isConsoleError } from '../errors/console-error'
type FastRefreshState = type FastRefreshState =
/** No refresh in progress. */ /** No refresh in progress. */
...@@ -169,7 +170,8 @@ function getInitialState( ...@@ -169,7 +170,8 @@ function getInitialState(
export function useErrorOverlayReducer( export function useErrorOverlayReducer(
routerType: 'pages' | 'app', routerType: 'pages' | 'app',
getComponentStack: (error: Error) => string | undefined, getComponentStack: (error: Error) => string | undefined,
getOwnerStack: (error: Error) => string | null | undefined getOwnerStack: (error: Error) => string | null | undefined,
isRecoverableError: (error: Error) => boolean
) { ) {
function pushErrorFilterDuplicates( function pushErrorFilterDuplicates(
events: SupportedErrorEvent[], events: SupportedErrorEvent[],
...@@ -188,6 +190,11 @@ export function useErrorOverlayReducer( ...@@ -188,6 +190,11 @@ export function useErrorOverlayReducer(
error, error,
frames, frames,
componentStackFrames, componentStackFrames,
type: isRecoverableError(error)
? 'recoverable'
: isConsoleError(error)
? 'console'
: 'runtime',
} }
const pendingEvents = events.filter((event) => { const pendingEvents = events.filter((event) => {
// Filter out duplicate errors // Filter out duplicate errors
......
...@@ -21,18 +21,21 @@ export const Default: Story = { ...@@ -21,18 +21,21 @@ export const Default: Story = {
runtime: true, runtime: true,
error: new Error('First error message'), error: new Error('First error message'),
frames: () => Promise.resolve([]), frames: () => Promise.resolve([]),
type: 'runtime',
}, },
{ {
id: 1, id: 1,
runtime: true, runtime: true,
error: new Error('Second error message'), error: new Error('Second error message'),
frames: () => Promise.resolve([]), frames: () => Promise.resolve([]),
type: 'runtime',
}, },
{ {
id: 2, id: 2,
runtime: true, runtime: true,
error: new Error('Third error message'), error: new Error('Third error message'),
frames: () => Promise.resolve([]), frames: () => Promise.resolve([]),
type: 'runtime',
}, },
], ],
activeIdx: 1, activeIdx: 1,
......
...@@ -2,6 +2,7 @@ import type { Meta, StoryObj } from '@storybook/react' ...@@ -2,6 +2,7 @@ import type { Meta, StoryObj } from '@storybook/react'
import { ErrorOverlayPagination } from './error-overlay-pagination' import { ErrorOverlayPagination } from './error-overlay-pagination'
import { withShadowPortal } from '../../../storybook/with-shadow-portal' import { withShadowPortal } from '../../../storybook/with-shadow-portal'
import { useState } from 'react' import { useState } from 'react'
import type { ReadyRuntimeError } from '../../../../utils/get-error-by-type'
const meta: Meta<typeof ErrorOverlayPagination> = { const meta: Meta<typeof ErrorOverlayPagination> = {
component: ErrorOverlayPagination, component: ErrorOverlayPagination,
...@@ -15,24 +16,27 @@ export default meta ...@@ -15,24 +16,27 @@ export default meta
type Story = StoryObj<typeof ErrorOverlayPagination> type Story = StoryObj<typeof ErrorOverlayPagination>
// Mock errors for stories // Mock errors for stories
const mockErrors = [ const mockErrors: ReadyRuntimeError[] = [
{ {
id: 1, id: 1,
runtime: true as const, runtime: true as const,
error: new Error('First error'), error: new Error('First error'),
frames: () => Promise.resolve([]), frames: () => Promise.resolve([]),
type: 'runtime',
}, },
{ {
id: 2, id: 2,
runtime: true as const, runtime: true as const,
error: new Error('Second error'), error: new Error('Second error'),
frames: () => Promise.resolve([]), frames: () => Promise.resolve([]),
type: 'runtime',
}, },
{ {
id: 3, id: 3,
runtime: true as const, runtime: true as const,
error: new Error('Third error'), error: new Error('Third error'),
frames: () => Promise.resolve([]), frames: () => Promise.resolve([]),
type: 'runtime',
}, },
] ]
......
...@@ -96,6 +96,7 @@ const runtimeErrors: ReadyRuntimeError[] = [ ...@@ -96,6 +96,7 @@ const runtimeErrors: ReadyRuntimeError[] = [
}, },
...Array(20).fill(ignoredFrame), ...Array(20).fill(ignoredFrame),
]), ]),
type: 'runtime',
}, },
{ {
id: 2, id: 2,
...@@ -113,6 +114,7 @@ const runtimeErrors: ReadyRuntimeError[] = [ ...@@ -113,6 +114,7 @@ const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Second error message'), originalCodeFrame: originalCodeFrame('Second error message'),
}, },
]), ]),
type: 'runtime',
}, },
{ {
id: 3, id: 3,
...@@ -130,6 +132,7 @@ const runtimeErrors: ReadyRuntimeError[] = [ ...@@ -130,6 +132,7 @@ const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Third error message'), originalCodeFrame: originalCodeFrame('Third error message'),
}, },
]), ]),
type: 'runtime',
}, },
{ {
id: 4, id: 4,
...@@ -147,6 +150,7 @@ const runtimeErrors: ReadyRuntimeError[] = [ ...@@ -147,6 +150,7 @@ const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('typeof window !== undefined'), originalCodeFrame: originalCodeFrame('typeof window !== undefined'),
}, },
]), ]),
type: 'runtime',
}, },
] ]
...@@ -232,6 +236,7 @@ export const WithHydrationWarning: Story = { ...@@ -232,6 +236,7 @@ export const WithHydrationWarning: Story = {
}, },
}, },
]), ]),
type: 'runtime',
}, },
], ],
debugInfo: { devtoolsFrontendUrl: undefined }, debugInfo: { devtoolsFrontendUrl: undefined },
......
...@@ -5,7 +5,6 @@ import { RuntimeError } from './runtime-error' ...@@ -5,7 +5,6 @@ import { RuntimeError } from './runtime-error'
import { getErrorSource } from '../../../../../shared/lib/error-source' import { getErrorSource } from '../../../../../shared/lib/error-source'
import { HotlinkedText } from '../components/hot-linked-text' import { HotlinkedText } from '../components/hot-linked-text'
import { PseudoHtmlDiff } from './runtime-error/component-stack-pseudo-html' import { PseudoHtmlDiff } from './runtime-error/component-stack-pseudo-html'
import { isConsoleError } from '../../../errors/console-error'
import { extractNextErrorCode } from '../../../../../lib/error-telemetry-utils' import { extractNextErrorCode } from '../../../../../lib/error-telemetry-utils'
import { import {
ErrorOverlayLayout, ErrorOverlayLayout,
...@@ -19,7 +18,6 @@ import { ...@@ -19,7 +18,6 @@ import {
import type { ReadyRuntimeError } from '../../utils/get-error-by-type' import type { ReadyRuntimeError } from '../../utils/get-error-by-type'
import type { ErrorBaseProps } from '../components/errors/error-overlay/error-overlay' import type { ErrorBaseProps } from '../components/errors/error-overlay/error-overlay'
import { getSquashedHydrationErrorDetails } from '../../pages/hydration-error-state' import { getSquashedHydrationErrorDetails } from '../../pages/hydration-error-state'
import { isRecoverableError } from '../../../../react-client-callbacks/on-recoverable-error'
export interface ErrorsProps extends ErrorBaseProps { export interface ErrorsProps extends ErrorBaseProps {
runtimeErrors: ReadyRuntimeError[] runtimeErrors: ReadyRuntimeError[]
...@@ -56,11 +54,14 @@ function GenericErrorDescription({ error }: { error: Error }) { ...@@ -56,11 +54,14 @@ function GenericErrorDescription({ error }: { error: Error }) {
) )
} }
function getErrorType(error: Error): ErrorOverlayLayoutProps['errorType'] { function getErrorTypeLabel(
if (isRecoverableError(error)) { error: Error,
type: ReadyRuntimeError['type']
): ErrorOverlayLayoutProps['errorType'] {
if (type === 'recoverable') {
return `Recoverable ${error.name}` return `Recoverable ${error.name}`
} }
if (isConsoleError(error)) { if (type === 'console') {
return `Console ${error.name}` return `Console ${error.name}`
} }
return `Runtime ${error.name}` return `Runtime ${error.name}`
...@@ -141,7 +142,7 @@ export function Errors({ ...@@ -141,7 +142,7 @@ export function Errors({
const isServerError = ['server', 'edge-server'].includes( const isServerError = ['server', 'edge-server'].includes(
getErrorSource(error) || '' getErrorSource(error) || ''
) )
const errorType = getErrorType(error) const errorType = getErrorTypeLabel(error, activeError.type)
// TOOD: May be better to always treat everything past the first blank line as notes // TOOD: May be better to always treat everything past the first blank line as notes
// We're currently only special casing hydration error messages. // We're currently only special casing hydration error messages.
const notes = errorDetails.notes const notes = errorDetails.notes
......
...@@ -13,6 +13,7 @@ export type SupportedErrorEvent = { ...@@ -13,6 +13,7 @@ export type SupportedErrorEvent = {
error: Error error: Error
frames: StackFrame[] frames: StackFrame[]
componentStackFrames?: ComponentStackFrame[] componentStackFrames?: ComponentStackFrame[]
type: 'runtime' | 'recoverable' | 'console'
} }
type Props = { type Props = {
......
...@@ -52,6 +52,7 @@ const initialState: OverlayState = { ...@@ -52,6 +52,7 @@ const initialState: OverlayState = {
column: 5, column: 5,
}, },
], ],
type: 'runtime',
}, },
{ {
id: 2, id: 2,
...@@ -59,6 +60,7 @@ const initialState: OverlayState = { ...@@ -59,6 +60,7 @@ const initialState: OverlayState = {
__NEXT_ERROR_CODE: 'E002', __NEXT_ERROR_CODE: 'E002',
}), }),
frames: [], frames: [],
type: 'runtime',
}, },
{ {
id: 3, id: 3,
...@@ -66,6 +68,7 @@ const initialState: OverlayState = { ...@@ -66,6 +68,7 @@ const initialState: OverlayState = {
__NEXT_ERROR_CODE: 'E003', __NEXT_ERROR_CODE: 'E003',
}), }),
frames: [], frames: [],
type: 'runtime',
}, },
], ],
refreshState: { type: 'idle' }, refreshState: { type: 'idle' },
......
...@@ -11,6 +11,7 @@ export type ReadyRuntimeError = { ...@@ -11,6 +11,7 @@ export type ReadyRuntimeError = {
error: Error & { environmentName?: string } error: Error & { environmentName?: string }
frames: OriginalStackFrame[] | (() => Promise<OriginalStackFrame[]>) frames: OriginalStackFrame[] | (() => Promise<OriginalStackFrame[]>)
componentStackFrames?: ComponentStackFrame[] componentStackFrames?: ComponentStackFrame[]
type: 'runtime' | 'console' | 'recoverable'
} }
export const useFrames = (error: ReadyRuntimeError): OriginalStackFrame[] => { export const useFrames = (error: ReadyRuntimeError): OriginalStackFrame[] => {
...@@ -43,6 +44,7 @@ export async function getErrorByType( ...@@ -43,6 +44,7 @@ export async function getErrorByType(
id: event.id, id: event.id,
runtime: true, runtime: true,
error: event.error, error: event.error,
type: event.type,
} as const } as const
if ('use' in React) { if ('use' in React) {
......
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