Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ReactDebugTools] add custom error type for future new hooks #24168

Merged
merged 3 commits into from
Mar 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,23 @@ 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) {
if (target.hasOwnProperty(prop)) {
return target[prop];
}
const error = new Error('Missing method in Dispatcher: ' + prop);
// Note: This error name needs to stay in sync with react-devtools-shared
// TODO: refactor this if we ever combine the devtools and debug tools packages
error.name = 'UnsupportedFeatureError';
Copy link
Contributor

@lunaruan lunaruan Mar 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this! Could you make this error name something more package specific (ex. 'ReactDebugTools') to minimize name clashes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about ReactDebugToolsError (for more consistency with built in error names)

throw error;
},
};

const DispatcherProxy = new Proxy(Dispatcher, DispatcherProxyHandler);

// Inspect

export type HookSource = {
Expand Down Expand Up @@ -664,7 +681,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 +725,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