Include error name in error chunks (#32157)
Sebastian "Sebbie" Silbermann committed
Jan 22, 2025 at 16:39 UTC
5f05181a8b527260fc0a338edcde0e9d3b35ab20
4 files changed
+31
-17
packages/react-client/src/ReactFlightClient.js
+6
-11
@@ -16,6 +16,7 @@ import type {
16
ReactTimeInfo,
17
ReactStackTrace,
18
ReactCallSite,
19
+ ReactErrorInfoDev,
20
} from 'shared/ReactTypes';
21
import type {LazyComponent} from 'react/src/ReactLazy';
22
@@ -2123,18 +2124,12 @@ function resolveErrorProd(response: Response): Error {
2124
2125
function resolveErrorDev(
2126
response: Response,
2126
- errorInfo: {
2127
- name: string,
2128
- message: string,
2129
- stack: ReactStackTrace,
2130
- env: string,
2131
- ...
2132
- },
2127
+ errorInfo: ReactErrorInfoDev,
2128
): 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;
2133
2134
if (!__DEV__) {
2135
// 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
+1
@@ -1387,6 +1387,7 @@ describe('ReactFlight', () => {
1387
errors: [
1388
{
1389
message: 'This is an error',
1390
+ name: 'Error',
1391
stack: expect.stringContaining(
1392
'Error: This is an error\n' +
1393
' at eval (eval at testFunction (inspected-page.html:29:11),%20%3Canonymous%3E:1:35)\n' +
packages/react-server/src/ReactFlightServer.js
+10
-6
@@ -64,6 +64,8 @@ import type {
64
ReactTimeInfo,
65
ReactStackTrace,
66
ReactCallSite,
67
+ ReactErrorInfo,
68
+ ReactErrorInfoDev,
69
} from 'shared/ReactTypes';
70
import type {ReactElement} from 'shared/ReactElementType';
71
import type {LazyComponent} from 'react/src/ReactLazy';
@@ -3093,8 +3095,8 @@ function emitPostponeChunk(
3095
3096
function serializeErrorValue(request: Request, error: Error): string {
3097
if (__DEV__) {
3096
- let name;
3097
- let message;
3098
+ let name: string = 'Error';
3099
+ let message: string;
3100
let stack: ReactStackTrace;
3101
let env = (0, request.environmentName)();
3102
try {
@@ -3112,7 +3114,7 @@ function serializeErrorValue(request: Request, error: Error): string {
3114
message = 'An error occurred but serializing the error message failed.';
3115
stack = [];
3116
}
3115
- const errorInfo = {name, message, stack, env};
3117
+ const errorInfo: ReactErrorInfoDev = {name, message, stack, env};
3118
const id = outlineModel(request, errorInfo);
3119
return '$Z' + id.toString(16);
3120
} else {
@@ -3129,13 +3131,15 @@ function emitErrorChunk(
3131
digest: string,
3132
error: mixed,
3133
): void {
3132
- let errorInfo: any;
3134
+ let errorInfo: ReactErrorInfo;
3135
if (__DEV__) {
3134
- let message;
3136
+ let name: string = 'Error';
3137
+ let message: string;
3138
let stack: ReactStackTrace;
3139
let env = (0, request.environmentName)();
3140
try {
3141
if (error instanceof Error) {
3142
+ name = error.name;
3143
// eslint-disable-next-line react-internal/safe-string-coercion
3144
message = String(error.message);
3145
stack = filterStackTrace(request, error, 0);
@@ -3157,7 +3161,7 @@ function emitErrorChunk(
3161
message = 'An error occurred but serializing the error message failed.';
3162
stack = [];
3163
}
3160
- errorInfo = {digest, message, stack, env};
3164
+ errorInfo = {digest, name, message, stack, env};
3165
} else {
3166
errorInfo = {digest};
3167
}
packages/shared/ReactTypes.js
+14
@@ -203,6 +203,20 @@ export type ReactEnvironmentInfo = {
203
+env: string,
204
};
205
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
+
220
export type ReactAsyncInfo = {
221
+type: string,
222
// Stashed Data for the Specific Execution Environment. Not part of the transport protocol