77
88import React , { type ComponentProps } from 'react' ;
99import Translate from '@docusaurus/Translate' ;
10+ import { getErrorCausalChain } from '@docusaurus/utils-common' ;
1011import styles from './errorBoundaryUtils.module.css' ;
1112
1213export function ErrorBoundaryTryAgainButton (
@@ -22,7 +23,34 @@ export function ErrorBoundaryTryAgainButton(
2223 </ button >
2324 ) ;
2425}
25-
2626export function ErrorBoundaryError ( { error} : { error : Error } ) : JSX . Element {
27- return < p className = { styles . errorBoundaryError } > { error . message } </ p > ;
27+ const causalChain = getErrorCausalChain ( error ) ;
28+ const fullMessage = causalChain . map ( ( e ) => e . message ) . join ( '\n\nCause:\n' ) ;
29+ return < p className = { styles . errorBoundaryError } > { fullMessage } </ p > ;
30+ }
31+
32+ /**
33+ * This component is useful to wrap a low-level error into a more meaningful
34+ * error with extra context, using the ES error-cause feature.
35+ *
36+ * <ErrorCauseBoundary
37+ * onError={(error) => new Error("extra context message",{cause: error})}
38+ * >
39+ * <RiskyComponent>
40+ * </ErrorCauseBoundary>
41+ */
42+ export class ErrorCauseBoundary extends React . Component <
43+ {
44+ children : React . ReactNode ;
45+ onError : ( error : Error , errorInfo : React . ErrorInfo ) => Error ;
46+ } ,
47+ unknown
48+ > {
49+ override componentDidCatch ( error : Error , errorInfo : React . ErrorInfo ) : never {
50+ throw this . props . onError ( error , errorInfo ) ;
51+ }
52+
53+ override render ( ) : React . ReactNode {
54+ return this . props . children ;
55+ }
2856}
0 commit comments