Skip to content

Commit a11680b

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

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-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: 16 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,19 @@ 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('Missing method in Dispatcher: ' + prop);
368+
},
369+
};
370+
371+
const DispatcherProxy = new Proxy(Dispatcher, DispatcherProxyHandler);
372+
359373
// Inspect
360374

361375
export type HookSource = {
@@ -664,7 +678,7 @@ export function inspectHooks<Props>(
664678

665679
const previousDispatcher = currentDispatcher.current;
666680
let readHookLog;
667-
currentDispatcher.current = Dispatcher;
681+
currentDispatcher.current = DispatcherProxy;
668682
let ancestorStackError;
669683
try {
670684
ancestorStackError = new Error();
@@ -708,7 +722,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
708722
): HooksTree {
709723
const previousDispatcher = currentDispatcher.current;
710724
let readHookLog;
711-
currentDispatcher.current = Dispatcher;
725+
currentDispatcher.current = DispatcherProxy;
712726
let ancestorStackError;
713727
try {
714728
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)