Victor Leal Victor Leal - 9 months ago 52
PHP Question

How Laravel handles PHP warnings?

I'm trying to connect to a LDAP server using Laravel. It is important to say that I'm using the PHP functions

instead of using a package to handle it.

The point is that when I provide wrong user and password, the
function gives to us a PHP warning. I'm OK with this warning and, as is in the documentation, the function returns false when the bind does not occur.

But, Laravel is throwing an exception when this warning is triggered. This is not an exception, Laravel should not throw an exception and I wouldn't like to handle this as an exception; I just have to build an
condition that will return a message to the user.

Does Laravel catch all warnings as an Exception?

Answer Source

This is the intended behavior for Laravel. Laravel will turn any error into an ErrorException instance. Here's the bootstrap() method inside the Illuminate/Foundation/Bootstrap/HandleExceptions.php class.

public function bootstrap(Application $app)
    $this->app = $app;


    set_error_handler([$this, 'handleError']);

    set_exception_handler([$this, 'handleException']);

    register_shutdown_function([$this, 'handleShutdown']);

    if (! $app->environment('testing')) {
        ini_set('display_errors', 'Off');

The error_reporting(-1); will set PHP to report all errors (read more here).

While this part of code:

set_error_handler([$this, 'handleError']);

Will set a custom error handler. If you check the handleError() method, it's pretty clear that Laravel will turn any error into an ErrorException instance.

public function handleError($level, $message, $file = '', $line = 0, $context = [])
    if (error_reporting() & $level) {
        throw new ErrorException($message, 0, $level, $file, $line);

Read more about user-defined error handler here.

Hope this clear things up. :)