Skip to content

Commit

Permalink
fzaninotto#68 enhance error handling on rasterizer/phantomjs call fai…
Browse files Browse the repository at this point in the history
…lure
  • Loading branch information
slorber committed Dec 11, 2014
1 parent eb5aa54 commit 823c77f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
7 changes: 4 additions & 3 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ module.exports = function(app, useCors) {
var callRasterizer = function(rasterizerOptions, callback) {
request.get(rasterizerOptions, function(error, response, body) {
if (error || response.statusCode != 200) {
console.log('Error while requesting the rasterizer: %s', error.message);
var effectiveError = error ? error : new Error('Bad rasterizer response. StatusCode=['+response.statusCode+']. Body=['+body+']');
console.log('callRasterizer error:', effectiveError.message);
rasterizerService.restartService();
return callback(new Error(body));
return callback(effectiveError);
}
callback(null);
});
Expand Down Expand Up @@ -113,4 +114,4 @@ module.exports = function(app, useCors) {
});
}

};
};
31 changes: 27 additions & 4 deletions scripts/rasterizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,44 @@ service = server.listen(port, function(request, response) {
response.write('Error while parsing headers: ' + err.message);
return response.close();
}

// See http://newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs-page-load-fails/
page.onResourceError = function(resourceError) {
page.reason = resourceError.errorString;
page.reason_url = resourceError.url;
};

var pageOpenCallbackFired = false;
page.open(url, function(status) {
pageOpenCallbackFired = true;
if (status == 'success') {
window.setTimeout(function () {
page.render(path);
response.statusCode = 200;
response.write('Success: Screenshot saved to ' + path + "\n");
page.release();
response.close();
}, delay);
} else {
response.write('Error: Url returned status ' + status + "\n");
var message = 'PhantomJS could not open the WebPage ['+url+'] because: ' + page.reason;
console.log(message);
response.statusCode = 502;
response.write(message);
page.release();
response.close();
}
});
// must start the response now, or phantom closes the connection
response.statusCode = 200;
response.write('');

// 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);

});

0 comments on commit 823c77f

Please sign in to comment.