Skip to content

Commit

Permalink
[ReactDebugTools] add custom error type for future new hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaychen committed Mar 28, 2022
1 parent e7d0053 commit a11680b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
27 changes: 27 additions & 0 deletions packages/react-debug-tools/src/ReactDebugCustomErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

/**
* This file contains a list of custom Errors that ReactDebugTools can throw in
* special occasions.
* The names of the errors are exported so that other packages (such as DevTools)
* can use them to detect and handle them separately.
*/

export const ErrorsNames = {
UNSUPPORTTED_FEATURE_ERROR: 'UnsupportedFeatureError',
};

// For now we just override the name. If we decide to move react-debug-tools to
// devtools package, we should use a real Error class instead.
export function createUnsupportedFeatureError(message: string = '') {
const error = new Error(message);
error.name = ErrorsNames.UNSUPPORTTED_FEATURE_ERROR;
return error;
}
18 changes: 16 additions & 2 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
ContextProvider,
ForwardRef,
} from 'react-reconciler/src/ReactWorkTags';
import {createUnsupportedFeatureError} from './ReactDebugCustomErrors';

type CurrentDispatcherRef = typeof ReactSharedInternals.ReactCurrentDispatcher;

Expand Down Expand Up @@ -356,6 +357,19 @@ const Dispatcher: DispatcherType = {
useId,
};

// create a proxy to throw a custom error
// in case future versions of React adds more hooks
const DispatcherProxyHandler = {
get(target, prop, _receiver) {
if (target.hasOwnProperty(prop)) {
return Reflect.get(...arguments);
}
throw createUnsupportedFeatureError('Missing method in Dispatcher: ' + prop);
},
};

const DispatcherProxy = new Proxy(Dispatcher, DispatcherProxyHandler);

// Inspect

export type HookSource = {
Expand Down Expand Up @@ -664,7 +678,7 @@ export function inspectHooks<Props>(

const previousDispatcher = currentDispatcher.current;
let readHookLog;
currentDispatcher.current = Dispatcher;
currentDispatcher.current = DispatcherProxy;
let ancestorStackError;
try {
ancestorStackError = new Error();
Expand Down Expand Up @@ -708,7 +722,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
): HooksTree {
const previousDispatcher = currentDispatcher.current;
let readHookLog;
currentDispatcher.current = Dispatcher;
currentDispatcher.current = DispatcherProxy;
let ancestorStackError;
try {
ancestorStackError = new Error();
Expand Down
3 changes: 2 additions & 1 deletion packages/react-debug-tools/src/ReactDebugTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
*/

import {inspectHooks, inspectHooksOfFiber} from './ReactDebugHooks';
import {ErrorsNames} from './ReactDebugCustomErrors';

export {inspectHooks, inspectHooksOfFiber};
export {inspectHooks, inspectHooksOfFiber, ErrorsNames};

0 comments on commit a11680b

Please sign in to comment.