File size: 1,582 Bytes
b91e262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import isError from '../../../../lib/is-error'
import { isNextRouterError } from '../../../../client/components/is-next-router-error'
import { handleConsoleError } from './use-error-handler'
import { parseConsoleArgs } from '../../../../client/lib/console'
import { forwardErrorLog } from '../forward-logs'

export const originConsoleError = globalThis.console.error

// Patch console.error to collect information about hydration errors
export function patchConsoleError() {
  // Ensure it's only patched once
  if (typeof window === 'undefined') {
    return
  }
  window.console.error = function error(...args: any[]) {
    let maybeError: unknown
    if (process.env.NODE_ENV !== 'production') {
      const { error: replayedError } = parseConsoleArgs(args)
      if (replayedError) {
        maybeError = replayedError
      } else if (isError(args[0])) {
        maybeError = args[0]
      } else {
        // 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') {
        handleConsoleError(
          // 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
        )
      }
      forwardErrorLog(args)

      originConsoleError.apply(window.console, args)
    }
  }
}