import type { QueryParamError, Unreachable, WorkspaceNotFoundError, } from '@affine/env/constant'; import { PageNotFoundError } from '@affine/env/constant'; import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom'; import { currentPageIdAtom, currentWorkspaceIdAtom, rootStore, } from '@toeverything/plugin-infra/manager'; import { useAtomValue } from 'jotai/react'; import { Provider } from 'jotai/react'; import type { NextRouter } from 'next/router'; import type { ErrorInfo, ReactElement, ReactNode } from 'react'; import type React from 'react'; import { Component } from 'react'; export type AffineErrorBoundaryProps = React.PropsWithChildren<{ router: NextRouter; }>; type AffineError = | QueryParamError | Unreachable | WorkspaceNotFoundError | PageNotFoundError | Error; interface AffineErrorBoundaryState { error: AffineError | null; } export const DumpInfo = (props: Pick) => { const router = props.router; const metadata = useAtomValue(rootWorkspacesMetadataAtom); const currentWorkspaceId = useAtomValue(currentWorkspaceIdAtom); const currentPageId = useAtomValue(currentPageIdAtom); const path = router.asPath; const query = router.query; return ( <>
Please copy the following information and send it to the developer.
path: {path}
query: {JSON.stringify(query)}
currentWorkspaceId: {currentWorkspaceId}
currentPageId: {currentPageId}
metadata: {JSON.stringify(metadata)}
); }; export class AffineErrorBoundary extends Component< AffineErrorBoundaryProps, AffineErrorBoundaryState > { public override state: AffineErrorBoundaryState = { error: null, }; public static getDerivedStateFromError( error: AffineError ): AffineErrorBoundaryState { return { error }; } public override componentDidCatch(error: AffineError, errorInfo: ErrorInfo) { console.error('Uncaught error:', error, errorInfo); } public override render(): ReactNode { if (this.state.error) { let errorDetail: ReactElement | null = null; const error = this.state.error; if (error instanceof PageNotFoundError) { errorDetail = ( <>

Sorry.. there was an error

<> Page error Cannot find page {error.pageId} in workspace{' '} {error.workspace.id} ); } else { errorDetail = ( <>

Sorry.. there was an error

{error.message ?? error.toString()} ); } return ( <> {errorDetail} ); } return this.props.children; } }