Skip to content

Commit 5f05181

Browse files
authored
Include error name in error chunks (facebook#32157)
1 parent b000019 commit 5f05181

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
ReactTimeInfo,
1717
ReactStackTrace,
1818
ReactCallSite,
19+
ReactErrorInfoDev,
1920
} from 'shared/ReactTypes';
2021
import type {LazyComponent} from 'react/src/ReactLazy';
2122

@@ -2123,18 +2124,12 @@ function resolveErrorProd(response: Response): Error {
21232124

21242125
function resolveErrorDev(
21252126
response: Response,
2126-
errorInfo: {
2127-
name: string,
2128-
message: string,
2129-
stack: ReactStackTrace,
2130-
env: string,
2131-
...
2132-
},
2127+
errorInfo: ReactErrorInfoDev,
21332128
): Error {
2134-
const name: string = errorInfo.name;
2135-
const message: string = errorInfo.message;
2136-
const stack: ReactStackTrace = errorInfo.stack;
2137-
const env: string = errorInfo.env;
2129+
const name = errorInfo.name;
2130+
const message = errorInfo.message;
2131+
const stack = errorInfo.stack;
2132+
const env = errorInfo.env;
21382133

21392134
if (!__DEV__) {
21402135
// These errors should never make it into a build so we don't need to encode them in codes.json

packages/react-client/src/__tests__/ReactFlight-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,7 @@ describe('ReactFlight', () => {
13871387
errors: [
13881388
{
13891389
message: 'This is an error',
1390+
name: 'Error',
13901391
stack: expect.stringContaining(
13911392
'Error: This is an error\n' +
13921393
' at eval (eval at testFunction (inspected-page.html:29:11),%20%3Canonymous%3E:1:35)\n' +

packages/react-server/src/ReactFlightServer.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ import type {
6464
ReactTimeInfo,
6565
ReactStackTrace,
6666
ReactCallSite,
67+
ReactErrorInfo,
68+
ReactErrorInfoDev,
6769
} from 'shared/ReactTypes';
6870
import type {ReactElement} from 'shared/ReactElementType';
6971
import type {LazyComponent} from 'react/src/ReactLazy';
@@ -3093,8 +3095,8 @@ function emitPostponeChunk(
30933095

30943096
function serializeErrorValue(request: Request, error: Error): string {
30953097
if (__DEV__) {
3096-
let name;
3097-
let message;
3098+
let name: string = 'Error';
3099+
let message: string;
30983100
let stack: ReactStackTrace;
30993101
let env = (0, request.environmentName)();
31003102
try {
@@ -3112,7 +3114,7 @@ function serializeErrorValue(request: Request, error: Error): string {
31123114
message = 'An error occurred but serializing the error message failed.';
31133115
stack = [];
31143116
}
3115-
const errorInfo = {name, message, stack, env};
3117+
const errorInfo: ReactErrorInfoDev = {name, message, stack, env};
31163118
const id = outlineModel(request, errorInfo);
31173119
return '$Z' + id.toString(16);
31183120
} else {
@@ -3129,13 +3131,15 @@ function emitErrorChunk(
31293131
digest: string,
31303132
error: mixed,
31313133
): void {
3132-
let errorInfo: any;
3134+
let errorInfo: ReactErrorInfo;
31333135
if (__DEV__) {
3134-
let message;
3136+
let name: string = 'Error';
3137+
let message: string;
31353138
let stack: ReactStackTrace;
31363139
let env = (0, request.environmentName)();
31373140
try {
31383141
if (error instanceof Error) {
3142+
name = error.name;
31393143
// eslint-disable-next-line react-internal/safe-string-coercion
31403144
message = String(error.message);
31413145
stack = filterStackTrace(request, error, 0);
@@ -3157,7 +3161,7 @@ function emitErrorChunk(
31573161
message = 'An error occurred but serializing the error message failed.';
31583162
stack = [];
31593163
}
3160-
errorInfo = {digest, message, stack, env};
3164+
errorInfo = {digest, name, message, stack, env};
31613165
} else {
31623166
errorInfo = {digest};
31633167
}

packages/shared/ReactTypes.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,20 @@ export type ReactEnvironmentInfo = {
203203
+env: string,
204204
};
205205

206+
export type ReactErrorInfoProd = {
207+
+digest: string,
208+
};
209+
210+
export type ReactErrorInfoDev = {
211+
+digest?: string,
212+
+name: string,
213+
+message: string,
214+
+stack: ReactStackTrace,
215+
+env: string,
216+
};
217+
218+
export type ReactErrorInfo = ReactErrorInfoProd | ReactErrorInfoDev;
219+
206220
export type ReactAsyncInfo = {
207221
+type: string,
208222
// Stashed Data for the Specific Execution Environment. Not part of the transport protocol

0 commit comments

Comments
 (0)