@@ -29,7 +29,11 @@ import {
2929 ContextProvider ,
3030 ForwardRef ,
3131} from 'react-reconciler/src/ReactWorkTags' ;
32- import { createUnsupportedFeatureError } from './ReactDebugCustomErrors' ;
32+ import {
33+ createUnsupportedFeatureError ,
34+ createRenderFunctionError ,
35+ ErrorsNames ,
36+ } from './ReactDebugCustomErrors' ;
3337
3438type CurrentDispatcherRef = typeof ReactSharedInternals . ReactCurrentDispatcher ;
3539
@@ -666,6 +670,18 @@ function processDebugValues(
666670 }
667671}
668672
673+ function handleRenderFunctionError ( error : any ) : void {
674+ if ( error instanceof Error && error . name === ErrorsNames . UNSUPPORTTED_FEATURE_ERROR ) {
675+ throw error ;
676+ }
677+ // If the error is not caused by an unsupported feature, it means
678+ // that the error is caused by user's code in renderFunction.
679+ // In this case, we should wrap the original error inside a custom error
680+ // so that devtools can show a clear message for it.
681+ // Note: original error might be any type.
682+ throw createRenderFunctionError ( 'Error rendering inspected component' , { cause : error } ) ;
683+ }
684+
669685export function inspectHooks < Props > (
670686 renderFunction : Props => React$Node ,
671687 props : Props ,
@@ -685,6 +701,8 @@ export function inspectHooks<Props>(
685701 try {
686702 ancestorStackError = new Error ( ) ;
687703 renderFunction ( props ) ;
704+ } catch ( error ) {
705+ handleRenderFunctionError ( error ) ;
688706 } finally {
689707 readHookLog = hookLog ;
690708 hookLog = [ ] ;
@@ -729,6 +747,8 @@ function inspectHooksOfForwardRef<Props, Ref>(
729747 try {
730748 ancestorStackError = new Error ( ) ;
731749 renderFunction ( props , ref ) ;
750+ } catch (error) {
751+ handleRenderFunctionError ( error ) ;
732752 } finally {
733753 readHookLog = hookLog ;
734754 hookLog = [ ] ;
0 commit comments