Skip to content

Commit ddff1a4

Browse files
committed
[inspector] Error boundary
1 parent cdc39f9 commit ddff1a4

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/** @jsx createElement */
2+
3+
import {ErrorInfo, ReactNode} from 'react';
4+
import {PureComponent, createElement} from '../ui-react/common';
5+
6+
interface Props {
7+
readonly children: ReactNode;
8+
}
9+
10+
interface State {
11+
e: 0 | 1;
12+
}
13+
14+
export class ErrorBoundary extends PureComponent<Props, State> {
15+
constructor(props: Props) {
16+
super(props);
17+
this.state = {e: 0};
18+
}
19+
20+
static getDerivedStateFromError = () => ({e: 1});
21+
22+
// eslint-disable-next-line react/no-arrow-function-lifecycle
23+
componentDidCatch = (error: Error, info: ErrorInfo) =>
24+
// eslint-disable-next-line no-console
25+
console.error(error, info.componentStack);
26+
27+
render() {
28+
return this.state.e ? (
29+
<span className="warn">
30+
Inspector error: please see console for details.
31+
</span>
32+
) : (
33+
this.props.children
34+
);
35+
}
36+
}

src/store-inspector/Panel.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import {OPEN_VALUE, POSITION_VALUE} from './common';
44
import {Body} from './Body';
5+
import {ErrorBoundary} from './ErrorBoundary';
56
import {Header} from './Header';
67
import {StoreProp} from './types';
78
import {createElement} from '../ui-react/common';
@@ -13,7 +14,9 @@ export const Panel = ({s}: StoreProp) => {
1314
return useValue(OPEN_VALUE, s) ? (
1415
<main data-position={position}>
1516
<Header s={s} />
16-
<Body s={s} />
17+
<ErrorBoundary>
18+
<Body s={s} />
19+
</ErrorBoundary>
1720
</main>
1821
) : null;
1922
};

src/store-inspector/style.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export const APP_STYLESHEET = arrayJoin(
8585
'button.next': 'margin-right:0.5rem',
8686
[`th,#${UNIQUE_ID} td`]:
8787
'overflow:hidden;text-overflow:ellipsis;padding:0.25rem 0.5rem;max-width:12rem;white-space:nowrap;border-width:1px 0;border-style:solid;border-color:#777;text-align:left',
88+
89+
'span.warn': 'margin:0.25rem;color:#d81b60',
8890
},
8991
(style, selector) => (style ? `#${UNIQUE_ID} ${selector}{${style}}` : ''),
9092
),

src/ui-react/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {Store} from '../types/store';
77
import {isUndefined} from '../common/other';
88

99
export const {
10-
createElement,
10+
PureComponent,
1111
Fragment,
12+
createElement,
1213
useCallback,
1314
useLayoutEffect,
1415
useRef,

0 commit comments

Comments
 (0)