Skip to content

Commit 0527f36

Browse files
committed
Pages Router: Fix error overlay
Same changes we did for App Router in fd04461
1 parent 3098f87 commit 0527f36

File tree

1 file changed

+16
-2
lines changed
  • packages/next/src/client/components/react-dev-overlay/pages

1 file changed

+16
-2
lines changed

packages/next/src/client/components/react-dev-overlay/pages/client.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ patchConsoleError()
2222
let isRegistered = false
2323
let stackTraceLimit: number | undefined = undefined
2424

25-
function onUnhandledError(ev: ErrorEvent) {
26-
const error = ev?.error
25+
function handleError(error: unknown) {
2726
if (!error || !(error instanceof Error) || typeof error.stack !== 'string') {
2827
// A non-error was thrown, we don't have anything to show. :-(
2928
return
@@ -63,6 +62,19 @@ function onUnhandledError(ev: ErrorEvent) {
6362
}
6463
}
6564

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+
6678
function onUnhandledRejection(ev: PromiseRejectionEvent) {
6779
const reason = ev?.reason
6880
if (
@@ -96,6 +108,7 @@ export function register() {
96108

97109
window.addEventListener('error', onUnhandledError)
98110
window.addEventListener('unhandledrejection', onUnhandledRejection)
111+
window.console.error = nextJsHandleConsoleError
99112
}
100113

101114
export function unregister() {
@@ -113,6 +126,7 @@ export function unregister() {
113126

114127
window.removeEventListener('error', onUnhandledError)
115128
window.removeEventListener('unhandledrejection', onUnhandledRejection)
129+
window.console.error = origConsoleError
116130
}
117131

118132
export function onBuildOk() {

0 commit comments

Comments
 (0)