@@ -22,8 +22,7 @@ patchConsoleError()
22
22
let isRegistered = false
23
23
let stackTraceLimit : number | undefined = undefined
24
24
25
- function onUnhandledError ( ev : ErrorEvent ) {
26
- const error = ev ?. error
25
+ function handleError ( error : unknown ) {
27
26
if ( ! error || ! ( error instanceof Error ) || typeof error . stack !== 'string' ) {
28
27
// A non-error was thrown, we don't have anything to show. :-(
29
28
return
@@ -63,6 +62,19 @@ function onUnhandledError(ev: ErrorEvent) {
63
62
}
64
63
}
65
64
65
+ let origConsoleError = console . error
66
+ function nextJsHandleConsoleError ( ...args : any [ ] ) {
67
+ // See https://github.com/facebook/react/blob/d50323eb845c5fde0d720cae888bf35dedd05506/packages/react-reconciler/src/ReactFiberErrorLogger.js#L78
68
+ const error = process . env . NODE_ENV !== 'production' ? args [ 1 ] : args [ 0 ]
69
+ handleError ( error )
70
+ origConsoleError . apply ( window . console , args )
71
+ }
72
+
73
+ function onUnhandledError ( event : ErrorEvent ) {
74
+ const error = event ?. error
75
+ handleError ( error )
76
+ }
77
+
66
78
function onUnhandledRejection ( ev : PromiseRejectionEvent ) {
67
79
const reason = ev ?. reason
68
80
if (
@@ -96,6 +108,7 @@ export function register() {
96
108
97
109
window . addEventListener ( 'error' , onUnhandledError )
98
110
window . addEventListener ( 'unhandledrejection' , onUnhandledRejection )
111
+ window . console . error = nextJsHandleConsoleError
99
112
}
100
113
101
114
export function unregister ( ) {
@@ -113,6 +126,7 @@ export function unregister() {
113
126
114
127
window . removeEventListener ( 'error' , onUnhandledError )
115
128
window . removeEventListener ( 'unhandledrejection' , onUnhandledRejection )
129
+ window . console . error = origConsoleError
116
130
}
117
131
118
132
export function onBuildOk ( ) {
0 commit comments