Setting up a global PhantomJS error handler
This recipe introduces the onError
callback and demonstrates how we can use it to catch and handle errors in the PhantomJS runtime. As this onError
callback is attached to the phantom
object, we can use it to handle errors that are not otherwise handled by try-catch
statements in our PhantomJS scripts or by onError
handlers attached to webpage
objects.
Getting ready
To run this recipe, we will need a script that we believe has a tendency to fail.
The script in this recipe is available in the downloadable code repository as recipe04.js
under chapter02
. If we run the provided example script, we must change to the root directory for the book's sample code.
How to do it…
Consider the following script:
phantom.onError = function(message, trace) { console.error('[PHANTOMJS ERROR] ' + message); trace.forEach(function(t) { console.error(' >> [' + t.line + '] ' + (t.function ? '[' + t.function...