Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch and Handle CasperError

Tags:

casperjs

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)

like image 406
arcyqwerty Avatar asked Dec 09 '13 20:12

arcyqwerty


2 Answers

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.

like image 172
Christopher Ellis Avatar answered Sep 30 '22 06:09

Christopher Ellis


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)

like image 42
zziemke Avatar answered Sep 30 '22 06:09

zziemke