@@ -6,7 +6,6 @@ import { handleClientError } from '../components/react-dev-overlay/internal/help
6
6
import { isNextRouterError } from '../components/is-next-router-error'
7
7
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
8
8
import { reportGlobalError } from './report-global-error'
9
- import isError from '../../lib/is-error'
10
9
import { originConsoleError } from '../components/globals/intercept-console-error'
11
10
12
11
export const onCaughtError : HydrationOptions [ 'onCaughtError' ] = (
@@ -16,9 +15,7 @@ export const onCaughtError: HydrationOptions['onCaughtError'] = (
16
15
// Skip certain custom errors which are not expected to be reported on client
17
16
if ( isBailoutToCSRError ( err ) || isNextRouterError ( err ) ) return
18
17
19
- const stitchedError = getReactStitchedError ( err )
20
-
21
- if ( process . env . NODE_ENV === 'development' ) {
18
+ if ( process . env . NODE_ENV !== 'production' ) {
22
19
const errorBoundaryComponent = errorInfo ?. errorBoundary ?. constructor
23
20
const errorBoundaryName =
24
21
// read react component displayName
@@ -36,11 +33,6 @@ export const onCaughtError: HydrationOptions['onCaughtError'] = (
36
33
componentThatErroredFrame ?. match ( / \s + a t ( \w + ) \s + | ( \w + ) @ / ) ?? [ ]
37
34
const componentThatErroredName = matches [ 1 ] || matches [ 2 ] || 'Unknown'
38
35
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
-
44
36
// Create error location with errored component and error boundary, to match the behavior of default React onCaughtError handler.
45
37
const errorBoundaryMessage = `It was handled by the <${ errorBoundaryName } > error boundary.`
46
38
const componentErrorMessage = componentThatErroredName
@@ -49,10 +41,16 @@ export const onCaughtError: HydrationOptions['onCaughtError'] = (
49
41
50
42
const errorLocation = `${ componentErrorMessage } ${ errorBoundaryMessage } `
51
43
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 )
53
53
54
- // Log the modified error message with stack so without being intercepted again.
55
- originConsoleError ( originErrorStack + '\n\n' + errorLocation )
56
54
handleClientError ( stitchedError , [ ] )
57
55
} else {
58
56
originConsoleError ( err )
@@ -66,29 +64,28 @@ export const onUncaughtError: HydrationOptions['onUncaughtError'] = (
66
64
// Skip certain custom errors which are not expected to be reported on client
67
65
if ( isBailoutToCSRError ( err ) || isNextRouterError ( err ) ) return
68
66
69
- const stitchedError = getReactStitchedError ( err )
70
-
71
- if ( process . env . NODE_ENV === 'development' ) {
67
+ if ( process . env . NODE_ENV !== 'production' ) {
72
68
const componentThatErroredFrame = errorInfo ?. componentStack ?. split ( '\n' ) [ 1 ]
73
69
74
70
// Match chrome or safari stack trace
75
71
const matches =
76
72
componentThatErroredFrame ?. match ( / \s + a t ( \w + ) \s + | ( \w + ) @ / ) ?? [ ]
77
73
const componentThatErroredName = matches [ 1 ] || matches [ 2 ] || 'Unknown'
78
74
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
-
84
75
// Create error location with errored component and error boundary, to match the behavior of default React onCaughtError handler.
85
76
const errorLocation = componentThatErroredName
86
77
? `The above error occurred in the <${ componentThatErroredName } > component.`
87
78
: `The above error occurred in one of your components.`
88
79
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 )
92
89
reportGlobalError ( stitchedError )
93
90
} else {
94
91
reportGlobalError ( err )
0 commit comments