-
Notifications
You must be signed in to change notification settings - Fork 49.8k
[ReactDebugTools] wrap uncaught error from rendering user's component #24216
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
Merged
mondaychen
merged 12 commits into
facebook:main
from
mondaychen:custom_error_debug_tools_2
Apr 1, 2022
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e10125d
[ReactDebugTools] wrap uncaught error from rendering user's component
mondaychen 82bcb13
fix lint
mondaychen 35faa3a
make error names more package specific
mondaychen 589deb6
update per review comments
mondaychen 56f0584
fix tests
mondaychen e180865
fix lint
mondaychen 2a3e9d4
fix tests
mondaychen e4039a4
fix lint
mondaychen 4939cb2
fix error name & nits
mondaychen ce34cfe
try catch instead of mocking error
mondaychen 19cb6e5
fix test for older node.js version
mondaychen 79d0f59
avoid false positive from try-catch in tests
mondaychen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -366,7 +366,7 @@ const DispatcherProxyHandler = { | |
| const error = new Error('Missing method in Dispatcher: ' + prop); | ||
| // Note: This error name needs to stay in sync with react-devtools-shared | ||
| // TODO: refactor this if we ever combine the devtools and debug tools packages | ||
| error.name = 'UnsupportedFeatureError'; | ||
| error.name = 'ReactDebugToolsUnsupportedHookError'; | ||
| throw error; | ||
| }, | ||
| }; | ||
|
|
@@ -667,6 +667,28 @@ function processDebugValues( | |
| } | ||
| } | ||
|
|
||
| function handleRenderFunctionError(error: any): void { | ||
| // original error might be any type. | ||
| if ( | ||
| error instanceof Error && | ||
| error.name === 'ReactDebugToolsUnsupportedHookError' | ||
| ) { | ||
| throw error; | ||
| } | ||
| // If the error is not caused by an unsupported feature, it means | ||
| // that the error is caused by user's code in renderFunction. | ||
| // In this case, we should wrap the original error inside a custom error | ||
| // so that devtools can give a clear message about it. | ||
| // $FlowFixMe: Flow doesn't know about 2nd argument of Error constructor | ||
| const wrapperError = new Error('Error rendering inspected component', { | ||
| cause: error, | ||
| }); | ||
| // Note: This error name needs to stay in sync with react-devtools-shared | ||
| // TODO: refactor this if we ever combine the devtools and debug tools packages | ||
| wrapperError.name = 'ReactDebugToolsRenderFunctionError'; | ||
| throw wrapperError; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Stylistic choice but this feels a bit cleaner to me? let newError = error;
if (!(error instanceof Error) || error.name !== 'ReactDebugToolsUnsupportedHookError') {
// If the error is not caused by an unsupported feature, it means
// that the error is caused by user's code in renderFunction.
// In this case, we should wrap the original error inside a custom error
// so that devtools can give a clear message about it.
// $FlowFixMe: Flow doesn't know about 2nd argument of Error constructor
let newError = new Error('Error rendering inspected component', {
cause: error,
});
// Note: This error name needs to stay in sync with react-devtools-shared
// TODO: refactor this if we ever combine the devtools and debug tools packages
newError.name = 'ReactDebugToolsRenderFunctionError';
}
throw newError; |
||
| } | ||
|
|
||
| export function inspectHooks<Props>( | ||
| renderFunction: Props => React$Node, | ||
| props: Props, | ||
|
|
@@ -686,6 +708,8 @@ export function inspectHooks<Props>( | |
| try { | ||
| ancestorStackError = new Error(); | ||
| renderFunction(props); | ||
| } catch (error) { | ||
| handleRenderFunctionError(error); | ||
| } finally { | ||
| readHookLog = hookLog; | ||
| hookLog = []; | ||
|
|
@@ -730,6 +754,8 @@ function inspectHooksOfForwardRef<Props, Ref>( | |
| try { | ||
| ancestorStackError = new Error(); | ||
| renderFunction(props, ref); | ||
| } catch (error) { | ||
| handleRenderFunctionError(error); | ||
| } finally { | ||
| readHookLog = hookLog; | ||
| hookLog = []; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.