6

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?

3 Answers 3

5

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>';
1

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

xdebug_print_function_stack($exception);
1
  • 1
    This will print the current stack, not the exception stack. Mar 21, 2016 at 22:46
0

With newer versions of xdebug you can display caught (and uncaught) exceptions natively with:

xdebug.show_exception_trace = 1

integer xdebug.show_exception_trace = 0

When this setting is set to 1, Xdebug will show a stack trace whenever an Exception or Error is raised - even if this Exception or Error is actually caught.

Error 'exceptions' were introduced in PHP 7.

Or, if you only want to show errors (caught or uncaught), but not Exceptions, use this instead:

xdebug.show_error_trace = 1

integer xdebug.show_error_trace = 0

When this setting is set to 1, Xdebug will show a stack trace whenever an Error is raised - even if this Error is actually caught.

https://xdebug.org/docs/develop#show_error_trace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.