title: How do I capture JavaScript errors generated in a page fetched by PhantomJS?
body: <p>I have a PhantomJS script that loads a local HTML file, injects some javascript files, then executes some javascript in the context of the page.  The javascript that runs generates an exception, but I only get output from the console, which doesn't seem to distinguish between an error and a normal log and doesn't have file, line numbers or a stacktrace.</p>
  
  <p>What I need is a way to capture or otherwise distinguish these errors.  I have already tried:</p>
  
  <ul>
  <li>Wrapping my PhantomJS script in a try-catch
  <ul>
  <li>Result:  nothing is thrown far enough to be caught by this</li>
  </ul></li>
  <li>Define a <code>window.onerror</code> function
  <ul>
  <li>Result: nothing happens.  WebKit does not implement an onerror event on the window</li>
  </ul></li>
  </ul>
  
  <p>I would prefer to be able to retrieve the error object itself so that I can retrieve the stacktrace.</p>