Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/playwright-core/src/client/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { isError } from '@isomorphic/rtti';
import { parseSerializedValue, serializeValue } from '@protocol/serializers';
import { parseSerializedValue, parseSystemErrorFields, serializeSystemErrorFields, serializeValue, systemErrorMessage } from '@protocol/serializers';

import type { SerializedError } from './channels';

Expand Down Expand Up @@ -51,7 +51,7 @@ export function isTargetClosedError(error: Error) {

export function serializeError(e: any): SerializedError {
if (isError(e))
return { error: { message: e.message, stack: e.stack, name: e.name } };
return { error: { message: systemErrorMessage(e), stack: e.stack, name: e.name, ...serializeSystemErrorFields(e) } };
return { value: serializeValue(e, value => ({ fallThrough: value })) };
}

Expand All @@ -71,5 +71,6 @@ 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just append it to the message, so that it works across langs?

return e;
}
5 changes: 3 additions & 2 deletions packages/playwright-core/src/server/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { isError } from '@isomorphic/rtti';
import { parseSerializedValue, serializeValue } from '@protocol/serializers';
import { parseSerializedValue, parseSystemErrorFields, serializeSystemErrorFields, serializeValue, systemErrorMessage } from '@protocol/serializers';

import type { SerializedError } from './channels';

Expand Down Expand Up @@ -50,7 +50,7 @@ export function isTargetClosedError(error: Error) {

export function serializeError(e: any): SerializedError {
if (isError(e))
return { error: { message: e.message, stack: e.stack, name: e.name } };
return { error: { message: systemErrorMessage(e), stack: e.stack, name: e.name, ...serializeSystemErrorFields(e) } };
return { value: serializeValue(e, value => ({ fallThrough: value })) };
}

Expand All @@ -63,5 +63,6 @@ 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);
return e;
}
7 changes: 7 additions & 0 deletions packages/protocol/spec/serialized.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ SerializedError:
message: string
name: string
stack: string?
# See https://nodejs.org/api/errors.html#class-systemerror
code: string?
errno: int?
syscall: string?
address: string?
port: int?
hostname: string?
value: SerializedValue?
37 changes: 36 additions & 1 deletion packages/protocol/src/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,42 @@
* limitations under the License.
*/

import type { SerializedValue } from '@protocol/structs';
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',
};

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];
}
}

export function systemErrorMessage(error: any): string {
const message = error.message;
if (typeof error.code !== 'string' || !/^E[A-Z0-9]+$/.test(error.code) || message.includes(error.code))
return message;
return `${message} (${error.code})`;
}

export function parseSerializedValue(value: SerializedValue, handles: any[] | undefined): any {
return innerParseSerializedValue(value, handles, new Map(), []);
Expand Down
6 changes: 6 additions & 0 deletions packages/protocol/src/structs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ export type SerializedError = {
message: string,
name: string,
stack?: string,
code?: string,
errno?: number,
syscall?: string,
address?: string,
port?: number,
hostname?: string,
},
value?: SerializedValue,
};
Expand Down
6 changes: 6 additions & 0 deletions packages/protocol/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2998,6 +2998,12 @@ scheme.SerializedError = tObject({
message: tString,
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')),
});
Expand Down
26 changes: 26 additions & 0 deletions tests/library/global-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,32 @@ it('should retry ECONNRESET', {
await request.dispose();
});

it('should expose node error fields 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'.
server.setRoute('/reset', req => req.socket.resetAndDestroy());
const request = await playwright.request.newContext();
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');
await request.dispose();
});

it('should append node error code to the message', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42532' }
}, async ({ playwright, server }) => {
// Node reports this one as a plain 'socket hang up' that does not mention the code.
server.setRoute('/hangup', req => req.socket.destroy());
const request = await playwright.request.newContext();
const error = await request.get(server.PREFIX + '/hangup', { maxRetries: 0 }).catch(e => e);
expect(error.message).toContain('socket hang up (ECONNRESET)');
expect(error.code).toBe('ECONNRESET');
await request.dispose();
});

it('should not crash when server refuses body before reading it', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42074' }
}, async ({ playwright, server }) => {
Expand Down
Loading