Detect non-2XX response from website
Thanks for writing this great plugin. Is there any way to detect/retry errors from non-2XX responses in the screenshots?
For example:
I am using:
await captureWebsite.file(url, filename, options)
Also, unrelated, but setting darkMode: true doesn't seem to work for https://news.google.com. I didn't dig into the code to find out how you're setting this, but I've used { 'prefers-color-scheme': 'dark' } on CDP (with EmulationMedia) to get that to render properly.
Also, unrelated, but setting darkMode: true doesn't seem to work for https://news.google.com. I didn't dig into the code to find out how you're setting this, but I've used { 'prefers-color-scheme': 'dark' } on CDP (with EmulationMedia) to get that to render properly.
That's what I'm doing and it works on other websites: https://github.com/sindresorhus/capture-website/blob/808757d9d9840a92c2c98f6b261595efbff8342b/index.js#L229-L232
It's possible to detect the status code. I haven't made up my mind whether it makes sense to throw on non-200 response by default or not.
page.goto 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().
https://pptr.dev/#?product=Puppeteer&version=v10.0.0&show=api-pagegotourl-options
Thanks for your suggestions and getting back to me.
That's what I'm doing and it works on other websites:
Interestingly, it worked for me too (including on your website), just not on Google News for some reason. I'll experiment a bit more and let you know if I discover any reason for it.
It's possible to detect the status code. I haven't made up my mind whether it makes sense to throw on non-200 response by default or not.
If you wanted to keep it simple, what about a certain number of retries (e.g., 3) on non-2XX response? Or, even an option to throw errors on non-2XX response (false by default) -- or finally -- what about returning this code as the ultimate returned response from the promise? I think it makes sense - especially in the context of website screenshots - to detect the response code.
You could add retried, I think it would be even nicer to return the status code and leave it up to the programmer to decide what to do (in my case, a non-200 indicates a serious failure, and I don't want to take a screenshot, I want to know that it failed and throw away the screenshot). - Also thanks for the fantastic module!