Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(fetch): only preserve error code across serialization (#43039)

Dmitry Gozman0dd42e8f8f24
diff --git a/packages/playwright-core/src/client/errors.ts b/packages/playwright-core/src/client/errors.ts
index 27bb6734bf..884cc4a908 100644
--- a/packages/playwright-core/src/client/errors.ts
+++ b/packages/playwright-core/src/client/errors.ts
@@ -15,7 +15,7 @@
*/
import { isError } from '@isomorphic/rtti';
-import { parseSerializedValue, parseSystemErrorFields, serializeSystemErrorFields, serializeValue, systemErrorMessage } from '@protocol/serializers';
+import { parseSerializedValue, serializeValue, systemErrorCode, systemErrorMessage } from '@protocol/serializers';
import type { SerializedError } from './channels';
@@ -51,7 +51,7 @@ export function isTargetClosedError(error: Error) {
export function serializeError(e: any): SerializedError {
if (isError(e))
- return { error: { message: systemErrorMessage(e), stack: e.stack, name: e.name, ...serializeSystemErrorFields(e) } };
+ return { error: { message: systemErrorMessage(e), stack: e.stack, name: e.name, code: systemErrorCode(e) } };
return { value: serializeValue(e, value => ({ fallThrough: value })) };
}
@@ -71,6 +71,7 @@ export function parseError(error: SerializedError): PlaywrightError {
else
e = Object.assign(new PlaywrightError(error.error.message), { name: error.error.name });
e.stack = error.error.stack || '';
- parseSystemErrorFields(error.error, e);
+ if (error.error.code !== undefined)
+ (e as any).code = error.error.code;
return e;
}
diff --git a/packages/playwright-core/src/server/errors.ts b/packages/playwright-core/src/server/errors.ts
index d250a5cfac..a6f057135e 100644
--- a/packages/playwright-core/src/server/errors.ts
+++ b/packages/playwright-core/src/server/errors.ts
@@ -15,7 +15,7 @@
*/
import { isError } from '@isomorphic/rtti';
-import { parseSerializedValue, parseSystemErrorFields, serializeSystemErrorFields, serializeValue, systemErrorMessage } from '@protocol/serializers';
+import { parseSerializedValue, serializeValue, systemErrorCode, systemErrorMessage } from '@protocol/serializers';
import { rewriteErrorMessage } from '@utils/stackTrace';
import { isProtocolError } from './protocolError';
@@ -66,7 +66,7 @@ export function rewriteErrorForClosedTarget(error: Error, closeReason: string |
export function serializeError(e: any): SerializedError {
if (isError(e))
- return { error: { message: systemErrorMessage(e), stack: e.stack, name: e.name, ...serializeSystemErrorFields(e) } };
+ return { error: { message: systemErrorMessage(e), stack: e.stack, name: e.name, code: systemErrorCode(e) } };
return { value: serializeValue(e, value => ({ fallThrough: value })) };
}
@@ -79,6 +79,7 @@ export function parseError(error: SerializedError): Error {
const e = new Error(error.error.message);
e.stack = error.error.stack || '';
e.name = error.error.name;
- parseSystemErrorFields(error.error, e);
+ if (error.error.code !== undefined)
+ (e as any).code = error.error.code;
return e;
}
diff --git a/packages/protocol/spec/serialized.yml b/packages/protocol/spec/serialized.yml
index e40ee4803c..0f9a8fa365 100644
--- a/packages/protocol/spec/serialized.yml
+++ b/packages/protocol/spec/serialized.yml
@@ -104,11 +104,6 @@ SerializedError:
message: string
name: string
stack: string?
- # See https://nodejs.org/api/errors.html#class-systemerror
+ # See https://nodejs.org/api/errors.html#errorcode
code: string?
- errno: int?
- syscall: string?
- address: string?
- port: int?
- hostname: string?
value: SerializedValue?
diff --git a/packages/protocol/src/serializers.ts b/packages/protocol/src/serializers.ts
index 0b245c1b8f..2504b4d8ae 100644
--- a/packages/protocol/src/serializers.ts
+++ b/packages/protocol/src/serializers.ts
@@ -14,34 +14,11 @@
* limitations under the License.
*/
-import type { SerializedError, SerializedValue } from '@protocol/structs';
-
-type SerializedSystemError = NonNullable<SerializedError['error']>;
-
-// Structured fields of a Node.js system error, see https://nodejs.org/api/errors.html#class-systemerror.
-const systemErrorFields: { [key in keyof SerializedSystemError]?: 'string' | 'number' } = {
- code: 'string',
- errno: 'number',
- syscall: 'string',
- address: 'string',
- port: 'number',
- hostname: 'string',
-};
+import type { SerializedValue } from '@protocol/structs';
-export function serializeSystemErrorFields(error: any): Partial<SerializedSystemError> {
- const result: any = {};
- for (const [field, type] of Object.entries(systemErrorFields)) {
- if (typeof error[field] === type)
- result[field] = error[field];
- }
- return result;
-}
-
-export function parseSystemErrorFields(error: SerializedSystemError, target: Error) {
- for (const [field, type] of Object.entries(systemErrorFields)) {
- if (typeof (error as any)[field] === type)
- (target as any)[field] = (error as any)[field];
- }
+// See https://nodejs.org/api/errors.html#errorcode.
+export function systemErrorCode(error: any): string | undefined {
+ return typeof error.code === 'string' ? error.code : undefined;
}
export function systemErrorMessage(error: any): string {
diff --git a/packages/protocol/src/structs.d.ts b/packages/protocol/src/structs.d.ts
index 084e3c13e7..fbbc33f679 100644
--- a/packages/protocol/src/structs.d.ts
+++ b/packages/protocol/src/structs.d.ts
@@ -314,11 +314,6 @@ export type SerializedError = {
name: string,
stack?: string,
code?: string,
- errno?: number,
- syscall?: string,
- address?: string,
- port?: number,
- hostname?: string,
},
value?: SerializedValue,
};
diff --git a/packages/protocol/src/validator.ts b/packages/protocol/src/validator.ts
index 52f8b5ac9e..9cc85050d8 100644
--- a/packages/protocol/src/validator.ts
+++ b/packages/protocol/src/validator.ts
@@ -3097,11 +3097,6 @@ scheme.SerializedError = tObject({
name: tString,
stack: tOptional(tString),
code: tOptional(tString),
- errno: tOptional(tInt),
- syscall: tOptional(tString),
- address: tOptional(tString),
- port: tOptional(tInt),
- hostname: tOptional(tString),
})),
value: tOptional(tType('SerializedValue')),
});
diff --git a/tests/library/global-fetch.spec.ts b/tests/library/global-fetch.spec.ts
index fffd04cb1e..2a714a4145 100644
--- a/tests/library/global-fetch.spec.ts
+++ b/tests/library/global-fetch.spec.ts
@@ -719,7 +719,7 @@ it('should retry ECONNRESET', {
await request.dispose();
});
-it('should expose node error fields on network errors', {
+it('should expose node error code on network errors', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42532' }
}, async ({ playwright, server }) => {
// Reset the connection so that node reports a system error instead of a plain 'socket hang up'.
@@ -728,8 +728,8 @@ it('should expose node error fields on network errors', {
const error = await request.get(server.PREFIX + '/reset', { maxRetries: 0 }).catch(e => e);
expect(error.message).toContain('ECONNRESET');
expect(error.code).toBe('ECONNRESET');
- expect(error.syscall).toBe('read');
- expect(typeof error.errno).toBe('number');
+ expect(error.syscall).toBe(undefined);
+ expect(error.errno).toBe(undefined);
await request.dispose();
});