Unverified Commit bed56afe authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[errors] refactor default global-error into a separate file (#78182)

### What

Move built-in `global-error` into a new module, separated from `error-boundary`
parent dd5f67f5
...@@ -88,7 +88,7 @@ pub async fn get_app_page_entry( ...@@ -88,7 +88,7 @@ pub async fn get_app_page_entry(
"VAR_MODULE_GLOBAL_ERROR" => if inner_assets.contains_key(GLOBAL_ERROR) { "VAR_MODULE_GLOBAL_ERROR" => if inner_assets.contains_key(GLOBAL_ERROR) {
GLOBAL_ERROR.into() GLOBAL_ERROR.into()
} else { } else {
"next/dist/client/components/error-boundary".into() "next/dist/client/components/global-error".into()
}, },
}, },
fxindexmap! { fxindexmap! {
......
...@@ -77,7 +77,7 @@ const GLOBAL_ERROR_FILE_TYPE = 'global-error' ...@@ -77,7 +77,7 @@ const GLOBAL_ERROR_FILE_TYPE = 'global-error'
const PAGE_SEGMENT = 'page$' const PAGE_SEGMENT = 'page$'
const PARALLEL_CHILDREN_SEGMENT = 'children$' const PARALLEL_CHILDREN_SEGMENT = 'children$'
const defaultGlobalErrorPath = 'next/dist/client/components/error-boundary' const defaultGlobalErrorPath = 'next/dist/client/components/global-error'
const defaultLayoutPath = 'next/dist/client/components/default-layout' const defaultLayoutPath = 'next/dist/client/components/default-layout'
type DirResolver = (pathToResolve: string) => string type DirResolver = (pathToResolve: string) => string
......
...@@ -23,11 +23,10 @@ import { ...@@ -23,11 +23,10 @@ import {
PathParamsContext, PathParamsContext,
} from '../../shared/lib/hooks-client-context.shared-runtime' } from '../../shared/lib/hooks-client-context.shared-runtime'
import { dispatchAppRouterAction, useActionQueue } from './use-action-queue' import { dispatchAppRouterAction, useActionQueue } from './use-action-queue'
import { import { ErrorBoundary } from './error-boundary'
default as DefaultGlobalError, import DefaultGlobalError, {
ErrorBoundary,
type GlobalErrorComponent, type GlobalErrorComponent,
} from './error-boundary' } from '../../client/components/global-error'
import { isBot } from '../../shared/lib/router/utils/is-bot' import { isBot } from '../../shared/lib/router/utils/is-bot'
import { addBasePath } from '../add-base-path' import { addBasePath } from '../add-base-path'
import { AppRouterAnnouncer } from './app-router-announcer' import { AppRouterAnnouncer } from './app-router-announcer'
......
...@@ -4,33 +4,7 @@ import React, { type JSX } from 'react' ...@@ -4,33 +4,7 @@ import React, { type JSX } from 'react'
import { useUntrackedPathname } from './navigation-untracked' import { useUntrackedPathname } from './navigation-untracked'
import { isNextRouterError } from './is-next-router-error' import { isNextRouterError } from './is-next-router-error'
import { handleHardNavError } from './nav-failure-handler' import { handleHardNavError } from './nav-failure-handler'
import { HandleISRError } from './handle-isr-error'
const workAsyncStorage =
typeof window === 'undefined'
? (
require('../../server/app-render/work-async-storage.external') as typeof import('../../server/app-render/work-async-storage.external')
).workAsyncStorage
: undefined
const styles = {
error: {
// https://github.com/sindresorhus/modern-normalize/blob/main/modern-normalize.css#L38-L52
fontFamily:
'system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"',
height: '100vh',
textAlign: 'center',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
},
text: {
fontSize: '14px',
fontWeight: 400,
lineHeight: '28px',
margin: '0 8px',
},
} as const
export type ErrorComponent = React.ComponentType<{ export type ErrorComponent = React.ComponentType<{
error: Error error: Error
...@@ -56,21 +30,6 @@ interface ErrorBoundaryHandlerState { ...@@ -56,21 +30,6 @@ interface ErrorBoundaryHandlerState {
previousPathname: string | null previousPathname: string | null
} }
// if we are revalidating we want to re-throw the error so the
// function crashes so we can maintain our previous cache
// instead of caching the error page
function HandleISRError({ error }: { error: any }) {
if (workAsyncStorage) {
const store = workAsyncStorage.getStore()
if (store?.isRevalidate || store?.isStaticGeneration) {
console.error(error)
throw error
}
}
return null
}
export class ErrorBoundaryHandler extends React.Component< export class ErrorBoundaryHandler extends React.Component<
ErrorBoundaryHandlerProps, ErrorBoundaryHandlerProps,
ErrorBoundaryHandlerState ErrorBoundaryHandlerState
...@@ -152,36 +111,6 @@ export class ErrorBoundaryHandler extends React.Component< ...@@ -152,36 +111,6 @@ export class ErrorBoundaryHandler extends React.Component<
} }
} }
export type GlobalErrorComponent = React.ComponentType<{
error: any
}>
export function GlobalError({ error }: { error: any }) {
const digest: string | undefined = error?.digest
return (
<html id="__next_error__">
<head></head>
<body>
<HandleISRError error={error} />
<div style={styles.error}>
<div>
<h2 style={styles.text}>
Application error: a {digest ? 'server' : 'client'}-side exception
has occurred while loading {window.location.hostname} (see the{' '}
{digest ? 'server logs' : 'browser console'} for more
information).
</h2>
{digest ? <p style={styles.text}>{`Digest: ${digest}`}</p> : null}
</div>
</div>
</body>
</html>
)
}
// Exported so that the import signature in the loaders can be identical to user
// supplied custom global error signatures.
export default GlobalError
/** /**
* Handles errors through `getDerivedStateFromError`. * Handles errors through `getDerivedStateFromError`.
* Renders the provided error component and provides a way to `reset` the error boundary state. * Renders the provided error component and provides a way to `reset` the error boundary state.
......
'use client'
import { HandleISRError } from './handle-isr-error'
const styles = {
error: {
// https://github.com/sindresorhus/modern-normalize/blob/main/modern-normalize.css#L38-L52
fontFamily:
'system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"',
height: '100vh',
textAlign: 'center',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
},
text: {
fontSize: '14px',
fontWeight: 400,
lineHeight: '28px',
margin: '0 8px',
},
} as const
export type GlobalErrorComponent = React.ComponentType<{
error: any
}>
function GlobalError({ error }: { error: any }) {
const digest: string | undefined = error?.digest
return (
<html id="__next_error__">
<head></head>
<body>
<HandleISRError error={error} />
<div style={styles.error}>
<div>
<h2 style={styles.text}>
Application error: a {digest ? 'server' : 'client'}-side exception
has occurred while loading {window.location.hostname} (see the{' '}
{digest ? 'server logs' : 'browser console'} for more
information).
</h2>
{digest ? <p style={styles.text}>{`Digest: ${digest}`}</p> : null}
</div>
</div>
</body>
</html>
)
}
// Exported so that the import signature in the loaders can be identical to user
// supplied custom global error signatures.
export default GlobalError
const workAsyncStorage =
typeof window === 'undefined'
? (
require('../../server/app-render/work-async-storage.external') as typeof import('../../server/app-render/work-async-storage.external')
).workAsyncStorage
: undefined
// if we are revalidating we want to re-throw the error so the
// function crashes so we can maintain our previous cache
// instead of caching the error page
export function HandleISRError({ error }: { error: any }) {
if (workAsyncStorage) {
const store = workAsyncStorage.getStore()
if (store?.isRevalidate || store?.isStaticGeneration) {
console.error(error)
throw error
}
}
return null
}
import { PureComponent } from 'react' import { PureComponent } from 'react'
import { RuntimeErrorHandler } from '../../errors/runtime-error-handler' import { RuntimeErrorHandler } from '../../errors/runtime-error-handler'
import { import { ErrorBoundary } from '../../error-boundary'
ErrorBoundary, import DefaultGlobalError, {
type GlobalErrorComponent, type GlobalErrorComponent,
GlobalError as DefaultGlobalError, } from '../../global-error'
} from '../../error-boundary'
type AppDevOverlayErrorBoundaryProps = { type AppDevOverlayErrorBoundaryProps = {
children: React.ReactNode children: React.ReactNode
......
import type { OverlayState } from '../shared' import type { OverlayState } from '../shared'
import type { GlobalErrorComponent } from '../../error-boundary' import type { GlobalErrorComponent } from '../../global-error'
import { useCallback, useEffect, useState } from 'react' import { useCallback, useEffect, useState } from 'react'
import { AppDevOverlayErrorBoundary } from './app-dev-overlay-error-boundary' import { AppDevOverlayErrorBoundary } from './app-dev-overlay-error-boundary'
......
import React from 'react' import React from 'react'
import { getSocketUrl } from '../utils/get-socket-url' import { getSocketUrl } from '../utils/get-socket-url'
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../../../../server/dev/hot-reloader-types' import { HMR_ACTIONS_SENT_TO_BROWSER } from '../../../../server/dev/hot-reloader-types'
import GlobalError from '../../error-boundary' import GlobalError from '../../global-error'
import { AppDevOverlayErrorBoundary } from './app-dev-overlay-error-boundary' import { AppDevOverlayErrorBoundary } from './app-dev-overlay-error-boundary'
const noop = () => {} const noop = () => {}
......
...@@ -42,7 +42,7 @@ import type { DebugInfo } from '../types' ...@@ -42,7 +42,7 @@ import type { DebugInfo } from '../types'
import { useUntrackedPathname } from '../../navigation-untracked' import { useUntrackedPathname } from '../../navigation-untracked'
import { getComponentStack, getOwnerStack } from '../../errors/stitched-error' import { getComponentStack, getOwnerStack } from '../../errors/stitched-error'
import { handleDevBuildIndicatorHmrEvents } from '../../../dev/dev-build-indicator/internal/handle-dev-build-indicator-hmr-events' import { handleDevBuildIndicatorHmrEvents } from '../../../dev/dev-build-indicator/internal/handle-dev-build-indicator-hmr-events'
import type { GlobalErrorComponent } from '../../error-boundary' import type { GlobalErrorComponent } from '../../global-error'
import type { DevIndicatorServerState } from '../../../../server/dev/dev-indicator-server-state' import type { DevIndicatorServerState } from '../../../../server/dev/dev-indicator-server-state'
import reportHmrLatency from '../utils/report-hmr-latency' import reportHmrLatency from '../utils/report-hmr-latency'
import { TurbopackHmr } from '../utils/turbopack-hot-reloader-common' import { TurbopackHmr } from '../utils/turbopack-hot-reloader-common'
......
...@@ -11,10 +11,8 @@ import { isNextRouterError } from '../components/is-next-router-error' ...@@ -11,10 +11,8 @@ import { isNextRouterError } from '../components/is-next-router-error'
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr' import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { reportGlobalError } from './report-global-error' import { reportGlobalError } from './report-global-error'
import { originConsoleError } from '../components/globals/intercept-console-error' import { originConsoleError } from '../components/globals/intercept-console-error'
import { import { ErrorBoundaryHandler } from '../components/error-boundary'
ErrorBoundaryHandler, import DefaultErrorBoundary from '../components/global-error'
GlobalError as DefaultErrorBoundary,
} from '../components/error-boundary'
export function onCaughtError( export function onCaughtError(
thrownValue: unknown, thrownValue: unknown,
......
...@@ -29,8 +29,11 @@ import * as serverHooks from '../../client/components/hooks-server-context' ...@@ -29,8 +29,11 @@ import * as serverHooks from '../../client/components/hooks-server-context'
import { HTTPAccessFallbackBoundary } from '../../client/components/http-access-fallback/error-boundary' import { HTTPAccessFallbackBoundary } from '../../client/components/http-access-fallback/error-boundary'
import { createMetadataComponents } from '../../lib/metadata/metadata' import { createMetadataComponents } from '../../lib/metadata/metadata'
import { patchFetch as _patchFetch } from '../lib/patch-fetch' import { patchFetch as _patchFetch } from '../lib/patch-fetch'
// not being used but needs to be included in the client manifest for /_not-found // Not being directly used but should be included in the client manifest for /_not-found
// * ErrorBoundary -> client/components/error-boundary
// * GlobalError -> client/components/global-error
import '../../client/components/error-boundary' import '../../client/components/error-boundary'
import '../../client/components/global-error'
import { import {
MetadataBoundary, MetadataBoundary,
ViewportBoundary, ViewportBoundary,
......
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