Unverified Commit 0bcbab52 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[dev-overlay] Move error handlers into dispatcher in /app (#79660)

The dispatcher will later be the bridge between user code and the dev overlay so it should house all logic that communicates with the Dev Overlay frontend.
parent c5130080
/// <reference types="webpack/module.d.ts" /> /// <reference types="webpack/module.d.ts" />
import type { ReactNode } from 'react' import type { ReactNode } from 'react'
import { useCallback, useEffect, startTransition, useMemo, useRef } from 'react' import { useEffect, startTransition, useMemo, useRef } from 'react'
import stripAnsi from 'next/dist/compiled/strip-ansi' import stripAnsi from 'next/dist/compiled/strip-ansi'
import formatWebpackMessages from '../utils/format-webpack-messages' import formatWebpackMessages from '../utils/format-webpack-messages'
import { useRouter } from '../../navigation' import { useRouter } from '../../navigation'
...@@ -57,6 +57,8 @@ export interface Dispatcher { ...@@ -57,6 +57,8 @@ export interface Dispatcher {
onRefresh(): void onRefresh(): void
onStaticIndicator(status: boolean): void onStaticIndicator(status: boolean): void
onDevIndicator(devIndicator: DevIndicatorServerState): void onDevIndicator(devIndicator: DevIndicatorServerState): void
onUnhandledError(error: Error): void
onUnhandledRejection(error: Error): void
} }
let mostRecentCompilationHash: any = null let mostRecentCompilationHash: any = null
...@@ -511,40 +513,33 @@ export default function HotReload({ ...@@ -511,40 +513,33 @@ export default function HotReload({
devIndicator, devIndicator,
}) })
}, },
onUnhandledError(error) {
// Component stack is added to the error in use-error-handler in case there was a hydration error
const componentStack = getComponentStack(error)
const ownerStack = getOwnerStack(error)
dispatch({
type: ACTION_UNHANDLED_ERROR,
reason: error,
frames: parseStack((error.stack || '') + (ownerStack || '')),
componentStackFrames:
typeof componentStack === 'string'
? parseComponentStack(componentStack)
: undefined,
})
},
onUnhandledRejection(error) {
const ownerStack = getOwnerStack(error)
dispatch({
type: ACTION_UNHANDLED_REJECTION,
reason: error,
frames: parseStack((error.stack || '') + (ownerStack || '')),
})
},
} }
}, [dispatch]) }, [dispatch])
const handleOnUnhandledError = useCallback( useErrorHandler(dispatcher.onUnhandledError, dispatcher.onUnhandledRejection)
(error: Error): void => {
// Component stack is added to the error in use-error-handler in case there was a hydration error
const componentStack = getComponentStack(error)
const ownerStack = getOwnerStack(error)
dispatch({
type: ACTION_UNHANDLED_ERROR,
reason: error,
frames: parseStack((error.stack || '') + (ownerStack || '')),
componentStackFrames:
typeof componentStack === 'string'
? parseComponentStack(componentStack)
: undefined,
})
},
[dispatch]
)
const handleOnUnhandledRejection = useCallback(
(error: Error): void => {
const ownerStack = getOwnerStack(error)
dispatch({
type: ACTION_UNHANDLED_REJECTION,
reason: error,
frames: parseStack((error.stack || '') + (ownerStack || '')),
})
},
[dispatch]
)
useErrorHandler(handleOnUnhandledError, handleOnUnhandledRejection)
const webSocketRef = useWebsocket(assetPrefix) const webSocketRef = useWebsocket(assetPrefix)
useWebsocketPing(webSocketRef) useWebsocketPing(webSocketRef)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment