Skip to content

Commit

Permalink
cherry-pick(release-1.18): fix: do not mute route handler exceptions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Jan 24, 2022
1 parent 2717e61 commit 7dcd171
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 14 deletions.
12 changes: 8 additions & 4 deletions packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
_onRoute(route: network.Route, request: network.Request) {
for (const routeHandler of this._routes) {
if (routeHandler.matches(request.url())) {
if (routeHandler.handle(route, request)) {
this._routes.splice(this._routes.indexOf(routeHandler), 1);
if (!this._routes.length)
this._wrapApiCall(() => this._disableInterception(), true).catch(() => {});
try {
routeHandler.handle(route, request);
} finally {
if (!routeHandler.isActive()) {
this._routes.splice(this._routes.indexOf(routeHandler), 1);
if (!this._routes.length)
this._wrapApiCall(() => this._disableInterception(), true).catch(() => {});
}
}
return;
}
Expand Down
13 changes: 7 additions & 6 deletions packages/playwright-core/src/client/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,13 @@ export class RouteHandler {
return urlMatches(this._baseURL, requestURL, this.url);
}

public handle(route: Route, request: Request): boolean {
try {
this.handler(route, request);
} finally {
return ++this.handledCount >= this._times;
}
public handle(route: Route, request: Request): void {
++this.handledCount;
this.handler(route, request);
}

public isActive(): boolean {
return this.handledCount < this._times;
}
}

Expand Down
12 changes: 8 additions & 4 deletions packages/playwright-core/src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,14 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
private _onRoute(route: Route, request: Request) {
for (const routeHandler of this._routes) {
if (routeHandler.matches(request.url())) {
if (routeHandler.handle(route, request)) {
this._routes.splice(this._routes.indexOf(routeHandler), 1);
if (!this._routes.length)
this._wrapApiCall(() => this._disableInterception(), true).catch(() => {});
try {
routeHandler.handle(route, request);
} finally {
if (!routeHandler.isActive()) {
this._routes.splice(this._routes.indexOf(routeHandler), 1);
if (!this._routes.length)
this._wrapApiCall(() => this._disableInterception(), true).catch(() => {});
}
}
return;
}
Expand Down
85 changes: 85 additions & 0 deletions tests/playwright-test/playwright.unhandled.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { test, expect } from './playwright-test-fixtures';

test('should lead in uncaughtException when page.route raises', async ({ runInlineTest, server }) => {
const result = await runInlineTest({
'a.test.ts': `
const { test } = pwt;
test('fail', async ({ page }) => {
test.fail();
await page.route('**/empty.html', route => {
throw new Error('foobar');
});
await page.goto('${server.EMPTY_PAGE}');
});
`,
}, { workers: 1 });
expect(result.skipped).toBe(1);
expect(result.output).toContain('foobar');
});

test('should lead in unhandledRejection when page.route raises', async ({ runInlineTest, server }) => {
const result = await runInlineTest({
'a.test.ts': `
const { test } = pwt;
test('fail', async ({ page }) => {
test.fail();
await page.route('**/empty.html', async route => {
throw new Error('foobar');
});
await page.goto('${server.EMPTY_PAGE}');
});
`,
}, { workers: 1 });
expect(result.skipped).toBe(1);
expect(result.output).toContain('foobar');
});

test('should lead in uncaughtException when context.route raises', async ({ runInlineTest, server }) => {
const result = await runInlineTest({
'a.test.ts': `
const { test } = pwt;
test('fail', async ({ context, page }) => {
test.fail();
await context.route('**/empty.html', route => {
throw new Error('foobar');
});
await page.goto('${server.EMPTY_PAGE}');
});
`,
}, { workers: 1 });
expect(result.skipped).toBe(1);
expect(result.output).toContain('foobar');
});

test('should lead in unhandledRejection when context.route raises', async ({ runInlineTest, server }) => {
const result = await runInlineTest({
'a.test.ts': `
const { test } = pwt;
test('fail', async ({ context, page }) => {
test.fail();
await context.route('**/empty.html', async route => {
throw new Error('foobar');
});
await page.goto('${server.EMPTY_PAGE}');
});
`,
}, { workers: 1 });
expect(result.skipped).toBe(1);
expect(result.output).toContain('foobar');
});

0 comments on commit 7dcd171

Please sign in to comment.