Skip to content

Commit e10125d

Browse files
committed
[ReactDebugTools] wrap uncaught error from rendering user's component
1 parent 0415b18 commit e10125d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,28 @@ function processDebugValues(
667667
}
668668
}
669669

670+
function handleRenderFunctionError(error: any): void {
671+
// original error might be any type.
672+
const isError = error instanceof Error;
673+
if (isError && error.name === 'UnsupportedFeatureError') {
674+
throw error;
675+
}
676+
// If the error is not caused by an unsupported feature, it means
677+
// that the error is caused by user's code in renderFunction.
678+
// In this case, we should wrap the original error inside a custom error
679+
// so that devtools can show a clear message for it.
680+
const messgae: string =
681+
isError && error.message
682+
? error.message
683+
: 'Error rendering inspected component'
684+
// $FlowFixMe: Flow doesn't know about 2nd argument of Error constructor
685+
const wrapperError = new Error(messgae, {cause: error});
686+
// Note: This error name needs to stay in sync with react-devtools-shared
687+
// TODO: refactor this if we ever combine the devtools and debug tools packages
688+
wrapperError.name = 'RenderFunctionError';
689+
throw wrapperError;
690+
}
691+
670692
export function inspectHooks<Props>(
671693
renderFunction: Props => React$Node,
672694
props: Props,
@@ -686,6 +708,8 @@ export function inspectHooks<Props>(
686708
try {
687709
ancestorStackError = new Error();
688710
renderFunction(props);
711+
} catch (error) {
712+
handleRenderFunctionError(error);
689713
} finally {
690714
readHookLog = hookLog;
691715
hookLog = [];
@@ -730,6 +754,8 @@ function inspectHooksOfForwardRef<Props, Ref>(
730754
try {
731755
ancestorStackError = new Error();
732756
renderFunction(props, ref);
757+
} catch (error) {
758+
handleRenderFunctionError(error);
733759
} finally {
734760
readHookLog = hookLog;
735761
hookLog = [];

0 commit comments

Comments
 (0)