Skip to content

Commit

Permalink
fix(core): always treat error boundary fallback as a callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 25, 2022
1 parent cd21a31 commit 2ad6f46
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
10 changes: 5 additions & 5 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ declare module '@theme/Error' {
import type {ComponentProps} from 'react';
import type ErrorBoundary from '@docusaurus/ErrorBoundary';

type ErrorProps = ComponentProps<
type ErrorProps = Parameters<
NonNullable<ComponentProps<typeof ErrorBoundary>['fallback']>
>;
>[0];

export interface Props extends ErrorProps {}
export default function Error(props: Props): JSX.Element;
Expand Down Expand Up @@ -125,13 +125,13 @@ declare module '@docusaurus/constants' {
}

declare module '@docusaurus/ErrorBoundary' {
import type {ReactNode, ComponentType} from 'react';
import type {ReactNode} from 'react';

export interface Props {
readonly fallback?: ComponentType<{
readonly fallback?: (props: {
readonly error: Error;
readonly tryAgain: () => void;
}>;
}) => JSX.Element;
readonly children: ReactNode;
}
export default function ErrorBoundary(props: Props): JSX.Element;
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus-theme-classic/src/theme/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export default function Layout(props: Props): JSX.Element {
<Navbar />

<div className={clsx(ThemeClassNames.wrapper.main, wrapperClassName)}>
<ErrorBoundary fallback={ErrorPageContent}>{children}</ErrorBoundary>
<ErrorBoundary fallback={(p) => <ErrorPageContent {...p} />}>
{children}
</ErrorBoundary>
</div>

{!noFooter && <Footer />}
Expand Down
4 changes: 1 addition & 3 deletions packages/docusaurus/src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ import SiteMetadataDefaults from './SiteMetadataDefaults';
// TODO, quick fix for CSS insertion order
// eslint-disable-next-line import/order
import ErrorBoundary from '@docusaurus/ErrorBoundary';
// eslint-disable-next-line import/order
import Error from '@theme/Error';

export default function App(): JSX.Element {
const routeElement = renderRoutes(routes);
const location = useLocation();
return (
<ErrorBoundary fallback={Error}>
<ErrorBoundary>
<DocusaurusContextProvider>
<BrowserContextProvider>
<Root>
Expand Down
10 changes: 6 additions & 4 deletions packages/docusaurus/src/client/exports/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export default class ErrorBoundary extends React.Component<Props, State> {
const {error} = this.state;

if (error) {
const Fallback = this.props.fallback ?? DefaultFallback;
return (
<Fallback error={error} tryAgain={() => this.setState({error: null})} />
);
const fallback =
this.props.fallback ?? ((props) => <DefaultFallback {...props} />);
return fallback({
error,
tryAgain: () => this.setState({error: null}),
});
}

// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
Expand Down
2 changes: 1 addition & 1 deletion website/docs/docusaurus-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ This component doesn't catch build-time errors and only protects against client-

#### Props {#errorboundary-props}

- `fallback`: a React component. The error boundary will render the component with two props: `error`, the error that was caught, and `tryAgain`, a function (`() => void`) callback to reset the error in the component and try rendering it again.
- `fallback`: an optional callback returning a JSX element. It will receive two props: `error`, the error that was caught, and `tryAgain`, a function (`() => void`) callback to reset the error in the component and try rendering it again. If not present, `@theme/Error` will be rendered instead. `@theme/Error` is used for the error boundaries wrapping the site, above the layout.

### `<Head/>` {#head}

Expand Down

0 comments on commit 2ad6f46

Please sign in to comment.