How do I handle page errors (e.g., 404, 500) in Puppeteer?
Benjamin C
benjamin c profile pic

Handling page errors, such as HTTP 404 or 500 errors, in Puppeteer involves capturing and processing error responses using thepage.on('response') andpage.on('requestfailed') events. Here's a detailed explanation of how to handle page errors in Puppeteer: 1. Launching a new browser instance and creating a new page:

1
2
3
4
5
6
7
8
9
10
11
12
13

   const puppeteer = require('puppeteer');

   (async () => {
     const browser = await puppeteer.launch();
     const page = await browser.newPage();

     // Perform actions with the page here

     // Close the browser
     await browser.close();
   })();
   

This code sets up a basic Puppeteer script. It launches a new headless browser instance and creates a new page to work with. 2. Handling response errors usingpage.on('response'): To handle errors for responses received from the server, you can listen to theresponse event and check the status code of the response.

1
2
3
4
5
6
7
8

   page.on('response', (response) => {
     const status = response.status();
     if (status >= 400) {
       console.log(`Error: ${status} ${response.url()}`);
     }
   });
   

In this example, theresponse event is handled usingpage.on('response'). The response status code is obtained usingresponse.status(), and if the status code is 400 or higher, an error message is logged with the status code and URL of the response. 3. Handling failed requests usingpage.on('requestfailed'): If a request fails due to network issues, you can listen to therequestfailed event and handle the error accordingly.

1
2
3
4
5

   page.on('requestfailed', (request) => {
     console.log(`Request failed: ${request.url()}`);
   });
   

In this code snippet, therequestfailed event is handled usingpage.on('requestfailed'). When a request fails, an error message is logged along with the URL of the failed request. 4. Optionally, intercepting requests to handle errors manually: Puppeteer also provides the ability to intercept requests and handle errors manually. You can usepage.setRequestInterception(true) to enable request interception and then customize the response or take appropriate actions based on the intercepted requests.

1
2
3
4
5
6
7
8
9

   page.setRequestInterception(true);

   page.on('request', (request) => {
     // Handle the request interception
     // Modify the response or take appropriate actions
     // Continue or abort the request
   });
   

This code snippet enables request interception usingpage.setRequestInterception(true). Therequest event is handled usingpage.on('request'), where you can customize the response or take any necessary actions based on the intercepted requests. By following these steps, you can handle page errors in Puppeteer. By listening to theresponse event and checking the status codes, and therequestfailed event for failed requests, you can capture and process errors that occur during page navigation or resource loading. Additionally, by intercepting requests, you have more control over handling errors and can modify responses or take appropriate actions as needed.