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
7 changes: 4 additions & 3 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, parseSystemErrorFields, serializeSystemErrorFields, serializeValue, systemErrorMessage } from '@protocol/serializers';
import { parseSerializedValue, serializeValue, systemErrorCode, 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: 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 })) };
}

Expand All @@ -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;
}
7 changes: 4 additions & 3 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, parseSystemErrorFields, serializeSystemErrorFields, serializeValue, systemErrorMessage } from '@protocol/serializers';
import { parseSerializedValue, serializeValue, systemErrorCode, systemErrorMessage } from '@protocol/serializers';
import { rewriteErrorMessage } from '@utils/stackTrace';

import { isProtocolError } from './protocolError';
Expand Down Expand Up @@ -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 })) };
}

Expand All @@ -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;
}
7 changes: 1 addition & 6 deletions packages/protocol/spec/serialized.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,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?
31 changes: 4 additions & 27 deletions packages/protocol/src/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,11 @@
* limitations under the License.
*/

import type { SerializedError, SerializedValue } from '@protocol/structs';
import type { 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];
}
// 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 {
Expand Down
5 changes: 0 additions & 5 deletions packages/protocol/src/structs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,6 @@ export type SerializedError = {
name: string,
stack?: string,
code?: string,
errno?: number,
syscall?: string,
address?: string,
port?: number,
hostname?: string,
},
value?: SerializedValue,
};
Expand Down
5 changes: 0 additions & 5 deletions packages/protocol/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3066,11 +3066,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')),
});
Expand Down
6 changes: 3 additions & 3 deletions tests/library/global-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -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();
});

Expand Down
Loading