onError
Introduced: PhantomJS 1.5
This callback is invoked when there is a JavaScript execution error not caught by a page.onError
handler. This is the closest it gets to having a global error handler in PhantomJS, and so it is a best practice to set this onError
handler up in order to catch any unexpected problems. The arguments passed to the callback are the error message and the stack trace [as an Array].
Examples
phantom.onError = function(msg, trace) {
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.log(msgStack.join('\n'));
phantom.exit(1);
};