Skip to content

Commit

Permalink
fix: interception id not found error in route.continue
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s committed Jan 25, 2024
1 parent f741f08 commit 02ff0d0
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions packages/playwright-core/src/server/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -367,6 +351,15 @@ export class Route extends SdkObject {
}
}

async function catchDisallowedErrors(callback: () => Promise<void>) {
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<Buffer>;
Expand Down

0 comments on commit 02ff0d0

Please sign in to comment.