Skip to content

Commit

Permalink
[ReactDebugTools] override error name instead of custom error class
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaychen committed Mar 28, 2022
1 parent 7068622 commit 626cf89
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
15 changes: 6 additions & 9 deletions packages/react-debug-tools/src/ReactDebugCustomErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ export const ErrorsNames = {
UNSUPPORTTED_FEATURE_ERROR: 'UnsupportedFeatureError',
};

export function UnsupportedFeatureError(message: string = "") {
// Make this an instanceof Error.
this.constructor.prototype.__proto__ = Error.prototype;

this.name = ErrorsNames.UNSUPPORTTED_FEATURE_ERROR;
this.message = message;
if (Error.captureStackTrace) { // only available in V8
Error.captureStackTrace(this, this.constructor);
}
// 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;
}
4 changes: 2 additions & 2 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
ContextProvider,
ForwardRef,
} from 'react-reconciler/src/ReactWorkTags';
import {UnsupportedFeatureError} from './ReactDebugCustomErrors';
import {createUnsupportedFeatureError} from './ReactDebugCustomErrors';

type CurrentDispatcherRef = typeof ReactSharedInternals.ReactCurrentDispatcher;

Expand Down Expand Up @@ -364,7 +364,7 @@ const DispatcherProxyHandler = {
if (target.hasOwnProperty(prop)) {
return Reflect.get(...arguments);
}
throw new UnsupportedFeatureError('Missing method in Dispatcher: ' + prop);
throw createUnsupportedFeatureError('Missing method in Dispatcher: ' + prop);
},
};

Expand Down

0 comments on commit 626cf89

Please sign in to comment.