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

[dev-overlay] Unify error deduplication logic (#78017)

parent 2a594945
// Dedupe the two consecutive errors: If the previous one is same as current one, ignore the current one.
export function enqueueConsecutiveDedupedError(
queue: Array<Error>,
error: Error
) {
const previousError = queue[queue.length - 1]
// Compare the error stack to dedupe the consecutive errors
if (previousError && previousError.stack === error.stack) {
return
}
queue.push(error)
}
...@@ -5,7 +5,6 @@ import { storeHydrationErrorStateFromConsoleArgs } from './hydration-error-info' ...@@ -5,7 +5,6 @@ import { storeHydrationErrorStateFromConsoleArgs } from './hydration-error-info'
import { formatConsoleArgs, parseConsoleArgs } from '../../lib/console' import { formatConsoleArgs, parseConsoleArgs } from '../../lib/console'
import isError from '../../../lib/is-error' import isError from '../../../lib/is-error'
import { createConsoleError } from './console-error' import { createConsoleError } from './console-error'
import { enqueueConsecutiveDedupedError } from './enqueue-client-error'
import { getReactStitchedError } from '../errors/stitched-error' import { getReactStitchedError } from '../errors/stitched-error'
const queueMicroTask = const queueMicroTask =
...@@ -37,7 +36,7 @@ export function handleConsoleError( ...@@ -37,7 +36,7 @@ export function handleConsoleError(
storeHydrationErrorStateFromConsoleArgs(...consoleErrorArgs) storeHydrationErrorStateFromConsoleArgs(...consoleErrorArgs)
attachHydrationErrorState(error) attachHydrationErrorState(error)
enqueueConsecutiveDedupedError(errorQueue, error) errorQueue.push(error)
for (const handler of errorHandlers) { for (const handler of errorHandlers) {
// Delayed the error being passed to React Dev Overlay, // Delayed the error being passed to React Dev Overlay,
// avoid the state being synchronously updated in the component. // avoid the state being synchronously updated in the component.
...@@ -60,7 +59,7 @@ export function handleClientError(originError: unknown) { ...@@ -60,7 +59,7 @@ export function handleClientError(originError: unknown) {
attachHydrationErrorState(error) attachHydrationErrorState(error)
enqueueConsecutiveDedupedError(errorQueue, error) errorQueue.push(error)
for (const handler of errorHandlers) { for (const handler of errorHandlers) {
// Delayed the error being passed to React Dev Overlay, // Delayed the error being passed to React Dev Overlay,
// avoid the state being synchronously updated in the component. // avoid the state being synchronously updated in the component.
......
...@@ -5,10 +5,6 @@ import type { ...@@ -5,10 +5,6 @@ import type {
} from '../../../shared' } from '../../../shared'
import { useMemo, useState, useEffect } from 'react' import { useMemo, useState, useEffect } from 'react'
import {
ACTION_UNHANDLED_ERROR,
ACTION_UNHANDLED_REJECTION,
} from '../../../shared'
import { import {
getErrorByType, getErrorByType,
type ReadyRuntimeError, type ReadyRuntimeError,
...@@ -19,17 +15,6 @@ export type SupportedErrorEvent = { ...@@ -19,17 +15,6 @@ export type SupportedErrorEvent = {
event: UnhandledErrorAction | UnhandledRejectionAction event: UnhandledErrorAction | UnhandledRejectionAction
} }
function getErrorSignature(ev: SupportedErrorEvent): string {
const { event } = ev
// eslint-disable-next-line default-case -- TypeScript checks this
switch (event.type) {
case ACTION_UNHANDLED_ERROR:
case ACTION_UNHANDLED_REJECTION: {
return `${event.reason.name}::${event.reason.message}::${event.reason.stack}`
}
}
}
type Props = { type Props = {
children: (params: { children: (params: {
runtimeErrors: ReadyRuntimeError[] runtimeErrors: ReadyRuntimeError[]
...@@ -72,14 +57,6 @@ const RenderRuntimeError = ({ children, state, isAppDir }: Props) => { ...@@ -72,14 +57,6 @@ const RenderRuntimeError = ({ children, state, isAppDir }: Props) => {
continue continue
} }
// Check for duplicate errors
if (idx > 0) {
const prev = errors[idx - 1]
if (getErrorSignature(prev) === getErrorSignature(e)) {
continue
}
}
next = e next = e
break break
} }
...@@ -108,12 +85,7 @@ const RenderRuntimeError = ({ children, state, isAppDir }: Props) => { ...@@ -108,12 +85,7 @@ const RenderRuntimeError = ({ children, state, isAppDir }: Props) => {
} }
}, [nextError, isAppDir]) }, [nextError, isAppDir])
const totalErrorCount = errors.filter((err, idx) => { const totalErrorCount = errors.length
const prev = errors[idx - 1]
// Check for duplicates
if (idx > 0) return getErrorSignature(prev) !== getErrorSignature(err)
return true
}).length
return children({ runtimeErrors, totalErrorCount }) return children({ runtimeErrors, totalErrorCount })
} }
......
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