Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhantomJS error handling

I'm having a hard time understanding how PhantomJS handles errors.

I have a locally installed Apache server running (xampp), and when I manually visit "http://localhost/" I get the "It Works!" page.

As a test, I wrote a small file (called forceError.js) that purposely causes an unchecked exception:

var page = require('webpage').create(),
    url = 'http://localhost/';

page.onError = function(msg, trace) {
  console.log("page.onError");
  var msgStack = ['ERROR: ' + msg];
  if (trace && trace.length) {
    msgStack.push('TRACE:');
    trace.forEach(function(t) {
      msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
    });
  }
  console.error(msgStack.join('\n'));
};

phantom.onError = function(msg, trace) {
  console.log("phantom.onError");
  var msgStack = ['PHANTOM ERROR: ' + msg];
  if (trace && trace.length) {
    msgStack.push('TRACE:');
    trace.forEach(function(t) {
      msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
    });
  }
  console.error(msgStack.join('\n'));
  phantom.exit(1);
};

page.open(url, function (status) {
    console.log("status: " + status);

    // an undefined function
    thisShouldForceAnError();
});

When I run this using:

phantomjs.exe forceError.js

First I get "status: success" and then the process just hangs. I don't see either page.onError or phantom.onError being invoked.

Is there some property or something I need to turn on to get general error handling?

I'm on Windows 7, PhantomJS version 2.0.0, and running this in my "git bash" shell.

like image 471
Jonathan.Brink Avatar asked Jul 09 '15 15:07

Jonathan.Brink


1 Answers

Your app hangs because it looks there is a loop when you call console.error within phantom.onError. Check this: Phantomjs v2, consume huge memory+cpu, after throwing exception.

like image 121
maxi-code Avatar answered Oct 24 '22 12:10

maxi-code