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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With