Skip to content

Commit

Permalink
revert unrelated changes
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 25, 2024
1 parent bcc5530 commit c20afd3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { isNextRouterError } from '../is-next-router-error'
import { captureStackTrace } from '../react-dev-overlay/internal/helpers/capture-stack-trace'
import { handleClientError } from '../react-dev-overlay/internal/helpers/use-error-handler'

const NEXT_CONSOLE_STACK_FRAME = 'next-console-stack-frame'

export const originConsoleError = window.console.error

// Patch console.error to collect information about hydration errors
Expand All @@ -13,48 +11,41 @@ export function patchConsoleError() {
if (typeof window === 'undefined') {
return
}
window.console.error = function error(...args: any[]) {
let maybeError: unknown
let isReplayedError = false

const namedLoggerInstance = {
[NEXT_CONSOLE_STACK_FRAME](...args: any[]) {
let maybeError: unknown
let isReplayedError = false

if (process.env.NODE_ENV !== 'production') {
const replayedError = matchReplayedError(...args)
if (replayedError) {
isReplayedError = true
maybeError = replayedError
} else {
// See https://github.com/facebook/react/blob/d50323eb845c5fde0d720cae888bf35dedd05506/packages/react-reconciler/src/ReactFiberErrorLogger.js#L78
maybeError = args[1]
}
if (process.env.NODE_ENV !== 'production') {
const replayedError = matchReplayedError(...args)
if (replayedError) {
isReplayedError = true
maybeError = replayedError
} else {
maybeError = args[0]
// See https://github.com/facebook/react/blob/d50323eb845c5fde0d720cae888bf35dedd05506/packages/react-reconciler/src/ReactFiberErrorLogger.js#L78
maybeError = args[1]
}
} else {
maybeError = args[0]
}

if (!isNextRouterError(maybeError)) {
if (process.env.NODE_ENV !== 'production') {
// Create an origin stack that pointing to the origin location of the error
if (!isReplayedError && isError(maybeError)) {
captureStackTrace(maybeError)
}

handleClientError(
// replayed errors have their own complex format string that should be used,
// but if we pass the error directly, `handleClientError` will ignore it
maybeError,
args
)
if (!isNextRouterError(maybeError)) {
if (process.env.NODE_ENV !== 'production') {
// Create an origin stack that pointing to the origin location of the error
if (!isReplayedError && isError(maybeError)) {
captureStackTrace(maybeError)
}

originConsoleError.apply(window.console, args)
handleClientError(
// replayed errors have their own complex format string that should be used,
// but if we pass the error directly, `handleClientError` will ignore it
maybeError,
args
)
}
},
}

window.console.error = namedLoggerInstance[NEXT_CONSOLE_STACK_FRAME].bind(
window.console
)
originConsoleError.apply(window.console, args)
}
}
}

function matchReplayedError(...args: unknown[]): Error | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import React from 'react'
import isError from '../../../../../lib/is-error'
import { stripReactStackTrace } from './strip-stack-frame'

const REACT_ERROR_STACK_BOTTOM_FRAME = 'react-stack-bottom-frame'
const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX = new RegExp(
`(at ${REACT_ERROR_STACK_BOTTOM_FRAME} )|(${REACT_ERROR_STACK_BOTTOM_FRAME}\\@)`
)

export function getReactStitchedError<T = unknown>(err: T): Error | T {
if (typeof (React as any).captureOwnerStack !== 'function') {
return err
}

const isErrorInstance = isError(err)
const originStack = isErrorInstance ? err.stack || '' : ''
const originMessage = isErrorInstance ? err.message : ''
let newStack = stripReactStackTrace(originStack)
const stackLines = originStack.split('\n')
const indexOfSplit = stackLines.findIndex((line) =>
REACT_ERROR_STACK_BOTTOM_FRAME_REGEX.test(line)
)
const isOriginalReactError = indexOfSplit >= 0 // has the react-stack-bottom-frame
let newStack = isOriginalReactError
? stackLines.slice(0, indexOfSplit).join('\n')
: originStack

const newError = new Error(originMessage)
// Copy all enumerable properties, e.g. digest
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit c20afd3

Please sign in to comment.