Sometimes, we will encounter unexpected problems that generate errors triggered by PhantomJS scripts or from the page itself. There are several ways to handle errors and exceptions in our script; we can use callbacks that are readily available in PhantomJS or through JavaScript exception handling. More often, we combine both to have a much better way of capturing errors.
PhantomJS scripts are not compiled, so we do expect that both runtime- and syntax-related errors could be thrown during script execution. These types of errors can be handled using the phantom
object's onError
callback.
If PhantomJS encounters a syntax error in the script, it will call the function defined for this callback; if there is no function defined for this callback, it will perform the default implementation and output the errors in the console as shown in the following screenshot:
PhantomJS will display the error message followed by a series of lines, which denotes the stack of the error call. It will also display the function or method with a source line number.
This information is also passed to any function that we've defined for the onError
callback. The onError
callback definition is as follows:
phantom.onError = function(msg, trace) { // code handling };
The onError
callback will accept two parameters as shown in the preceding code. The first parameter is the error message, which is in the string format. The code stack trace is the second parameter. This object is an array that contains the following attribute of each item:
Stack trace item attributes |
Description |
---|---|
|
The name of the function where the error originated. |
|
The name of the file or script. |
|
The source URL of the script. This is omitted if the file is present. |
|
The line number where the error occurred. |
Let's re-implement the onError
function and slightly modify the default error output.
phantom.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); }); } };
First, we display the actual error message as you can see in the preceding code. Then, we check whether or not the code stack trace is present by checking whether or not the trace parameter object can be converted to the true value. If we do have the stack trace object, we iterate it as an array of trace items. We will display the function, if present, and then the script file or the source URL, and then, finally, the line number associated with it.
Truthy and Falsy
JavaScript objects have an inherent Boolean value; this means that everything in JavaScript can be converted to either true
or false
. We might come across some of these comparisons and conversions all throughout the book and these few reminders might help a bit. All of the following values are converted to falsy or false:
false
0
null
undefined
NaN
""
All other objects are converted to truthy, including empty functions, empty arrays, empty objects, and string literals 0
and false
.
Running the script would give us a slightly different output from the PhantomJS default behavior.
The following code is based on the example script specified in the preceding screenshot:
var system = require('system'), var url = system.args[1]; phantom.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); }); } }; var page = require('webpage').create(); page.open(url); function say() { phantom.hello(); } say();
The script contains a method call to an unknown function, as seen in the preceding code. The hello
function is not present in PhantomJS's phantom
object. This will generate an error event, which will trigger the onError
callback.
We don't normally display the error stack trace in our application, but rather, suppress them from the screen and write the details to a logfile, which we will do later on.
3.138.105.124