diff --git a/packages/playwright-core/src/server/network.ts b/packages/playwright-core/src/server/network.ts index dc2fa2ea3c01d3..6738e0520ca27b 100644 --- a/packages/playwright-core/src/server/network.ts +++ b/packages/playwright-core/src/server/network.ts @@ -26,6 +26,7 @@ import type { HeadersArray, NameValue } from '../common/types'; import { APIRequestContext } from './fetch'; import type { NormalizedContinueOverrides } from './types'; import { BrowserContext } from './browserContext'; +import { isProtocolError } from './protocolError'; export function filterCookies(cookies: channels.NetworkCookie[], urls: string[]): channels.NetworkCookie[] { const parsedURLs = urls.map(s => new URL(s)); @@ -270,13 +271,7 @@ export class Route extends SdkObject { async abort(errorCode: string = 'failed') { this._startHandling(); this._request._context.emit(BrowserContext.Events.RequestAborted, this._request); - await Promise.race([ - this._delegate.abort(errorCode), - // If the request is already cancelled by the page before we handle the route, - // we'll receive loading failed event and will ignore route handling error. - this._request._waitForRequestFailure() - ]); - + await catchDisallowedErrors(() => this._delegate.abort(errorCode)); this._endHandling(); } @@ -304,17 +299,12 @@ export class Route extends SdkObject { const headers = [...(overrides.headers || [])]; this._maybeAddCorsHeaders(headers); this._request._context.emit(BrowserContext.Events.RequestFulfilled, this._request); - await Promise.race([ - this._delegate.fulfill({ - status: overrides.status || 200, - headers, - body, - isBase64, - }), - // If the request is already cancelled by the page before we handle the route, - // we'll receive loading failed event and will ignore route handling error. - this._request._waitForRequestFailure() - ]); + await catchDisallowedErrors(() => this._delegate.fulfill({ + status: overrides.status || 200, + headers, + body: body!, + isBase64, + })); this._endHandling(); } @@ -347,13 +337,7 @@ export class Route extends SdkObject { this._request._setOverrides(overrides); if (!overrides.isFallback) this._request._context.emit(BrowserContext.Events.RequestContinued, this._request); - await Promise.race([ - this._delegate.continue(this._request, overrides), - // If the request is already cancelled by the page before we handle the route, - // we'll receive loading failed event and will ignore route handling error. - this._request._waitForRequestFailure() - ]); - + await catchDisallowedErrors(() => this._delegate.continue(this._request, overrides)); this._endHandling(); } @@ -367,6 +351,15 @@ export class Route extends SdkObject { } } +async function catchDisallowedErrors(callback: () => Promise) { + try { + return await callback(); + } catch (e) { + if (isProtocolError(e) && e.message.includes('Invalid http status code or phrase')) + throw e; + } +} + export type RouteHandler = (route: Route, request: Request) => boolean; type GetResponseBodyCallback = () => Promise;