@@ -6,7 +6,6 @@ import { handleClientError } from '../components/react-dev-overlay/internal/help
66import { isNextRouterError } from '../components/is-next-router-error'
77import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
88import { reportGlobalError } from './report-global-error'
9- import isError from '../../lib/is-error'
109import { originConsoleError } from '../components/globals/intercept-console-error'
1110
1211export const onCaughtError : HydrationOptions [ 'onCaughtError' ] = (
@@ -16,9 +15,7 @@ export const onCaughtError: HydrationOptions['onCaughtError'] = (
1615 // Skip certain custom errors which are not expected to be reported on client
1716 if ( isBailoutToCSRError ( err ) || isNextRouterError ( err ) ) return
1817
19- const stitchedError = getReactStitchedError ( err )
20-
21- if ( process . env . NODE_ENV === 'development' ) {
18+ if ( process . env . NODE_ENV !== 'production' ) {
2219 const errorBoundaryComponent = errorInfo ?. errorBoundary ?. constructor
2320 const errorBoundaryName =
2421 // read react component displayName
@@ -36,11 +33,6 @@ export const onCaughtError: HydrationOptions['onCaughtError'] = (
3633 componentThatErroredFrame ?. match ( / \s + a t ( \w + ) \s + | ( \w + ) @ / ) ?? [ ]
3734 const componentThatErroredName = matches [ 1 ] || matches [ 2 ] || 'Unknown'
3835
39- // In development mode, pass along the component stack to the error
40- if ( process . env . NODE_ENV === 'development' && errorInfo . componentStack ) {
41- ; ( stitchedError as any ) . _componentStack = errorInfo . componentStack
42- }
43-
4436 // Create error location with errored component and error boundary, to match the behavior of default React onCaughtError handler.
4537 const errorBoundaryMessage = `It was handled by the <${ errorBoundaryName } > error boundary.`
4638 const componentErrorMessage = componentThatErroredName
@@ -49,10 +41,16 @@ export const onCaughtError: HydrationOptions['onCaughtError'] = (
4941
5042 const errorLocation = `${ componentErrorMessage } ${ errorBoundaryMessage } `
5143
52- const originErrorStack = isError ( err ) ? err . stack || '' : ''
44+ const stitchedError = getReactStitchedError ( err )
45+ // TODO: change to passing down errorInfo later
46+ // In development mode, pass along the component stack to the error
47+ if ( errorInfo . componentStack ) {
48+ ; ( stitchedError as any ) . _componentStack = errorInfo . componentStack
49+ }
50+
51+ // Log and report the error with location but without modifying the error stack
52+ originConsoleError ( '%o\n\n%s' , err , errorLocation )
5353
54- // Log the modified error message with stack so without being intercepted again.
55- originConsoleError ( originErrorStack + '\n\n' + errorLocation )
5654 handleClientError ( stitchedError , [ ] )
5755 } else {
5856 originConsoleError ( err )
@@ -66,29 +64,28 @@ export const onUncaughtError: HydrationOptions['onUncaughtError'] = (
6664 // Skip certain custom errors which are not expected to be reported on client
6765 if ( isBailoutToCSRError ( err ) || isNextRouterError ( err ) ) return
6866
69- const stitchedError = getReactStitchedError ( err )
70-
71- if ( process . env . NODE_ENV === 'development' ) {
67+ if ( process . env . NODE_ENV !== 'production' ) {
7268 const componentThatErroredFrame = errorInfo ?. componentStack ?. split ( '\n' ) [ 1 ]
7369
7470 // Match chrome or safari stack trace
7571 const matches =
7672 componentThatErroredFrame ?. match ( / \s + a t ( \w + ) \s + | ( \w + ) @ / ) ?? [ ]
7773 const componentThatErroredName = matches [ 1 ] || matches [ 2 ] || 'Unknown'
7874
79- // In development mode, pass along the component stack to the error
80- if ( process . env . NODE_ENV === 'development' && errorInfo . componentStack ) {
81- ; ( stitchedError as any ) . _componentStack = errorInfo . componentStack
82- }
83-
8475 // Create error location with errored component and error boundary, to match the behavior of default React onCaughtError handler.
8576 const errorLocation = componentThatErroredName
8677 ? `The above error occurred in the <${ componentThatErroredName } > component.`
8778 : `The above error occurred in one of your components.`
8879
89- const errStack = ( stitchedError as any ) . stack || ''
90- // Log the modified error message with stack so without being intercepted again.
91- originConsoleError ( errStack + '\n\n' + errorLocation )
80+ const stitchedError = getReactStitchedError ( err )
81+ // TODO: change to passing down errorInfo later
82+ // In development mode, pass along the component stack to the error
83+ if ( errorInfo . componentStack ) {
84+ ; ( stitchedError as any ) . _componentStack = errorInfo . componentStack
85+ }
86+
87+ // Log and report the error with location but without modifying the error stack
88+ originConsoleError ( '%o\n\n%s' , err , errorLocation )
9289 reportGlobalError ( stitchedError )
9390 } else {
9491 reportGlobalError ( err )
0 commit comments