rox rox - 21 days ago 10
Javascript Question

Why casperjs aborted silently with invalid javascript code?

This is what I've tried:

[roxma@localhost test]$ cat invalid.js
"use strict";

var casper = require('casper').create({});

// missing ;
var aa

casper.run();
[roxma@localhost test]$ casperjs invalid.js
[roxma@localhost test]$


I have no idea why caspers aborted silently. I would expected some hint on the error.

casperjs --version
1.1.3
phantomjs -v
2.1.1


=====================

Edit



Sorry, my mistake. The previous example is not an error.

The following example is closer to the situation I encountered.

"use strict";
var casper = require('casper').create({});

casper.start('http://www.qq.com/');

casper.then(function() {
this.echo('begin');
var a;
var b = {
a: a['d']
};
this.echo('end');
});

casper.run();

[roxma@localhost test]$ casperjs invalid.js
begin
[roxma@localhost test]$

Answer

You need to use casper.on('error') callback, in order to catch the error, like so:

"use strict";
var casper = require('casper').create({
//    verbose: true,
//    logLevel: 'debug'
});
//casper.on('remote.message', function(msg) {this.echo('The error from evaluate: ' + msg);});
casper.on('error', function(msg) {
    this.echo('Error: ' + msg);
})

casper.start('http://www.qq.com/',function() {
        this.echo('begin');
        var a;
        var b = { a: a['d'] };
        this.echo('end');
});
casper.run();