Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = {
Comment thread
mondaychen marked this conversation as resolved.
Outdated
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;
}
20 changes: 18 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,21 @@ 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) {
Comment thread
mondaychen marked this conversation as resolved.
Outdated
if (target.hasOwnProperty(prop)) {
return Reflect.get(...arguments);
Comment thread
bvaughn marked this conversation as resolved.
Outdated
}
throw createUnsupportedFeatureError(
'Missing method in Dispatcher: ' + prop,
);
},
};

const DispatcherProxy = new Proxy(Dispatcher, DispatcherProxyHandler);

// Inspect

export type HookSource = {
Expand Down Expand Up @@ -664,7 +680,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 +724,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};
Comment thread
bvaughn marked this conversation as resolved.
Outdated