fzaninotto / screenshot-as-a-service

Website screenshot service powered by node.js and phantomjs
1.1k stars 243 forks source link

Add better error handling when calling rasterizer #68

Open slorber opened 9 years ago

slorber commented 9 years ago

When PhantomJS fails to download the web page (page.open), it actually returns a status code of 200 + a failure message in the body.

The PhantomJS service should rather return an error code 502 Bad Gateway. So that the screenshot serving service can display a better error message.

The page.open error message can be accessed by the page API: check http://newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs-page-load-fails/

slorber commented 9 years ago

@fzaninotto notice I added a PR for this

I don't really know the original reasons why you did write the statusCode=200 directly because PhantomJS drop the connections. I don't know that behavior but tried to fix as best as I can so that it is easier to debug.

I've delayed a bit this "direct writing" with a useful comment:

  // This has been delayed a bit on purpose because once you write the http status code, 
  // you can't change it in case of error :(
  // So we only start writing after 500ms so that the page.open has a time window 
  // to eventually trigger an error with the appropriate status code
  window.setTimeout(function () {
    // must start the response now, or phantom closes the connection
    if ( pageOpenCallbackFired ) {
      response.statusCode = 200;
      response.write('');
    }
  },500);