in puppeteer page.goto()
documentation it says
In headless shell, this method will not throw an error when any valid HTTP status code is returned by the remote server, including 404 “Not Found” and 500 “Internal Server Error”. The status code for such responses can be retrieved by calling HTTPResponse.status().
i am using headfull mode and was trying something as
import puppeteer from "puppeteer";
const nap = async () =>
await new Promise((resolve) => {
setTimeout(resolve, 3000);
});
const errHandler = async () => {
await nap();
console.log("err");
};
const okHandler = async () => {
await nap();
console.log("ok");
};
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on("response", async (response) => {
if (response.status === 404) {
await errHandler();
} else {
await okHandler();
}
});
await page.goto("/404", { waitUntil: "networkidle0" });
})();
but puppeteer/browser is stack on loading page then throws after 30 seconds
...
this.#timeoutError = new TimeoutError(opts.message);
^
TimeoutError: Navigation timeout of 30000 ms exceeded
...
how should page.goto() 404 http status code should be caught and handled? (a correction to the code above would be very helpful)