Skip to content

Commit

Permalink
revert unrelated
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 10, 2024
1 parent 7bb7256 commit cec30e3
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { isNextRouterError } from '../is-next-router-error'
import { handleClientError } from '../react-dev-overlay/internal/helpers/use-error-handler'

const originConsoleError = window.console.error

const isReactOwnerStackEnabled = !!process.env.__NEXT_REACT_OWNER_STACK

// Patch console.error to collect information about hydration errors
function patchConsoleError() {
export function patchConsoleError() {
// Ensure it's only patched once
if (typeof window === 'undefined') {
return
Expand All @@ -15,13 +11,13 @@ function patchConsoleError() {
const originConsoleError = window.console.error
window.console.error = (...args) => {
// See https://github.com/facebook/react/blob/d50323eb845c5fde0d720cae888bf35dedd05506/packages/react-reconciler/src/ReactFiberErrorLogger.js#L78
const error =
process.env.NODE_ENV !== 'production'
? isReactOwnerStackEnabled
? args[1] || args[0]
: args[1]
: args[0]

// const error =
// process.env.NODE_ENV !== 'production'
// ? process.env.__NEXT_REACT_OWNER_STACK
// ? args[1] // || args[0]
// : args[1]
// : args[0]
const error = process.env.NODE_ENV !== 'production' ? args[1] : args[0]

if (!isNextRouterError(error)) {
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -36,5 +32,3 @@ function patchConsoleError() {
}
}
}

export { patchConsoleError, originConsoleError }
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ export default class ReactDevOverlay extends React.PureComponent<
state = { reactError: null }

static getDerivedStateFromError(err: Error): ReactDevOverlayState {
const stitchedError = getReactStitchedError(err)
if (!stitchedError.stack) return { reactError: null }
if (!err.stack) return { reactError: null }

const error = getReactStitchedError(err)
return {
reactError: {
id: 0,
event: {
type: ACTION_UNHANDLED_ERROR,
reason: stitchedError,
frames: parseStack(stitchedError.stack || ''),
reason: error,
frames: parseStack(error.stack || ''),
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
ACTION_UNHANDLED_ERROR,
ACTION_UNHANDLED_REJECTION,
ACTION_VERSION_INFO,
REACT_REFRESH_FULL_RELOAD_FROM_ERROR,
useErrorOverlayReducer,
} from '../shared'
import { parseStack } from '../internal/helpers/parseStack'
Expand All @@ -34,6 +33,7 @@ import type {
TurbopackMsgToBrowser,
} from '../../../../server/dev/hot-reloader-types'
import { extractModulesFromTurbopackMessage } from '../../../../server/dev/extract-modules-from-turbopack-message'
import { REACT_REFRESH_FULL_RELOAD_FROM_ERROR } from '../shared'
import type { HydrationErrorState } from '../internal/helpers/hydration-error-info'
import type { DebugInfo } from '../types'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import {
import { LeftRightDialogHeader } from '../components/LeftRightDialogHeader'
import { Overlay } from '../components/Overlay'
import { Toast } from '../components/Toast'
import {
getErrorByType,
type ReadyRuntimeError,
} from '../helpers/getErrorByType'
import { getErrorByType } from '../helpers/getErrorByType'
import type { ReadyRuntimeError } from '../helpers/getErrorByType'
import { noop as css } from '../helpers/noop-template'
import { CloseIcon } from '../icons/CloseIcon'
import { RuntimeError } from './RuntimeError'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX = new RegExp(
)

export function getReactStitchedError<T = unknown>(err: T): Error {
if (!process.env.__NEXT_REACT_OWNER_STACK) return err as any

const isErrorInstance = err instanceof Error
const originStack = isErrorInstance ? err.stack || '' : ''
const originMessage = isErrorInstance ? err.message : ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function pushErrorFilterDuplicates(
]
}

const INITIAL_OVERLAY_STATE: OverlayState = {
export const INITIAL_OVERLAY_STATE: OverlayState = {
nextId: 1,
buildError: null,
errors: [],
Expand Down Expand Up @@ -189,7 +189,5 @@ export function useErrorOverlayReducer() {
}, INITIAL_OVERLAY_STATE)
}

export { INITIAL_OVERLAY_STATE }

export const REACT_REFRESH_FULL_RELOAD_FROM_ERROR =
'[Fast Refresh] performing full reload because your application had an unrecoverable error'
9 changes: 4 additions & 5 deletions packages/next/src/client/react-client-callbacks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { HydrationOptions } from 'react-dom/client'
import { isBailoutToCSRError } from '../shared/lib/lazy-dynamic/bailout-to-csr'
import { getReactStitchedError } from './components/react-dev-overlay/internal/helpers/stitched-error'
import { originConsoleError } from './components/globals/intercept-console-error'
import { handleClientError } from './components/react-dev-overlay/internal/helpers/use-error-handler'
import isError from '../lib/is-error'
import { isNextRouterError } from './components/is-next-router-error'
Expand All @@ -12,21 +11,21 @@ const reportGlobalError =
// emulating an uncaught JavaScript error.
reportError
: (error: any) => {
console.log('No report - Unhandled error:', error)
window.console.error(error)
}

export const onRecoverableError: HydrationOptions['onRecoverableError'] = (
err,
errorInfo
) => {
if (isBailoutToCSRError(err)) return

const stitchedError = getReactStitchedError(err)
// In development mode, pass along the component stack to the error
if (process.env.NODE_ENV === 'development' && errorInfo.componentStack) {
;(stitchedError as any)._componentStack = errorInfo.componentStack
}
// Skip certain custom errors which are not expected to be reported on client
if (isBailoutToCSRError(err)) return

reportGlobalError(stitchedError)
}
Expand Down Expand Up @@ -65,7 +64,7 @@ export const onCaughtError: HydrationOptions['onCaughtError'] = (

const originErrorStack = isError(err) ? err.stack || '' : ''
// Always log the modified error instance so the console.error interception side can pick it up easily without constructing an error again.
originConsoleError(originErrorStack + '\n\n' + errorLocation)
console.error(originErrorStack + '\n\n' + errorLocation)
handleClientError(stitchedError)
} else {
console.error(err)
Expand Down Expand Up @@ -97,7 +96,7 @@ export const onUncaughtError: HydrationOptions['onUncaughtError'] = (
// Create error location with errored component and error boundary, to match the behavior of default React onCaughtError handler.
const errorLocation = `The above error occurred in the <${componentThatErroredName}> component.`

originConsoleError(stitchedError.stack + '\n\n' + errorLocation)
console.error(stitchedError.stack + '\n\n' + errorLocation)
// Always log the modified error instance so the console.error interception side can pick it up easily without constructing an error again.
reportGlobalError(stitchedError)
} else {
Expand Down

0 comments on commit cec30e3

Please sign in to comment.