Reputation: 79
I want to open a url and collect its httpcode and error, but I can't get its httpcode in goto().catch.
let result = {};
page.goto(url)
.then(res => {
result['http-code'] = res.status();
})
.catch(err => {
result['http-code'] = '?'; // TODO: get http-code in goto().catch
result['page-error'] = err + '';
})
.then(() => {
res.json(result);
});
Upvotes: 3
Views: 3180
Reputation: 101
Just in case samobody is looking for a way to navigate to a given page and get a status code (or any other information from the object HTTPResponse
)
puppeteer.launch( puppeteerOptions ).then(async browser => {
const page = await browser.newPage();
const url = "http://google.com";
const [rsp] = await Promise.all([
page.waitForNavigation(),
page.goto(url)
]);
console.log( 200 == rsp.status()
? "Status OK"
: "Status NOT ok" );
// ...
});
The first call, to page.waitForNavigation()
returns promise which resolves after the navigation and returns the HTTPResponse
object. The second call, page.goto()
navigates to the given URL.
Promise.all()
waits till both promises are resolved and returns an array with results. In this case we are interestedy only in result of the first call, which is stored in rsp
.
Upvotes: 3
Reputation: 8682
This is impossible.
TL;DR
Because the page.goto 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 response.status()
;
page.goto will throw an error if:
NOTE page.goto either throws an error or returns a main resource response. The only exceptions are navigation to about:blank or navigation to the same URL with a different hash, which would succeed and return null.
Upvotes: 4