Skip to content

Commit

Permalink
feat: rename PageError to WebError (#26913)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman committed Sep 6, 2023
1 parent 9105a20 commit 361038c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 76 deletions.
8 changes: 4 additions & 4 deletions docs/src/api/class-browsercontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ Use [`method: Page.waitForLoadState`] to wait until the page gets to a particula
cases).
:::

## event: BrowserContext.pageError
## event: BrowserContext.webError
* since: v1.38
- argument: <[PageError]>
- argument: <[WebError]>

Emitted when unhandled exceptions occur on any pages created through this
context. To only listen for `pageError` events from a particular page, use [`event: Page.pageError`].
Emitted when exception is unhandled in any of the pages in this
context. To listen for errors from a particular page, use [`event: Page.pageError`] instead.

## event: BrowserContext.request
* since: v1.12
Expand Down
27 changes: 13 additions & 14 deletions docs/src/api/class-pageerror.md → docs/src/api/class-weberror.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# class: PageError
# class: WebError
* since: v1.38

[PageError] class represents objects created by context when there are unhandled
execeptions thrown on the pages and dispatched via the [`event: BrowserContext.pageError`] event.
[WebError] class represents an unhandled exeception thrown in the page. It is dispatched via the [`event: BrowserContext.webError`] event.

```js
// Log all uncaught errors to the terminal
context.on('pageerror', pageerror => {
console.log(`Uncaught exception: "${pageerror.error()}"`);
context.on('weberror', webError => {
console.log(`Uncaught exception: "${webError.error()}"`);
});

// Navigate to a page with an exception.
Expand All @@ -16,8 +15,8 @@ await page.goto('data:text/html,<script>throw new Error("Test")</script>');

```java
// Log all uncaught errors to the terminal
context.onPageError(pagerror -> {
System.out.println("Uncaught exception: " + pagerror.error());
context.onWebError(webError -> {
System.out.println("Uncaught exception: " + webError.error());
});

// Navigate to a page with an exception.
Expand All @@ -26,41 +25,41 @@ page.navigate("data:text/html,<script>throw new Error('Test')</script>");

```python async
# Log all uncaught errors to the terminal
context.on("pageerror", lambda pageerror: print(f"uncaught exception: {pageerror.error}"))
context.on("weberror", lambda web_error: print(f"uncaught exception: {web_error.error}"))

# Navigate to a page with an exception.
await page.goto("data:text/html,<script>throw new Error('test')</script>")
```

```python sync
# Log all uncaught errors to the terminal
context.on("pageerror", lambda pageerror: print(f"uncaught exception: {pageerror.error}"))
context.on("weberror", lambda web_error: print(f"uncaught exception: {web_error.error}"))

# Navigate to a page with an exception.
page.goto("data:text/html,<script>throw new Error('test')</script>")
```

```csharp
// Log all uncaught errors to the terminal
context.PageError += (_, pageerror) =>
context.WebError += (_, webError) =>
{
Console.WriteLine("Uncaught exception: " + pageerror.Error);
Console.WriteLine("Uncaught exception: " + webError.Error);
};
```

## method: PageError.page
## method: WebError.page
* since: v1.38
- returns: <[null]|[Page]>

The page that produced this unhandled exception, if any.

## method: PageError.error
## method: WebError.error
* since: v1.38
- returns: <[Error]>

Unhandled error that was thrown.

## method: PageError.error
## method: WebError.error
* since: v1.38
* langs: java, csharp
- returns: <[string]>
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ export { Video } from './video';
export { Worker } from './worker';
export { CDPSession } from './cdpSession';
export { Playwright } from './playwright';
export { PageError } from './pageError';
export { WebError } from './webError';
4 changes: 2 additions & 2 deletions packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { rewriteErrorMessage } from '../utils/stackTrace';
import { HarRouter } from './harRouter';
import { ConsoleMessage } from './consoleMessage';
import { Dialog } from './dialog';
import { PageError } from './pageError';
import { WebError } from './webError';
import { parseError } from '../protocol/serializers';

export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel> implements api.BrowserContext {
Expand Down Expand Up @@ -105,7 +105,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
this._channel.on('pageError', ({ error, page }) => {
const pageObject = Page.from(page);
const parsedError = parseError(error);
this.emit(Events.BrowserContext.PageError, new PageError(pageObject, parsedError));
this.emit(Events.BrowserContext.WebError, new WebError(pageObject, parsedError));
if (pageObject)
pageObject.emit(Events.Page.PageError, parsedError);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/client/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const Events = {
Page: 'page',
// Can't use just 'error' due to node.js special treatment of error events.
// @see https://nodejs.org/api/events.html#events_error_events
PageError: 'pageerror',
WebError: 'weberror',
BackgroundPage: 'backgroundpage',
ServiceWorker: 'serviceworker',
Request: 'request',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type * as api from '../../types/types';
import type { Page } from './page';

export class PageError implements api.PageError {
export class WebError implements api.WebError {
private _page: Page | null;
private _error: Error;

Expand Down
91 changes: 43 additions & 48 deletions packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7625,11 +7625,10 @@ export interface BrowserContext {
on(event: 'page', listener: (page: Page) => void): this;

/**
* Emitted when unhandled exceptions occur on any pages created through this context. To only listen for `pageError`
* events from a particular page, use
* [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error).
* Emitted when exception is unhandled in any of the pages in this context. To listen for errors from a particular
* page, use [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error) instead.
*/
on(event: 'pageerror', listener: (pageError: PageError) => void): this;
on(event: 'weberror', listener: (webError: WebError) => void): this;

/**
* Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To
Expand Down Expand Up @@ -7704,7 +7703,7 @@ export interface BrowserContext {
/**
* Adds an event listener that will be automatically removed after it is triggered once. See `addListener` for more information about this event.
*/
once(event: 'pageerror', listener: (pageError: PageError) => void): this;
once(event: 'weberror', listener: (webError: WebError) => void): this;

/**
* Adds an event listener that will be automatically removed after it is triggered once. See `addListener` for more information about this event.
Expand Down Expand Up @@ -7818,11 +7817,10 @@ export interface BrowserContext {
addListener(event: 'page', listener: (page: Page) => void): this;

/**
* Emitted when unhandled exceptions occur on any pages created through this context. To only listen for `pageError`
* events from a particular page, use
* [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error).
* Emitted when exception is unhandled in any of the pages in this context. To listen for errors from a particular
* page, use [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error) instead.
*/
addListener(event: 'pageerror', listener: (pageError: PageError) => void): this;
addListener(event: 'weberror', listener: (webError: WebError) => void): this;

/**
* Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To
Expand Down Expand Up @@ -7897,7 +7895,7 @@ export interface BrowserContext {
/**
* Removes an event listener added by `on` or `addListener`.
*/
removeListener(event: 'pageerror', listener: (pageError: PageError) => void): this;
removeListener(event: 'weberror', listener: (webError: WebError) => void): this;

/**
* Removes an event listener added by `on` or `addListener`.
Expand Down Expand Up @@ -7952,7 +7950,7 @@ export interface BrowserContext {
/**
* Removes an event listener added by `on` or `addListener`.
*/
off(event: 'pageerror', listener: (pageError: PageError) => void): this;
off(event: 'weberror', listener: (webError: WebError) => void): this;

/**
* Removes an event listener added by `on` or `addListener`.
Expand Down Expand Up @@ -8066,11 +8064,10 @@ export interface BrowserContext {
prependListener(event: 'page', listener: (page: Page) => void): this;

/**
* Emitted when unhandled exceptions occur on any pages created through this context. To only listen for `pageError`
* events from a particular page, use
* [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error).
* Emitted when exception is unhandled in any of the pages in this context. To listen for errors from a particular
* page, use [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error) instead.
*/
prependListener(event: 'pageerror', listener: (pageError: PageError) => void): this;
prependListener(event: 'weberror', listener: (webError: WebError) => void): this;

/**
* Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To
Expand Down Expand Up @@ -8659,11 +8656,10 @@ export interface BrowserContext {
waitForEvent(event: 'page', optionsOrPredicate?: { predicate?: (page: Page) => boolean | Promise<boolean>, timeout?: number } | ((page: Page) => boolean | Promise<boolean>)): Promise<Page>;

/**
* Emitted when unhandled exceptions occur on any pages created through this context. To only listen for `pageError`
* events from a particular page, use
* [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error).
* Emitted when exception is unhandled in any of the pages in this context. To listen for errors from a particular
* page, use [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error) instead.
*/
waitForEvent(event: 'pageerror', optionsOrPredicate?: { predicate?: (pageError: PageError) => boolean | Promise<boolean>, timeout?: number } | ((pageError: PageError) => boolean | Promise<boolean>)): Promise<PageError>;
waitForEvent(event: 'weberror', optionsOrPredicate?: { predicate?: (webError: WebError) => boolean | Promise<boolean>, timeout?: number } | ((webError: WebError) => boolean | Promise<boolean>)): Promise<WebError>;

/**
* Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To
Expand Down Expand Up @@ -17960,35 +17956,6 @@ export interface Mouse {
wheel(deltaX: number, deltaY: number): Promise<void>;
}

/**
* {@link PageError} class represents objects created by context when there are unhandled execeptions thrown on the
* pages and dispatched via the
* [browserContext.on('pageerror')](https://playwright.dev/docs/api/class-browsercontext#browser-context-event-page-error)
* event.
*
* ```js
* // Log all uncaught errors to the terminal
* context.on('pageerror', pageerror => {
* console.log(`Uncaught exception: "${pageerror.error()}"`);
* });
*
* // Navigate to a page with an exception.
* await page.goto('data:text/html,<script>throw new Error("Test")</script>');
* ```
*
*/
export interface PageError {
/**
* Unhandled error that was thrown.
*/
error(): Error;

/**
* The page that produced this unhandled exception, if any.
*/
page(): null|Page;
}

/**
* This object can be used to launch or connect to Chromium, returning instances of {@link Browser}.
*/
Expand Down Expand Up @@ -19024,6 +18991,34 @@ export interface Video {
saveAs(path: string): Promise<void>;
}

/**
* {@link WebError} class represents an unhandled exeception thrown in the page. It is dispatched via the
* [browserContext.on('weberror')](https://playwright.dev/docs/api/class-browsercontext#browser-context-event-web-error)
* event.
*
* ```js
* // Log all uncaught errors to the terminal
* context.on('weberror', webError => {
* console.log(`Uncaught exception: "${webError.error()}"`);
* });
*
* // Navigate to a page with an exception.
* await page.goto('data:text/html,<script>throw new Error("Test")</script>');
* ```
*
*/
export interface WebError {
/**
* Unhandled error that was thrown.
*/
error(): Error;

/**
* The page that produced this unhandled exception, if any.
*/
page(): null|Page;
}

/**
* The {@link WebSocket} class represents websocket connections in the page.
*/
Expand Down
10 changes: 5 additions & 5 deletions tests/library/browsercontext-events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ test('dialog event should work with inline script tag', async ({ page, server })
await expect.poll(() => popup.evaluate('window.result')).toBe('hello');
});

test('pageError event should work', async ({ page }) => {
const [pageerror] = await Promise.all([
page.context().waitForEvent('pageerror'),
test('weberror event should work', async ({ page }) => {
const [webError] = await Promise.all([
page.context().waitForEvent('weberror'),
page.setContent('<script>throw new Error("boom")</script>'),
]);
expect(pageerror.page()).toBe(page);
expect(pageerror.error().stack).toContain('boom');
expect(webError.page()).toBe(page);
expect(webError.error().stack).toContain('boom');
});

0 comments on commit 361038c

Please sign in to comment.