fix: use AbortSignal.throwIfAborted (#10105)

This commit is contained in:
Alex Rudenko 2023-05-02 14:08:05 +02:00 committed by GitHub
parent a856f8ff76
commit 575f00a31d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 7 additions and 38 deletions

View File

@ -8,7 +8,6 @@ sidebar_label: API
| Class | Description |
| --------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [AbortError](./puppeteer.aborterror.md) | AbortError is emitted whenever certain operations are terminated due to an abort request. |
| [Accessibility](./puppeteer.accessibility.md) | The Accessibility class provides methods for inspecting the browser's accessibility tree. The accessibility tree is used by assistive technology such as [screen readers](https://en.wikipedia.org/wiki/Screen_reader) or [switches](https://en.wikipedia.org/wiki/Switch_access). |
| [Browser](./puppeteer.browser.md) | A Browser is created when Puppeteer connects to a browser instance, either through [PuppeteerNode.launch()](./puppeteer.puppeteernode.launch.md) or [Puppeteer.connect()](./puppeteer.puppeteer.connect.md). |
| [BrowserContext](./puppeteer.browsercontext.md) | BrowserContexts provide a way to operate multiple independent browser sessions. When a browser is launched, it has a single BrowserContext used by default. The method [Browser.newPage](./puppeteer.browser.newpage.md) creates a page in the default browser context. |

View File

@ -1,19 +0,0 @@
---
sidebar_label: AbortError
---
# AbortError class
AbortError is emitted whenever certain operations are terminated due to an abort request.
#### Signature:
```typescript
export declare class AbortError extends CustomError
```
**Extends:** [CustomError](./puppeteer.customerror.md)
## Remarks
Example operations are [page.waitForSelector](./puppeteer.page.waitforselector.md).

View File

@ -42,17 +42,6 @@ export class CustomError extends Error {
*/
export class TimeoutError extends CustomError {}
/**
* AbortError is emitted whenever certain operations are terminated due to
* an abort request.
*
* @remarks
* Example operations are {@link Page.waitForSelector | page.waitForSelector}.
*
* @public
*/
export class AbortError extends CustomError {}
/**
* ProtocolError is emitted whenever there is an error from the protocol.
*

View File

@ -20,7 +20,6 @@ import {assert} from '../util/assert.js';
import {isErrorLike} from '../util/ErrorLike.js';
import {interpolateFunction, stringifyFunction} from '../util/Function.js';
import {AbortError} from './Errors.js';
import type {Frame} from './Frame.js';
import {transposeIterableHandle} from './HandleIterator.js';
import type {WaitForSelectorOptions} from './IsolatedWorld.js';
@ -170,9 +169,7 @@ export class QueryHandler {
const {visible = false, hidden = false, timeout, signal} = options;
try {
if (signal?.aborted) {
throw new AbortError('QueryHander.waitFor has been aborted.');
}
signal?.throwIfAborted();
const handle = await frame.worlds[PUPPETEER_WORLD].waitForFunction(
async (PuppeteerUtil, query, selector, root, visible) => {
@ -203,7 +200,7 @@ export class QueryHandler {
if (signal?.aborted) {
await handle.dispose();
throw new AbortError('QueryHander.waitFor has been aborted.');
throw signal.reason;
}
if (!(handle instanceof ElementHandle)) {
@ -215,6 +212,9 @@ export class QueryHandler {
if (!isErrorLike(error)) {
throw error;
}
if (error.name === 'AbortError') {
throw error;
}
error.message = `Waiting for selector \`${selector}\` failed: ${error.message}`;
throw error;
} finally {

View File

@ -20,7 +20,7 @@ import type {Poller} from '../injected/Poller.js';
import {createDeferredPromise} from '../util/DeferredPromise.js';
import {stringifyFunction} from '../util/Function.js';
import {TimeoutError, AbortError} from './Errors.js';
import {TimeoutError} from './Errors.js';
import {IsolatedWorld} from './IsolatedWorld.js';
import {LazyArg} from './LazyArg.js';
import {HandleFor} from './types.js';
@ -66,7 +66,7 @@ export class WaitTask<T = unknown> {
this.#signal?.addEventListener(
'abort',
() => {
void this.terminate(new AbortError('WaitTask has been aborted.'));
void this.terminate(this.#signal?.reason);
},
{
once: true,