Skip to content

Commit e9aef87

Browse files
committed
better error message per review
1 parent 9613f9a commit e9aef87

File tree

5 files changed

+34
-79
lines changed

5 files changed

+34
-79
lines changed

packages/react-devtools-shared/src/backend/renderer.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,7 +3614,9 @@ export function attach(
36143614
console.error(message + '\n\n', error);
36153615
if (error.cause != null) {
36163616
console.error(
3617-
'Original error causing above error: \n\n',
3617+
'React DevTools encountered an error while trying to inspect hooks. ' +
3618+
'This is most likely caused by an error in current inspected component.' +
3619+
'The error thrown in the component is: \n\n',
36183620
error.cause,
36193621
);
36203622
if (error.cause instanceof Error) {
@@ -3624,7 +3626,8 @@ export function attach(
36243626
}
36253627

36263628
return {
3627-
type: 'user-error',
3629+
type: 'error',
3630+
errorType: 'user',
36283631
id,
36293632
responseID: requestID,
36303633
message,
@@ -3635,7 +3638,8 @@ export function attach(
36353638
// the error name is synced with ReactDebugHooks
36363639
if (error.name === 'ReactDebugToolsUnsupportedHookError') {
36373640
return {
3638-
type: 'unsupported-feature',
3641+
type: 'error',
3642+
errorType: 'unknown-hook',
36393643
id,
36403644
responseID: requestID,
36413645
message: 'Unsupported feature: ' + error.message,
@@ -3647,6 +3651,7 @@ export function attach(
36473651

36483652
return {
36493653
type: 'error',
3654+
errorType: 'uncaught',
36503655
id,
36513656
responseID: requestID,
36523657
message: error.message,

packages/react-devtools-shared/src/backend/types.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ export type InspectedElement = {|
281281
|};
282282

283283
export const InspectElementErrorType = 'error';
284-
export const InspectElementUserErrorType = 'user-error';
285-
export const InspectElementUnsupportedFeatureErrorType = 'unsupported-feature';
286284
export const InspectElementFullDataType = 'full-data';
287285
export const InspectElementNoChangeType = 'no-change';
288286
export const InspectElementNotFoundType = 'not-found';
@@ -291,23 +289,9 @@ export type InspectElementError = {|
291289
id: number,
292290
responseID: number,
293291
type: 'error',
292+
errorType: 'user' | 'unknown-hook' | 'uncaught',
294293
message: string,
295-
stack: string,
296-
|};
297-
298-
export type InspectElementUserError = {|
299-
id: number,
300-
responseID: number,
301-
type: 'user-error',
302-
message: string,
303-
stack: ?string,
304-
|};
305-
306-
export type InspectElementUnsupportedFeatureError = {|
307-
id: number,
308-
responseID: number,
309-
type: 'unsupported-feature',
310-
message: string,
294+
stack?: string,
311295
|};
312296

313297
export type InspectElementFullData = {|
@@ -339,8 +323,6 @@ export type InspectElementNotFound = {|
339323

340324
export type InspectedElementPayload =
341325
| InspectElementError
342-
| InspectElementUserError
343-
| InspectElementUnsupportedFeatureError
344326
| InspectElementFullData
345327
| InspectElementHydratedPath
346328
| InspectElementNoChange

packages/react-devtools-shared/src/devtools/views/ErrorBoundary/ErrorBoundary.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import CaughtErrorView from './CaughtErrorView';
1919
import UnsupportedBridgeOperationError from 'react-devtools-shared/src/UnsupportedBridgeOperationError';
2020
import TimeoutError from 'react-devtools-shared/src/errors/TimeoutError';
2121
import UserError from 'react-devtools-shared/src/errors/UserError';
22-
import UnsupportedFeatureError from 'react-devtools-shared/src/errors/UnsupportedFeatureError';
22+
import UnknownHookError from 'react-devtools-shared/src/errors/UnknownHookError';
2323
import {logEvent} from 'react-devtools-shared/src/Logger';
2424

2525
type Props = {|
@@ -38,7 +38,7 @@ type State = {|
3838
isUnsupportedBridgeOperationError: boolean,
3939
isTimeout: boolean,
4040
isUserError: boolean,
41-
isUnsupportedFeatureError: boolean,
41+
isUnknownHookError: boolean,
4242
|};
4343

4444
const InitialState: State = {
@@ -50,7 +50,7 @@ const InitialState: State = {
5050
isUnsupportedBridgeOperationError: false,
5151
isTimeout: false,
5252
isUserError: false,
53-
isUnsupportedFeatureError: false,
53+
isUnknownHookError: false,
5454
};
5555

5656
export default class ErrorBoundary extends Component<Props, State> {
@@ -66,7 +66,7 @@ export default class ErrorBoundary extends Component<Props, State> {
6666

6767
const isTimeout = error instanceof TimeoutError;
6868
const isUserError = error instanceof UserError;
69-
const isUnsupportedFeatureError = error instanceof UnsupportedFeatureError;
69+
const isUnknownHookError = error instanceof UnknownHookError;
7070
const isUnsupportedBridgeOperationError =
7171
error instanceof UnsupportedBridgeOperationError;
7272

@@ -85,7 +85,7 @@ export default class ErrorBoundary extends Component<Props, State> {
8585
errorMessage,
8686
hasError: true,
8787
isUnsupportedBridgeOperationError,
88-
isUnsupportedFeatureError,
88+
isUnknownHookError,
8989
isTimeout,
9090
isUserError,
9191
};
@@ -123,7 +123,7 @@ export default class ErrorBoundary extends Component<Props, State> {
123123
isUnsupportedBridgeOperationError,
124124
isTimeout,
125125
isUserError,
126-
isUnsupportedFeatureError,
126+
isUnknownHookError,
127127
} = this.state;
128128

129129
if (hasError) {
@@ -160,20 +160,17 @@ export default class ErrorBoundary extends Component<Props, State> {
160160
}
161161
/>
162162
);
163-
} else if (isUnsupportedFeatureError) {
163+
} else if (isUnknownHookError) {
164164
return (
165165
<CaughtErrorView
166166
callStack={callStack}
167167
componentStack={componentStack}
168-
errorMessage={
169-
errorMessage ||
170-
'Current DevTools version does not support a feature used in the inspected element.'
171-
}
168+
errorMessage={errorMessage || 'Encountered an unknown hook'}
172169
info={
173170
<>
174-
React DevTools is unable to handle a feature you are using in
175-
this component (e.g. a new React build-in Hook). Please upgrade
176-
to the latest version.
171+
React DevTools encountered an unknown hook. This is probably
172+
because the package is out of date. To fix, upgrade the package
173+
to the most recent version.
177174
</>
178175
}
179176
/>

packages/react-devtools-shared/src/errors/UnsupportedFeatureError.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/react-devtools-shared/src/inspectedElementMutableSource.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import type {LRUCache} from 'react-devtools-shared/src/types';
1919
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
2020
import type {
2121
InspectElementError,
22-
InspectElementUserError,
23-
InspectElementUnsupportedFeatureError,
2422
InspectElementFullData,
2523
InspectElementHydratedPath,
2624
} from 'react-devtools-shared/src/backend/types';
@@ -30,7 +28,7 @@ import type {
3028
InspectedElementResponseType,
3129
} from 'react-devtools-shared/src/devtools/views/Components/types';
3230
import UserError from 'react-devtools-shared/src/errors/UserError';
33-
import UnsupportedFeatureError from 'react-devtools-shared/src/errors/UnsupportedFeatureError';
31+
import UnknownHookError from 'react-devtools-shared/src/errors/UnknownHookError';
3432

3533
// Maps element ID to inspected data.
3634
// We use an LRU for this rather than a WeakMap because of how the "no-change" optimization works.
@@ -85,27 +83,21 @@ export function inspectElement({
8583
let inspectedElement;
8684
switch (type) {
8785
case 'error': {
88-
const {message, stack} = ((data: any): InspectElementError);
89-
86+
const {message, stack, errorType} = ((data: any): InspectElementError);
87+
88+
// create a different error class for each error type
89+
// and keep useful information from backend.
90+
let error;
91+
if (errorType === 'user') {
92+
error = new UserError(message);
93+
} else if (errorType === 'unknown-hook') {
94+
error = new UnknownHookError(message);
95+
} else {
96+
error = new Error(message);
97+
}
9098
// The backend's stack (where the error originated) is more meaningful than this stack.
91-
const error = new Error(message);
92-
error.stack = stack;
93-
94-
throw error;
95-
}
96-
97-
case 'user-error': {
98-
const {message, stack} = (data: InspectElementUserError);
99-
// Trying to keep useful information from user's component.
100-
const error = new UserError(message);
10199
error.stack = stack || error.stack;
102-
throw error;
103-
}
104100

105-
case 'unsupported-feature': {
106-
const {message} = (data: InspectElementUnsupportedFeatureError);
107-
// Trying to keep useful information from backend.
108-
const error = new UnsupportedFeatureError(message);
109101
throw error;
110102
}
111103

0 commit comments

Comments
 (0)