Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect non-2XX response from website #89

Open
dxdc opened this issue Jun 22, 2021 · 4 comments
Open

Detect non-2XX response from website #89

dxdc opened this issue Jun 22, 2021 · 4 comments

Comments

@dxdc
Copy link

dxdc commented Jun 22, 2021

Thanks for writing this great plugin. Is there any way to detect/retry errors from non-2XX responses in the screenshots?

For example:

Screen Shot 2021-06-22 at 4 13 48 PM

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.

@sindresorhus
Copy link
Owner

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:

capture-website/index.js

Lines 229 to 232 in 808757d

await page.emulateMediaFeatures([{
name: 'prefers-color-scheme',
value: options.darkMode ? 'dark' : 'light'
}]);

@sindresorhus
Copy link
Owner

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

@dxdc
Copy link
Author

dxdc commented Jun 25, 2021

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.

@vizall
Copy link

vizall commented Mar 14, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants