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

[devtools] Remove unused `componentStackFrames` field (#82395)

parent 333489d8
......@@ -4,10 +4,7 @@ import './app-webpack'
import { renderAppDevOverlay } from 'next/dist/compiled/next-devtools'
import { appBootstrap } from './app-bootstrap'
import {
getComponentStack,
getOwnerStack,
} from '../next-devtools/userspace/app/errors/stitched-error'
import { getOwnerStack } from '../next-devtools/userspace/app/errors/stitched-error'
import { isRecoverableError } from './react-client-callbacks/on-recoverable-error'
// eslint-disable-next-line @next/internal/typechecked-require
......@@ -18,6 +15,6 @@ appBootstrap(() => {
try {
hydrate(instrumentationHooks)
} finally {
renderAppDevOverlay(getComponentStack, getOwnerStack, isRecoverableError)
renderAppDevOverlay(getOwnerStack, isRecoverableError)
}
})
......@@ -13,11 +13,11 @@ appBootstrap(() => {
hydrate(instrumentationHooks)
} finally {
if (process.env.NODE_ENV !== 'production') {
const { getComponentStack, getOwnerStack } =
const { getOwnerStack } =
require('../next-devtools/userspace/app/errors/stitched-error') as typeof import('../next-devtools/userspace/app/errors/stitched-error')
const { renderAppDevOverlay } =
require('next/dist/compiled/next-devtools') as typeof import('next/dist/compiled/next-devtools')
renderAppDevOverlay(getComponentStack, getOwnerStack, isRecoverableError)
renderAppDevOverlay(getOwnerStack, isRecoverableError)
}
}
})
......@@ -54,7 +54,7 @@ export function onCaughtError(
// We don't consider errors caught unless they're caught by an explicit error
// boundary. The built-in ones are considered implicit.
// This mimics how the same app would behave without Next.js.
return onUncaughtError(thrownValue, errorInfo)
return onUncaughtError(thrownValue)
}
// Skip certain custom errors which are not expected to be reported on client
......@@ -84,7 +84,7 @@ export function onCaughtError(
: `The above error occurred in one of your components.`
const errorLocation = `${componentErrorMessage} ${errorBoundaryMessage}`
const error = devToolErrorMod.decorateDevError(thrownValue, errorInfo)
const error = devToolErrorMod.decorateDevError(thrownValue)
// Log and report the error with location but without modifying the error stack
devToolErrorMod.originConsoleError('%o\n\n%s', thrownValue, errorLocation)
......@@ -95,15 +95,12 @@ export function onCaughtError(
}
}
export function onUncaughtError(
thrownValue: unknown,
errorInfo: React.ErrorInfo
) {
export function onUncaughtError(thrownValue: unknown) {
// Skip certain custom errors which are not expected to be reported on client
if (isBailoutToCSRError(thrownValue) || isNextRouterError(thrownValue)) return
if (process.env.NODE_ENV !== 'production') {
const error = devToolErrorMod.decorateDevError(thrownValue, errorInfo)
const error = devToolErrorMod.decorateDevError(thrownValue)
// TODO: Add an adendum to the overlay telling people about custom error boundaries.
reportGlobalError(error)
......
......@@ -12,8 +12,7 @@ export function isRecoverableError(error: Error): boolean {
}
export const onRecoverableError: HydrationOptions['onRecoverableError'] = (
error,
errorInfo
error
) => {
// x-ref: https://github.com/facebook/react/pull/28736
let cause = isError(error) && 'cause' in error ? error.cause : error
......@@ -23,7 +22,7 @@ export const onRecoverableError: HydrationOptions['onRecoverableError'] = (
if (process.env.NODE_ENV !== 'production') {
const { decorateDevError } =
require('../../next-devtools/userspace/app/errors/stitched-error') as typeof import('../../next-devtools/userspace/app/errors/stitched-error')
const causeError = decorateDevError(cause, errorInfo)
const causeError = decorateDevError(cause)
recoverableErrors.add(causeError)
cause = causeError
}
......
......@@ -185,13 +185,11 @@ function replayQueuedEvents(dispatch: NonNullable<typeof maybeDispatch>) {
}
function DevOverlayRoot({
getComponentStack,
getOwnerStack,
getSquashedHydrationErrorDetails,
isRecoverableError,
routerType,
}: {
getComponentStack: (error: Error) => string | undefined
getOwnerStack: (error: Error) => string | null | undefined
getSquashedHydrationErrorDetails: (error: Error) => HydrationErrorState | null
isRecoverableError: (error: Error) => boolean
......@@ -199,7 +197,6 @@ function DevOverlayRoot({
}) {
const [state, dispatch] = useErrorOverlayReducer(
routerType,
getComponentStack,
getOwnerStack,
isRecoverableError
)
......@@ -254,7 +251,6 @@ function getSquashedHydrationErrorDetailsApp() {
}
export function renderAppDevOverlay(
getComponentStack: (error: Error) => string | undefined,
getOwnerStack: (error: Error) => string | null | undefined,
isRecoverableError: (error: Error) => boolean
): void {
......@@ -293,7 +289,6 @@ export function renderAppDevOverlay(
// At least it won't unmount any user code if it errors.
root.render(
<DevOverlayRoot
getComponentStack={getComponentStack}
getOwnerStack={getOwnerStack}
getSquashedHydrationErrorDetails={getSquashedHydrationErrorDetailsApp}
isRecoverableError={isRecoverableError}
......@@ -307,7 +302,6 @@ export function renderAppDevOverlay(
}
export function renderPagesDevOverlay(
getComponentStack: (error: Error) => string | undefined,
getOwnerStack: (error: Error) => string | null | undefined,
getSquashedHydrationErrorDetails: (
error: Error
......@@ -357,7 +351,6 @@ export function renderPagesDevOverlay(
// At least it won't unmount any user code if it errors.
root.render(
<DevOverlayRoot
getComponentStack={getComponentStack}
getOwnerStack={getOwnerStack}
getSquashedHydrationErrorDetails={getSquashedHydrationErrorDetails}
isRecoverableError={isRecoverableError}
......
......@@ -21,7 +21,6 @@ export const Default: Story = {
runtime: true,
error: new Error('First error message'),
frames: () => Promise.resolve([]),
componentStackFrames: undefined,
type: 'runtime',
},
{
......@@ -29,7 +28,6 @@ export const Default: Story = {
runtime: true,
error: new Error('Second error message'),
frames: () => Promise.resolve([]),
componentStackFrames: undefined,
type: 'runtime',
},
{
......@@ -37,7 +35,6 @@ export const Default: Story = {
runtime: true,
error: new Error('Third error message'),
frames: () => Promise.resolve([]),
componentStackFrames: undefined,
type: 'runtime',
},
],
......
......@@ -22,7 +22,6 @@ const mockErrors: ReadyRuntimeError[] = [
runtime: true as const,
error: new Error('First error'),
frames: () => Promise.resolve([]),
componentStackFrames: undefined,
type: 'runtime',
},
{
......@@ -30,7 +29,6 @@ const mockErrors: ReadyRuntimeError[] = [
runtime: true as const,
error: new Error('Second error'),
frames: () => Promise.resolve([]),
componentStackFrames: undefined,
type: 'runtime',
},
{
......@@ -38,7 +36,6 @@ const mockErrors: ReadyRuntimeError[] = [
runtime: true as const,
error: new Error('Third error'),
frames: () => Promise.resolve([]),
componentStackFrames: undefined,
type: 'runtime',
},
]
......
......@@ -89,22 +89,6 @@ export const WithHydrationWarning: Story = {
},
},
]),
componentStackFrames: [
{
component: 'MyComponent',
file: 'app/page.tsx',
line1: 10,
column1: 5,
canOpenInEditor: true,
},
{
component: 'ParentComponent',
file: 'app/layout.tsx',
line1: 20,
column1: 3,
canOpenInEditor: true,
},
],
type: 'runtime',
},
],
......
......@@ -6,14 +6,12 @@ import {
getErrorByType,
type ReadyRuntimeError,
} from '../../utils/get-error-by-type'
import type { ComponentStackFrame } from '../../utils/parse-component-stack'
import { usePersistentCacheErrorDetection } from '../../components/errors/error-overlay-toolbar/restart-server-button'
export type SupportedErrorEvent = {
id: number
error: Error
frames: readonly StackFrame[]
componentStackFrames?: readonly ComponentStackFrame[]
type: 'runtime' | 'recoverable' | 'console'
}
......
......@@ -2,7 +2,6 @@ import { useReducer } from 'react'
import type { VersionInfo } from '../../server/dev/parse-version-info'
import type { SupportedErrorEvent } from './container/runtime-error/render-error'
import { parseComponentStack } from './utils/parse-component-stack'
import type { DebugInfo } from '../shared/types'
import type { DevIndicatorServerState } from '../../server/dev/dev-indicator-server-state'
import { parseStack } from '../../server/lib/parse-stack'
......@@ -319,7 +318,6 @@ function getInitialState(
export function useErrorOverlayReducer(
routerType: 'pages' | 'app',
getComponentStack: (error: Error) => string | undefined,
getOwnerStack: (error: Error) => string | null | undefined,
isRecoverableError: (error: Error) => boolean
) {
......@@ -328,18 +326,12 @@ export function useErrorOverlayReducer(
id: number,
error: Error
): readonly SupportedErrorEvent[] {
const componentStack = getComponentStack(error)
const componentStackFrames =
componentStack === undefined
? undefined
: parseComponentStack(componentStack)
const ownerStack = getOwnerStack(error)
const frames = parseStack((error.stack || '') + (ownerStack || ''))
const pendingEvent: SupportedErrorEvent = {
id,
error,
frames,
componentStackFrames,
type: isRecoverableError(error)
? 'recoverable'
: isConsoleError(error)
......
......@@ -62,15 +62,6 @@ export const errors: SupportedErrorEvent[] = [
error: Object.assign(new Error('First error message'), {
__NEXT_ERROR_CODE: 'E001',
}),
componentStackFrames: [
{
file: 'app/page.tsx',
component: 'Home',
line1: 10,
column1: 5,
canOpenInEditor: true,
},
],
frames: [
{
file: 'app/page.tsx',
......@@ -126,7 +117,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
},
...Array(20).fill(ignoredFrame),
]),
componentStackFrames: undefined,
type: 'runtime',
},
{
......@@ -145,7 +135,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Second error message'),
},
]),
componentStackFrames: undefined,
type: 'console',
},
{
......@@ -164,7 +153,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Third error message'),
},
]),
componentStackFrames: undefined,
type: 'recoverable',
},
{
......@@ -183,7 +171,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('typeof window !== undefined'),
},
]),
componentStackFrames: undefined,
type: 'runtime',
},
{
......@@ -208,7 +195,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Fifth error message'),
},
]),
componentStackFrames: undefined,
type: 'console',
},
{
......@@ -227,7 +213,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Sixth error message'),
},
]),
componentStackFrames: undefined,
type: 'recoverable',
},
{
......@@ -246,7 +231,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Sixth error message'),
},
]),
componentStackFrames: undefined,
type: 'runtime',
},
{
......@@ -265,7 +249,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Eighth error message'),
},
]),
componentStackFrames: undefined,
type: 'runtime',
},
{
......@@ -284,7 +267,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Ninth error message'),
},
]),
componentStackFrames: undefined,
type: 'runtime',
},
{
......@@ -303,7 +285,6 @@ export const runtimeErrors: ReadyRuntimeError[] = [
originalCodeFrame: originalCodeFrame('Tenth error message'),
},
]),
componentStackFrames: undefined,
type: 'runtime',
},
]
import type { SupportedErrorEvent } from '../container/runtime-error/render-error'
import { getOriginalStackFrames } from '../../shared/stack-frame'
import type { OriginalStackFrame } from '../../shared/stack-frame'
import type { ComponentStackFrame } from './parse-component-stack'
import { getErrorSource } from '../../../shared/lib/error-source'
import React from 'react'
......@@ -12,7 +11,6 @@ export type ReadyRuntimeError = {
frames:
| readonly OriginalStackFrame[]
| (() => Promise<readonly OriginalStackFrame[]>)
componentStackFrames: readonly ComponentStackFrame[] | undefined
type: 'runtime' | 'console' | 'recoverable'
}
......@@ -51,7 +49,6 @@ export async function getErrorByType(
runtime: true,
error: event.error,
type: event.type,
componentStackFrames: event.componentStackFrames,
} as const
if ('use' in React) {
......
export type ComponentStackFrame = {
canOpenInEditor: boolean
component: string
file: string | null
line1: number | null
column1: number | null
}
enum LocationType {
FILE = 'file',
WEBPACK_INTERNAL = 'webpack-internal',
HTTP = 'http',
PROTOCOL_RELATIVE = 'protocol-relative',
UNKNOWN = 'unknown',
}
/**
* Get the type of frame line based on the location
*/
function getLocationType(location: string): LocationType {
if (location.startsWith('file://')) {
return LocationType.FILE
}
if (location.includes('webpack-internal://')) {
return LocationType.WEBPACK_INTERNAL
}
if (location.startsWith('http://') || location.startsWith('https://')) {
return LocationType.HTTP
}
if (location.startsWith('//')) {
return LocationType.PROTOCOL_RELATIVE
}
return LocationType.UNKNOWN
}
function parseStackFrameLocation(
location: string
): Omit<ComponentStackFrame, 'component'> {
const locationType = getLocationType(location)
const modulePath = location?.replace(
/^(webpack-internal:\/\/\/|file:\/\/)(\(.*\)\/)?/,
''
)
const [, file, line1, column1] = modulePath?.match(/^(.+):(\d+):(\d+)/) ?? []
switch (locationType) {
case LocationType.FILE:
case LocationType.WEBPACK_INTERNAL:
return {
canOpenInEditor: true,
file,
line1: line1 ? Number(line1) : null,
column1: column1 ? Number(column1) : null,
}
// When the location is a URL we only show the file
// TODO: Resolve http(s) URLs through sourcemaps
case LocationType.HTTP:
case LocationType.PROTOCOL_RELATIVE:
case LocationType.UNKNOWN:
default: {
return {
canOpenInEditor: false,
file: null,
line1: null,
column1: null,
}
}
}
}
export function parseComponentStack(
componentStack: string
): ComponentStackFrame[] {
const componentStackFrames: ComponentStackFrame[] = []
for (const line of componentStack.trim().split('\n')) {
// TODO: support safari stack trace
// Get component and file from the component stack line
const match = /at ([^ ]+)( \((.*)\))?/.exec(line)
if (match?.[1]) {
const component = match[1]
const location = match[3]
if (!location) {
componentStackFrames.push({
canOpenInEditor: false,
component,
file: null,
line1: null,
column1: null,
})
continue
}
// Stop parsing the component stack if we reach a Next.js component
if (location?.includes('next/dist')) {
break
}
const frameLocation = parseStackFrameLocation(location)
componentStackFrames.push({
component,
...frameLocation,
})
}
}
return componentStackFrames
}
......@@ -2,14 +2,6 @@ import React from 'react'
import isError from '../../../../lib/is-error'
const ownerStacks = new WeakMap<Error, string | null>()
const componentStacks = new WeakMap<Error, string>()
export function getComponentStack(error: Error): string | undefined {
return componentStacks.get(error)
}
export function setComponentStack(error: Error, stack: string) {
componentStacks.set(error, stack)
}
export function getOwnerStack(error: Error): string | null | undefined {
return ownerStacks.get(error)
......@@ -29,16 +21,8 @@ export function setOwnerStackIfAvailable(error: Error): void {
}
}
export function decorateDevError(
thrownValue: unknown,
errorInfo: React.ErrorInfo
) {
export function decorateDevError(thrownValue: unknown) {
const error = coerceError(thrownValue)
setOwnerStackIfAvailable(error)
// TODO: change to passing down errorInfo later
// In development mode, pass along the component stack to the error
if (errorInfo.componentStack) {
setComponentStack(error, errorInfo.componentStack)
}
return error
}
......@@ -6,7 +6,7 @@ import {
storeHydrationErrorStateFromConsoleArgs,
} from './hydration-error-state'
import { Router } from '../../../client/router'
import { getComponentStack, getOwnerStack } from '../app/errors/stitched-error'
import { getOwnerStack } from '../app/errors/stitched-error'
import { isRecoverableError } from '../../../client/react-client-callbacks/on-recoverable-error'
import { getSquashedHydrationErrorDetails } from './hydration-error-state'
import { PagesDevOverlayErrorBoundary } from './pages-dev-overlay-error-boundary'
......@@ -23,7 +23,6 @@ const usePagesDevOverlayBridge = () => {
// NDT uses a different React instance so it's not technically a state update
// scheduled from useInsertionEffect.
renderPagesDevOverlay(
getComponentStack,
getOwnerStack,
getSquashedHydrationErrorDetails,
isRecoverableError
......
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