Skip to content

Commit 60cb32b

Browse files
committed
[ReactDebugTools] add custom error type for future new hooks
1 parent e7d0053 commit 60cb32b

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
/**
11+
* This file contains a list of custom Errors that ReactDebugTools can throw in
12+
* special occasions.
13+
* The names of the errors are exported so that other packages (such as DevTools)
14+
* can use them to detect and handle them separately.
15+
*/
16+
17+
export const ErrorsNames = {
18+
UNSUPPORTTED_FEATURE_ERROR: 'UnsupportedFeatureError',
19+
};
20+
21+
// For now we just override the name. If we decide to move react-debug-tools to
22+
// devtools package, we should use a real Error class instead.
23+
export function createUnsupportedFeatureError(message: string = '') {
24+
const error = new Error(message);
25+
error.name = ErrorsNames.UNSUPPORTTED_FEATURE_ERROR;
26+
return error;
27+
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
ContextProvider,
3030
ForwardRef,
3131
} from 'react-reconciler/src/ReactWorkTags';
32+
import {createUnsupportedFeatureError} from './ReactDebugCustomErrors';
3233

3334
type CurrentDispatcherRef = typeof ReactSharedInternals.ReactCurrentDispatcher;
3435

@@ -356,6 +357,21 @@ const Dispatcher: DispatcherType = {
356357
useId,
357358
};
358359

360+
// create a proxy to throw a custom error
361+
// in case future versions of React adds more hooks
362+
const DispatcherProxyHandler = {
363+
get(target, prop, _receiver) {
364+
if (target.hasOwnProperty(prop)) {
365+
return Reflect.get(...arguments);
366+
}
367+
throw createUnsupportedFeatureError(
368+
'Missing method in Dispatcher: ' + prop,
369+
);
370+
},
371+
};
372+
373+
const DispatcherProxy = new Proxy(Dispatcher, DispatcherProxyHandler);
374+
359375
// Inspect
360376

361377
export type HookSource = {
@@ -664,7 +680,7 @@ export function inspectHooks<Props>(
664680

665681
const previousDispatcher = currentDispatcher.current;
666682
let readHookLog;
667-
currentDispatcher.current = Dispatcher;
683+
currentDispatcher.current = DispatcherProxy;
668684
let ancestorStackError;
669685
try {
670686
ancestorStackError = new Error();
@@ -708,7 +724,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
708724
): HooksTree {
709725
const previousDispatcher = currentDispatcher.current;
710726
let readHookLog;
711-
currentDispatcher.current = Dispatcher;
727+
currentDispatcher.current = DispatcherProxy;
712728
let ancestorStackError;
713729
try {
714730
ancestorStackError = new Error();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
*/
99

1010
import {inspectHooks, inspectHooksOfFiber} from './ReactDebugHooks';
11+
import {ErrorsNames} from './ReactDebugCustomErrors';
1112

12-
export {inspectHooks, inspectHooksOfFiber};
13+
export {inspectHooks, inspectHooksOfFiber, ErrorsNames};

0 commit comments

Comments
 (0)