title: Silent errors in CasperJS
body: <p>I just started working with CasperJs, and I'm having difficulty debugging it because so many coding mistakes seem to cause the script to exit without providing an error message. When you use verbose mode, you get the messages you should get up until the offending line of code, and at that point it just quits.</p>
  
  <p>For example, if I execute the code:</p>
  
  <pre><code>var casper = require('casper').create({
      verbose: true,
      logLevel: "debug"
  });
  
  casper.start('https://www.google.com/#q=stackoverflow', function(){
  
  });
  casper.wait(2000, function(){
  });
  
  
  casper.then(function() {
      hrefAr = this.evaluate(getLinks);
      this.log(hrefAr.length + ' links found', 'info');
      this.exit();
  });
  
  casper.run(function() {
      this.exit();
  });
  
  function getLinks() {
      var links = document.querySelectorAll('a');
      return Array.prototype.map.call(links, function(e) {
          return e.getAttribute('href');
      });
  }
  </code></pre>
  
  <p>I get the following results:</p>
  
  <pre><code>...a bunch of info &amp; debug messages, followed by...
  [info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&amp;cad=h (HTTP 200)
  [info] [phantom] 89 links found
  [debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true
  [debug] [phantom] url changed to "about:blank"
  </code></pre>
  
  <p>If I add a log statement to the function getLinks...</p>
  
  <pre><code>...code as shown above...
  function getLinks() {
      this.log('getLinks ran', 'info');
      var links = document.querySelectorAll('a');
  ...code as shown above...
  </code></pre>
  
  <p>...this causes to script to fail, like so:</p>
  
  <pre><code>...the same info &amp; debug messages...
  [info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&amp;cad=h (HTTP 200)
  ...NO LOGS, ECHOS, OR RESULTS PAST THIS POINT, JUST THESE TWO CLOSING STATEMENTS...
  [debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true
  [debug] [phantom] url changed to "about:blank"
  </code></pre>
  
  <p>It doesn't tell you that anything has gone wrong, it just sends you back to blank and ends execution.</p>
  
  <p>Is there a way to get better error reporting? Or any error reporting?</p>
  
  <hr>
  
  <p>When I try to implement the workaround below using the following code:</p>
  
  <pre><code>var casper = require('casper').create({
      verbose: true,
      logLevel: "debug"
  });
  
  casper.start('https://www.google.com/#q=stackoverflow', function(){
  
  });
  casper.wait(2000, function(){
  });
  
  
  casper.then(function() {
      reportErrors(function() {
          hrefAr = this.evaluate(getLinks);
          this.log(hrefAr.length + ' links found', 'info');
          this.exit();
      });
  });
  
  casper.run(function() {
      this.exit();
  });
  
  function getLinks() {
  
          //this.log('getLinks ran', 'info');
          var links = document.querySelectorAll('a');
          return Array.prototype.map.call(links, function(e) {
              return e.getAttribute('href');
          });
  
  }
  
  function reportErrors(f) {
    var ret = null;
    try {
      ret = f();
    } catch (e) {
      casper.echo("ERROR: " + e);
      casper.exit();
    }
    return ret;
  }
  </code></pre>
  
  <p>I get...</p>
  
  <pre><code>...info &amp; debug messages shown above...
  [info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&amp;cad=h (HTTP 200)
  ERROR: TypeError: undefined is not a constructor (evaluating 'this.evaluate(getLinks)') 
  --THIS IS WHERE MY LINK COUNT WOULD BE REPORTED
  [debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true
  [debug] [phantom] url changed to "about:blank"
  </code></pre>