Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony ExceptionHandler throwing an error about itself

I'm working on an upgrade of an application to use Symfony 3 and PHP 7. I keep getting this error. I'm not sure where it stems from or how to fix it.

Symfony\Component\Debug\ExceptionHandler::handle() must be an instance of Exception

like image 273
Jeremy Avatar asked Feb 09 '23 03:02

Jeremy


1 Answers

TL;DR: A PHP error is triggered somewhere in your code. In PHP 5, it was triggered as a simple PHP error; in PHP 7 it's thrown as an Error that is now passed into Symfony's exception handler.

This error has probably always been in your application and was swallowed due to error_reporting or display_errors settings.


The error handling mechanism changed in PHP 7. Errors are now thrown as instances of the Error class and can be caught by an exception handler. See the documentation for more information:

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.

However, note that the Error class does not extend the Exception class (they both implement the Throwable interface, though).

Since the Symfony exception handler was registered as an exception handler using the set_exception_handler method, this handler will be invoked with all kinds of uncaught Error instances thrown around in your code, but cannot handle them as it expects an Exception per its type hint.

For the time being, you could solve this by implementing your own ExceptionHandler (that you can then use instead of Symfony's) that implements a handle(Throwable $error) function, allowing you to also catch PHP Errors.

like image 52
helmbert Avatar answered Feb 11 '23 01:02

helmbert