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)
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With