Phantomjs - page.onError - is trace always an empty object?

The phantomjs documentation says the following:

Error Handling To easily detect an error on a web page, be it a syntax error or another exception, an onError handler has been added for the WebPage object. An example of such a handler is:

page.onError = function (msg, trace) { console.log(msg); trace.forEach(function(item) { console.log(' ', item.file, ':', item.line); }) } 

Now, if the page opens the site with some JavaScript exceptions, detailed information will be printed (including stack trace).

Well, I created some β€œbroken” pages (pages where javascript throws an exception) and I get an error that throws but nothing in the trace

Can anyone help?

Here is an example of a case that I had:

html:

 <!docType> <html><head></head> <body> <script src='broken.js'></script> </body> </html> 

script: broken.js

 1. // this script is broken at line 5. 2. // 3. var i=20; 4. 5. i = somethingThatDontExist 6. 7. // we will never be here... 8. 
+4
source share
2 answers

You probably missed something in your test settings, but everything is working fine. You may have forgotten that phantomJS calls are asynchronous.

Here are my test settings:

test.html

 <!docType> <html><head></head> <body> <script src='broken.js'></script> </body> </html> 

broken.js

 var i = 20; i = somethingThatDontExist; 

testError.js

 var page = require('webpage').create(); page.onError = function (msg, trace) { console.log(msg); trace.forEach(function(item) { console.log(' ', item.file, ':', item.line); }) } page.open('test.html',function(){phantom.exit()}); 
0
source

This is an old question, but still a problem (I have run into this recently). The OP probably used an older version.

A few things to check ...

1) The API for console.log has changed between phantoms 2.1.1 and 2.5.0. In 2.5.0 console.log only the first argument is printed (in 2.1.1 it will print everything).

To be safe, first create a string and register it as the only argument to make sure that this is not just a logging problem. those. console.log(concatenatedString) not console.log(var1, var2, var3,...)

Use JSON.stringify(object) if you want to combine your objects and arrays.

2) In my experience, phantomjs is not consistent with how it fixes errors in the context of the page.

For example, if catchblock does console.log('message', error) on the javascript page, the phantomjs page.onConsoleMessage page.onConsoleMessage will get a line for the message and the string "[object Object]" ... so any error data contained inside this object gets lost between page context and phantomjs.

( console.error may have different behavior than console.log , but I have not tested it.)

3) page.onError is currently broken into 2.5.0 phantoms. It is never called, instead errors are displayed in page.onConsoleMessage , but they skip the error object (see # 2).

Use phantomjs 2.1.1 if you really need to know if there is an error on the page.

See the error below for reproducing code that illustrates the differences between 2.1.1 and 2.5.0 when it comes to page.onError , page.onResourceError and page.onConsoleMessage .

https://github.com/ariya/phantomjs/issues/14776

0
source

All Articles