捕获并处理CasperError [英] Catch and Handle CasperError
问题描述
使用CasperJS如何捕获和处理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).
这些错误已记录到控制台/ stdout,但我似乎看不到(从文档中)捕获和处理这些错误的方法。
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.
示例:
this.fillSelectors(selector, data);
可能会产生:
CasperError:填写表单时遇到错误:未找到表单
CasperError: Errors encountered while filling form: form not found
我知道我可以检查以确保在调用之前所有内容都存在,但是有没有办法追捕事实呢? (这也适用于许多其他操作,例如 casper.click
)
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);
});
然后我有一个自定义函数 ErrorFunc
处理所有警告或致命错误的数组。
and then I have a custom function ErrorFunc
to process array of any warnings or a fatal error.
如果单击失败,则应抛出 casper.on('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.
这里是有关Casper事件的文档。
这篇关于捕获并处理CasperError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!