Hieu Nguyen
Hieu Nguyen

Reputation: 263

PHP 7 Xdebug in VSCODE: how to ignore exceptions

I'm using plugin PHP Debugger in Visual Studio Code to debug PHP codes. All were ok with PHP <= 5.6. The debugger worked like a charm. But when I try to debug the code with PHP 7, it does not work as expected. It does not jump to the break points I added, I was stuck at thounsand excepton like

Exception has occurred. Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; RecurringType has a deprecated constructor

Or

Exception has occurred. Warning: Declaration of AntlrLexer::matchAny() should be compatible with BaseRecognizer::matchAny($input)

Or

Exception has occurred. Notice: Trying to get property of non-object

These exception are generated by the Framework so I can not fix them all, I just want to skip all these exceptions and jump to my break points only.

How can I achieve that? Thanks.

Upvotes: 24

Views: 8494

Answers (3)

Rafalfaro
Rafalfaro

Reputation: 221

In my case the extension or editor came with the EVERYTHING option checked by default. Unchecking EVERYTHING checkbox fixed the issue.

Upvotes: 5

MQuiggGeorgia
MQuiggGeorgia

Reputation: 759

I don't know if this will work with your configuration (I'm using PHP 7.2 with WordPress), and have found this works to keep XDebug from triggering every Exception, Warning, Notice, etc. if I just want to step through my own sections of code.

First, uncheck everything (including Everything, if checked) in the Breakpoints debug section:

XDebug Breakpoints

Then hover over the Breakpoints bar and click the +:

Breakpoint bar - Add Function

Now add the name of a function you want to debug:

Add breakpoint function

Finally, set a breakpoint within that function. You will now be able to trigger an XDebug step-through only within that function:

Stepping through an XDebug session without triggering every exception

You can, presumably, add as many of your own function breakpoints as you want. An interesting aside is that if you have multiple functions with the same name, you only have to add the function name once.

Upvotes: 39

Jeremy Harris
Jeremy Harris

Reputation: 24549

Problem

It sounds like the framework you are using was written with a pre PHP 7 version in mind. Error handling has changed and now an unhandled exception or Throwable instance, to be more accurate, will bubble up to the exception handling function defined with the set_exception_handler() function. The bigger problem is a FATAL ERROR means bang bang, process is dead and nothing for the debugger to bind to.

PHP 7 changes how most errors are reported by PHP. Instead of reporting errors through the traditional error reporting mechanism used by PHP 5, most errors are now reported by throwing Error exceptions.

As with normal exceptions, these Error exceptions will bubble up until they reach the first matching catch block. If there are no matching blocks, then any default exception handler installed with set_exception_handler() will be called, and if there is no default exception handler, then the exception will be converted to a fatal error and will be handled like a traditional error.

As the Error hierarchy does not inherit from Exception, code that uses catch (Exception $e) { ... } blocks to handle uncaught exceptions in PHP 5 will find that these Errors are not caught by these blocks. Either a catch (Error $e) { ... } block or a set_exception_handler() handler is required>.


Solution

So how can you skip over all these errors (which are being converted to fatal errors and stopping execution) and allow it to reach your breakpoint? By defining an error handler with set_exception_handler().

Inject something like this in your framework bootstrapping code. Be sure to look for anywhere that the framework might override this.

function ignore_exceptions_handler($exception) {
   // Do nothing here
}

set_exception_handler('ignore_exceptions_handler');

Reference: http://php.net/manual/en/function.set-exception-handler.php


More Details

So why does this happen? It has nothing to do with Visual Studio or the debugger. PHP 7 is simply enforcing errors more explicitly than previous versions (i.e <= 5.6). The framework you are using has code not supported by PHP 7 and the errors are not being handled.

Here is something else interesting: https://docs.devsense.com/en/debugging/exceptions

Upvotes: 4

Related Questions