-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DevTools error boundary: Search for pre-existing GH issues (#21279)
- Loading branch information
Brian Vaughn
authored
Apr 15, 2021
1 parent
9eddfbf
commit f3337aa
Showing
20 changed files
with
764 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
packages/react-devtools-shared/src/devtools/views/ErrorBoundary.css
This file was deleted.
Oops, something went wrong.
154 changes: 0 additions & 154 deletions
154
packages/react-devtools-shared/src/devtools/views/ErrorBoundary.js
This file was deleted.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
packages/react-devtools-shared/src/devtools/views/ErrorBoundary/ErrorBoundary.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import {Component, Suspense} from 'react'; | ||
import ErrorView from './ErrorView'; | ||
import SearchingGitHubIssues from './SearchingGitHubIssues'; | ||
import SuspendingErrorView from './SuspendingErrorView'; | ||
|
||
type Props = {| | ||
children: React$Node, | ||
|}; | ||
|
||
type State = {| | ||
callStack: string | null, | ||
componentStack: string | null, | ||
errorMessage: string | null, | ||
hasError: boolean, | ||
|}; | ||
|
||
const InitialState: State = { | ||
callStack: null, | ||
componentStack: null, | ||
errorMessage: null, | ||
hasError: false, | ||
}; | ||
|
||
export default class ErrorBoundary 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, | ||
}; | ||
} | ||
|
||
componentDidCatch(error: any, {componentStack}: any) { | ||
const callStack = | ||
typeof error === 'object' && | ||
error !== null && | ||
error.hasOwnProperty('stack') | ||
? error.stack | ||
.split('\n') | ||
.slice(1) | ||
.join('\n') | ||
: null; | ||
|
||
this.setState({ | ||
callStack, | ||
componentStack, | ||
}); | ||
} | ||
|
||
render() { | ||
const {children} = this.props; | ||
const {callStack, componentStack, errorMessage, hasError} = this.state; | ||
|
||
if (hasError) { | ||
return ( | ||
<ErrorView | ||
callStack={callStack} | ||
componentStack={componentStack} | ||
errorMessage={errorMessage}> | ||
<Suspense fallback={<SearchingGitHubIssues />}> | ||
<SuspendingErrorView | ||
callStack={callStack} | ||
componentStack={componentStack} | ||
errorMessage={errorMessage} | ||
/> | ||
</Suspense> | ||
</ErrorView> | ||
); | ||
} | ||
|
||
return children; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/react-devtools-shared/src/devtools/views/ErrorBoundary/ErrorView.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import styles from './shared.css'; | ||
|
||
type Props = {| | ||
callStack: string | null, | ||
children: React$Node, | ||
componentStack: string | null, | ||
errorMessage: string | null, | ||
|}; | ||
|
||
export default function ErrorView({ | ||
callStack, | ||
children, | ||
componentStack, | ||
errorMessage, | ||
}: Props) { | ||
return ( | ||
<div className={styles.ErrorBoundary}> | ||
{children} | ||
<div className={styles.ErrorInfo}> | ||
<div className={styles.Header}> | ||
Uncaught Error: {errorMessage || ''} | ||
</div> | ||
{!!callStack && ( | ||
<div className={styles.Stack}> | ||
The error was thrown {callStack.trim()} | ||
</div> | ||
)} | ||
{!!componentStack && ( | ||
<div className={styles.Stack}> | ||
The error occurred {componentStack.trim()} | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.