Capturing page script errors

Errors that are generated within the scripts that are coded within the page.evaluate method will not trigger phantom.onError; however, the webpage object's onError callback will receive the event instead. The definition of the function is the same, except that we define our handling on the webpage instance as shown in the following code:

var page = require('webpage').create();
page.onError = function(msg, trace) {
  console.log(msg);
  if (trace) {
    trace.forEach(function(t) {
      var stackmsg = '  at' + (t.function ? ' function ' + t.function : '') + ' (' + (t.file || t.sourceURL) + ':' + t.line + ')';
      console.log(stackmsg);
    });
  }
};

Assume that we have the following code in our webpage evaluate block:

page.open(url, function(status) ...

Get Getting Started with PhantomJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.