arcyqwerty
arcyqwerty

Reputation: 10675

Catch and Handle CasperError

Using CasperJS how do I catch and handle CasperError?

The default appears to continue execution of the program (which does nothing but propagates the error).

These errors are logged to the console/stdout but I don't seem to see a way (from the docs) to catch and handle these errors.

Example:

this.fillSelectors(selector, data);

May produce:

CasperError: Errors encountered while filling form: form not found

I know I can check to make sure everything exists before calling, but is there a way to catch after the fact? (this applies to many other operations like casper.click as well)

Upvotes: 5

Views: 5889

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 184955

This is the complete solution for who need it ^^

casper.on('error', function(msg, backtrace) {
    this.capture('/tmp/error.png');
    console.log('backtrace: ' + JSON.stringify(backtrace, null, 4));
    console.log('message: ' + JSON.stringify(msg, null, 4));
    console.log('check capture in /tmp/error.png');
    casper.exit(1);
})

Upvotes: 0

lkjdkljdksld
lkjdkljdksld

Reputation: 33

var casper = require('casper').create({
        onError: function(msg, backtrace) {
            this.capture('error.png');
            throw new ErrorFunc("fatal","error","filename",backtrace,msg);
        }
    });

This works pretty well. (See http://docs.casperjs.org/en/latest/modules/casper.html#index-1)

Upvotes: 1

Christopher Ellis
Christopher Ellis

Reputation: 1116

I use something currently like this:

casper.on('error', function(msg,backtrace) {
  this.capture('./out/error.png');
  throw new ErrorFunc("fatal","error","filename",backtrace,msg);
});

and then I have a custom function ErrorFunc to process array of any warnings or a fatal error.

If you have an unsuccessful click it should throw the casper.on('error'). So you can put custom code there for how you would like to handle the error.

Here's the documentation for Casper events.

Upvotes: 6

Related Questions