Our blog

 

Zend Framework More Detailed Stack Trace

If like me you kicked off your Zend Framework experience with the Quick Start and have kept the error controller system of the Quick Start for your other ZF projects, you might find this little snippet useful.

The Quick Start uses the Exception::getTraceAsString() method to give you the stack trace. Unfortunately this function seems to snip off a bit of useful info.. almost tantalising you with the info but not quite giving you enough to actually know what is going wrong!

To get a more detailed stack trace, I recommend changing this and using the Exception::getTrace() function which returns and array of the trace. You can then echo out all of this info in full.

For me (as I have xdebug for my dev environment, which is the only place you should be dumping stack traces anyway) I use the following chunk of code in my views/scripts/error/error.phtml

PHP:
  1. <? // application/views/scripts/error/error.phtml ?>
  2.  
  3. <h1>An error occurred</h1>
  4. <h2><?= $this->message ?></h2>
  5.  
  6. <? if ('production' != $this->env): ?>
  7.     <h3>Exception information:</h3>
  8.     <p>
  9.         <b>Message:</b> <?= $this->exception->getMessage() ?>
  10.     </p>
  11.     <h3>Request Parameters:</h3>
  12.     <pre><? var_dump($this->request->getParams()) ?></pre>
  13.  
  14.     <h3>Stack Trace 2:</h3>
  15.     <?php
  16.     foreach($this->exception->getTrace() as $t){
  17.         var_dump($t);
  18.     }
  19.     ?>
  20. <? endif ?>

More Reading:

3 Comments

[...] I experimented with echoing out each trace as detailed in this post, however I found that this caused a fatal error due to some kind of recursion so I have commented [...]

 

jesper
August 5th, 2009

I agree that the default stack trace is quite confusing :)

Where do I change the Exception stack trace thing to make it use the view script you have made?

thanks in advance

 

admin
August 11th, 2009

if you used the quick start as the basis of your zend framework project layout, you should find this file:

application/views/scripts/error/error.phtml

 

 

Leave a Reply