Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error boundary in inspected elements panel #21531

Merged
merged 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
.Error {
justify-content: center;
align-items: center;
display: flex;
flex-direction: column;
height: 100%;
font-size: var(--font-size-sans-large);
font-weight: bold;
text-align: center;
background-color: var(--color-error-background);
color: var(--color-error-text);
border: 1px solid var(--color-error-border);
padding: 1rem;
}

.Message {
margin-bottom: 1rem;
}

.RetryButton {
.Wrapper {
border-left: 1px solid var(--color-border);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,86 +8,19 @@
*/

import * as React from 'react';
import {Component, useContext} from 'react';
import {TreeDispatcherContext} from './TreeContext';
import Button from 'react-devtools-shared/src/devtools/views/Button';
import ErrorBoundary from '../ErrorBoundary';
import styles from './InspectedElementErrorBoundary.css';

import type {DispatcherContext} from './InspectedElementErrorBoundary.css';

type WrapperProps = {|
children: React$Node,
|};

export default function InspectedElementErrorBoundaryWrapper({
children,
}: WrapperProps) {
const dispatch = useContext(TreeDispatcherContext);

return (
<InspectedElementErrorBoundary children={children} dispatch={dispatch} />
<div className={styles.Wrapper}>
<ErrorBoundary canDismiss={true}>{children}</ErrorBoundary>
</div>
);
}

type Props = {|
children: React$Node,
dispatch: DispatcherContext,
|};

type State = {|
errorMessage: string | null,
hasError: boolean,
|};

const InitialState: State = {
errorMessage: null,
hasError: false,
};

class InspectedElementErrorBoundary extends Component<Props, State> {
state: State = InitialState;

static getDerivedStateFromError(error: any) {
const errorMessage =
typeof error === 'object' &&
error !== null &&
error.hasOwnProperty('message')
? error.message
: error;

return {
errorMessage,
hasError: true,
};
}

render() {
const {children} = this.props;
const {errorMessage, hasError} = this.state;

if (hasError) {
return (
<div className={styles.Error}>
<div className={styles.Message}>{errorMessage || 'Error'}</div>
<Button className={styles.RetryButton} onClick={this._retry}>
Dismiss
</Button>
</div>
);
}

return children;
}

_retry = () => {
const {dispatch} = this.props;
dispatch({
type: 'SELECT_ELEMENT_BY_ID',
payload: null,
});
this.setState({
errorMessage: null,
hasError: false,
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import SuspendingErrorView from './SuspendingErrorView';

type Props = {|
children: React$Node,
store: Store,
canDismiss?: boolean,
store?: Store,
|};

type State = {|
Expand Down Expand Up @@ -70,18 +71,24 @@ export default class ErrorBoundary extends Component<Props, State> {
}

componentDidMount() {
this.props.store.addListener('error', this._onStoreError);
const {store} = this.props;
if (store != null) {
store.addListener('error', this._onStoreError);
}
}

componentWillUnmount() {
this.props.store.removeListener('error', this._onStoreError);
const {store} = this.props;
if (store != null) {
store.removeListener('error', this._onStoreError);
}
}

render() {
const {children} = this.props;
const {canDismiss: canDismissProp, children} = this.props;
const {
callStack,
canDismiss,
canDismiss: canDismissState,
componentStack,
errorMessage,
hasError,
Expand All @@ -92,7 +99,9 @@ export default class ErrorBoundary extends Component<Props, State> {
<ErrorView
callStack={callStack}
componentStack={componentStack}
dismissError={canDismiss ? this._dismissError : null}
dismissError={
canDismissProp || canDismissState ? this._dismissError : null
}
errorMessage={errorMessage}>
<Suspense fallback={<SearchingGitHubIssues />}>
<SuspendingErrorView
Expand Down