Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Print caught exception like Xdebug

I have a PHP function which I call when I caught an exception. It accepts that exception as optional parameter, so it can show it for debugging purposes:

public static function notFound($resource, \Exception $exception = null) {
    // [... normal error handling ...]

    if (DEBUG && $exception != null) {
        echo '<br><br><h2>Stacktrace:</h2>';
        print_r($exception);          
    }
    die();
}

What I would like is to display this exception the same way uncaught exceptions and warnings are displayed with xdebug (styled with CSS and HTML, possibly with a different color scheme). Like this (created by echo $exception->getTrace();):

enter image description here

If I use print_r($exception); I get this, which seems to contain at least some of the needed output for xdebug:

enter image description here

I have tried echo $exception->xdebug_message;, var_dump($exception);, and var_dump($exception->xdebug_message);, but none of it worked. The last one seems to be the closed to what I want, but I can't get it to work correctly:

enter image description here

Is there a way to use xdebug styling for caught exceptions?

like image 449
tim Avatar asked Apr 06 '15 12:04

tim


2 Answers

Answered here: https://stackoverflow.com/a/24768176/1286814.

In short, you just need to wrap xdebug_message inside a table:

echo '<table>'.$e->xdebug_message.'</table>';
like image 199
Joe Avatar answered Oct 20 '22 02:10

Joe


Ok, I found a solution in this list of all xdebug functions:

xdebug_print_function_stack($exception);
like image 29
tim Avatar answered Oct 20 '22 04:10

tim