Skip to content

Commit

Permalink
Errors thrown by Store can be dismissed in boundary (facebook#21520)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn authored and koto committed Jun 15, 2021
1 parent 06f998a commit adc17c1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ type Props = {|

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

const InitialState: State = {
callStack: null,
canDismiss: false,
componentStack: null,
errorMessage: null,
hasError: false,
Expand Down Expand Up @@ -77,13 +79,20 @@ export default class ErrorBoundary extends Component<Props, State> {

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

if (hasError) {
return (
<ErrorView
callStack={callStack}
componentStack={componentStack}
dismissError={canDismiss ? this._dismissError : null}
errorMessage={errorMessage}>
<Suspense fallback={<SearchingGitHubIssues />}>
<SuspendingErrorView
Expand All @@ -99,9 +108,16 @@ export default class ErrorBoundary extends Component<Props, State> {
return children;
}

_dismissError = () => {
this.setState(InitialState);
};

_onStoreError = (error: Error) => {
if (!this.state.hasError) {
this.setState(ErrorBoundary.getDerivedStateFromError(error));
this.setState({
...ErrorBoundary.getDerivedStateFromError(error),
canDismiss: true,
});
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,39 @@
*/

import * as React from 'react';
import Button from '../Button';
import ButtonIcon from '../ButtonIcon';
import styles from './shared.css';

type Props = {|
callStack: string | null,
children: React$Node,
componentStack: string | null,
dismissError: Function | null,
errorMessage: string | null,
|};

export default function ErrorView({
callStack,
children,
componentStack,
dismissError = null,
errorMessage,
}: Props) {
return (
<div className={styles.ErrorBoundary}>
{children}
<div className={styles.ErrorInfo}>
<div className={styles.Header}>
Uncaught Error: {errorMessage || ''}
<div className={styles.HeaderRow}>
<div className={styles.Header}>
Uncaught Error: {errorMessage || ''}
</div>
{dismissError !== null && (
<Button className={styles.CloseButton} onClick={dismissError}>
Dismiss
<ButtonIcon className={styles.CloseButtonIcon} type="close" />
</Button>
)}
</div>
{!!callStack && (
<div className={styles.Stack}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,22 @@
overflow: auto;
}

.Header {
.HeaderRow {
display: flex;
flex-direction: row;
font-size: var(--font-size-sans-large);
font-weight: bold;
color: var(--color-error-text);
}

.Header {
flex: 1 1 auto;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
min-width: 0;
}

.Stack {
margin-top: 0.5rem;
white-space: pre-wrap;
Expand Down Expand Up @@ -75,9 +85,21 @@
.ReproSteps {
margin-left: 0.25rem;
color: var(--color-console-warning-text);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
min-width: 0;
}

.UpdateExistingIssuePrompt {
margin-right: 0.25rem;
color: var(--color-console-warning-text);
}

.CloseButton {
font-weight: bold;
}

.CloseButtonIcon {
margin-left: 0.25rem;
}

0 comments on commit adc17c1

Please sign in to comment.