Skip to content

Commit 588c1f1

Browse files
authored
feat: allow replace ErrorBoundary compponent (#744)
1 parent dd126b0 commit 588c1f1

File tree

3 files changed

+64
-32
lines changed

3 files changed

+64
-32
lines changed

src/components/ComponentsProvider/componentsRegistry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import {StaffCard} from '../User/StaffCard';
22
import {AsideNavigation} from '../../containers/AsideNavigation/AsideNavigation';
33

44
import {ComponentsRegistryTemplate, Registry} from './registry';
5+
import {ErrorBoundaryInner} from '../ErrorBoundary/ErrorBoundary';
56

67
const componentsRegistryInner = new Registry()
78
.register('StaffCard', StaffCard)
8-
.register('AsideNavigation', AsideNavigation);
9+
.register('AsideNavigation', AsideNavigation)
10+
.register('ErrorBoundary', ErrorBoundaryInner);
911

1012
export type ComponentsRegistry = ComponentsRegistryTemplate<typeof componentsRegistryInner>;
1113

src/components/ErrorBoundary/ErrorBoundary.tsx

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,83 @@ import {Button, Disclosure} from '@gravity-ui/uikit';
66

77
import {registerError} from '../../utils/registerError';
88
import {Illustration} from '../Illustration';
9+
import {useComponent} from '../ComponentsProvider/ComponentsProvider';
910
import i18n from './i18n';
1011
import './ErrorBoundary.scss';
1112

1213
const b = cn('ydb-error-boundary');
1314

15+
export function ErrorBoundary({children}: {children?: ReactNode}) {
16+
const ErrorBoundaryComponent = useComponent('ErrorBoundary');
17+
return <ErrorBoundaryComponent>{children}</ErrorBoundaryComponent>;
18+
}
19+
1420
interface ErrorBoundaryProps {
1521
children?: ReactNode;
1622
useRetry?: boolean;
1723
onReportProblem?: (error?: Error) => void;
1824
}
1925

20-
export const ErrorBoundary = ({children, useRetry = true, onReportProblem}: ErrorBoundaryProps) => {
26+
export function ErrorBoundaryInner({
27+
children,
28+
useRetry = true,
29+
onReportProblem,
30+
}: ErrorBoundaryProps) {
2131
return (
2232
<ErrorBoundaryBase
2333
onError={(error, info) => {
2434
registerError(error, info.componentStack, 'error-boundary');
2535
}}
2636
fallbackRender={({error, resetErrorBoundary}) => {
2737
return (
28-
<div className={b(null)}>
29-
<Illustration name="error" className={b('illustration')} />
30-
<div className={b('content')}>
31-
<h2 className={b('error-title')}>{i18n('error-title')}</h2>
32-
<div className={b('error-description')}>
33-
{i18n('error-description')}
34-
</div>
35-
<Disclosure
36-
summary={i18n('show-details')}
37-
className={b('show-details')}
38-
size="m"
39-
>
40-
<pre className={b('error-details')}>{error.stack}</pre>
41-
</Disclosure>
42-
<div className={b('actions')}>
43-
{useRetry && (
44-
<Button view="outlined" onClick={resetErrorBoundary}>
45-
{i18n('button-reset')}
46-
</Button>
47-
)}
48-
{onReportProblem && (
49-
<Button view="outlined" onClick={() => onReportProblem(error)}>
50-
{i18n('report-problem')}
51-
</Button>
52-
)}
53-
</div>
54-
</div>
55-
</div>
38+
<ErrorBoundaryFallback
39+
error={error}
40+
useRetry={useRetry}
41+
resetErrorBoundary={resetErrorBoundary}
42+
onReportProblem={onReportProblem}
43+
/>
5644
);
5745
}}
5846
>
5947
{children}
6048
</ErrorBoundaryBase>
6149
);
62-
};
50+
}
51+
52+
interface ErrorBoundaryFallbackProps {
53+
error: Error;
54+
useRetry?: boolean;
55+
resetErrorBoundary: () => void;
56+
onReportProblem?: (error?: Error) => void;
57+
}
58+
export function ErrorBoundaryFallback({
59+
error,
60+
resetErrorBoundary,
61+
useRetry,
62+
onReportProblem,
63+
}: ErrorBoundaryFallbackProps) {
64+
return (
65+
<div className={b()}>
66+
<Illustration name="error" className={b('illustration')} />
67+
<div className={b('content')}>
68+
<h2 className={b('error-title')}>{i18n('error-title')}</h2>
69+
<div className={b('error-description')}>{i18n('error-description')}</div>
70+
<Disclosure summary={i18n('show-details')} className={b('show-details')} size="m">
71+
<pre className={b('error-details')}>{error.stack}</pre>
72+
</Disclosure>
73+
<div className={b('actions')}>
74+
{useRetry && (
75+
<Button view="outlined" onClick={resetErrorBoundary}>
76+
{i18n('button-reset')}
77+
</Button>
78+
)}
79+
{onReportProblem && (
80+
<Button view="outlined" onClick={() => onReportProblem(error)}>
81+
{i18n('report-problem')}
82+
</Button>
83+
)}
84+
</div>
85+
</div>
86+
</div>
87+
);
88+
}

src/lib.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
export {App as SingleClusterApp, AppSlots} from './containers/App';
22
export {AppWithClusters as MultiClusterApp} from './containers/AppWithClusters/AppWithClusters';
3-
export {ErrorBoundary} from './components/ErrorBoundary/ErrorBoundary';
3+
export {
4+
ErrorBoundaryInner as ErrorBoundary,
5+
ErrorBoundaryFallback,
6+
} from './components/ErrorBoundary/ErrorBoundary';
47

58
export {configureStore, rootReducer} from './store';
9+
export {default as appRoutes} from './routes';
610

711
export {createApi, YdbEmbeddedAPI, YdbWebVersionAPI} from './services/api';
812
export {settingsManager} from './services/settings';

0 commit comments

Comments
 (0)