gajus / puppeteer-proxy

Proxies Puppeteer Page requests.
Other
197 stars 24 forks source link

Request is already handled #22

Closed ethyaan closed 2 years ago

ethyaan commented 2 years ago

Hi, I'm using below peace of code but I'm getting this error.

await page.setRequestInterception(true);

              page.on("request", async (request) => {
                await proxyRequest({
                  page,
                  proxyUrl: proxySerevr,
                  request,
                });
              });

Error is

(node:91639) UnhandledPromiseRejectionWarning: Error: Request is already handled!
    at assert (/Users/ehsan/Work/Wesrom/leadshark-scrapper/node_modules/puppeteer/src/common/assert.ts:23:21)
    at HTTPRequest.abort (/Users/ehsan/Work/Wesrom/leadshark-scrapper/node_modules/puppeteer/src/common/HTTPRequest.ts:574:11)
    at proxyRequest (/Users/ehsan/Work/Wesrom/leadshark-scrapper/node_modules/puppeteer-proxy/src/routines/proxyRequest.js:147:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async proxyRequest0 (/Users/ehsan/Work/Wesrom/leadshark-scrapper/node_modules/puppeteer-proxy/src/routines/proxyRequest.js:165:5)
    at async /Users/ehsan/Work/Wesrom/leadshark-scrapper/src/app.module.ts:92:17
    at async HTTPRequest.finalizeInterceptions (/Users/ehsan/Work/Wesrom/leadshark-scrapper/node_modules/puppeteer/src/common/HTTPRequest.ts:240:5)
(node:91639) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 118)
gajus commented 2 years ago

As the error says, the request has been handled already, presumably by another plugin that you are using. Not a bug.

ethyaan commented 2 years ago

Thank you but your answer didn't help me. I used below snippet to solve my problem and it works. for everyone who wants to know more about interception, first please upgrade to v13.0.1 or above second read this documentation page.setRequestInterception

  page.on('request', async (interceptedRequest) => {
    if (interceptedRequest.interceptResolutionState().action === "already-handled") return;
    await proxyRequest({
      page,
      proxyUrl: proxySerevr.connection,
      request: interceptedRequest,
    });
    if (interceptedRequest.interceptResolutionState().action === "already-handled") return;
    interceptedRequest.continue();
  });