framework: BindingResolutionException with ExceptionHandler

It looks like the ExceptionHandler has a problem with PHP Fatal Errors. I simply get a blank page and this shows up in the log:

PHP Fatal error:  
Uncaught exception 'Illuminate\\Container\\BindingResolutionException' with message 'Target [Illuminate\\Contracts\\Debug\\ExceptionHandler] is not instantiable.' 
in vendor/laravel/framework/src/Illuminate/Container/Container.php:756

Stack trace:
#0 vendor/laravel/framework/src/Illuminate/Container/Container.php(652): Illuminate\\Container\\Container->build('Illuminate\\\\Cont...', Array)
#1 vendor/laravel/framework/src/Illuminate/Foundation/Application.php(496): Illuminate\\Container\\Container->make('Illuminate\\\\Cont...', Array)
#2 vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(142): Illuminate\\Foundation\\Application->make('Illuminate\\\\Cont...')
#3 vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(73): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->getExceptionHandler(
#4 in vendor/laravel/framework/src/Illuminate/Container/Container.php on line 756, referer: http://localhost/

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 43 (13 by maintainers)

Most upvoted comments

You’re doing it wrong.

@GrahamCampbell very nice, thank you.

Closing the issue with “You’re doing it wrong.” though – thats’s rather arrogant. When we cloned the l5 dev version, the IoC binding for Illuminate\Contracts\Debug\ExceptionHandler was missing from bootstrap/app.php.

If you’re not intimately familiar with a framework (or an updated version of a framework), the solution is not – at first glance – visible.

Thanks for posting your solution though. Abstracting out the displayer is a nice idea and I will do a similar approach.

As stated above @GrahamCampbell i don’t know why this hasn’t been re-opened, this is a pretty severe issue and obviously not fixed, there are several issues on this that when even closed people are still commenting they are engaging.

To my untrained eyes, this issue has been floating around for years.

Sorry if I’m being spam-my. I’m unsure where to post this.

PHPUnit 6.0.13 by Sebastian Bergmann and contributors.

Runtime: PHP 7.1.3 with Xdebug 2.5.0 Configuration: /home/rof/src/bitbucket.org/waypointbuilding_team/toto/phpunit.ci.xml

…E

Time: 10.05 seconds, Memory: 14.00MB

There was 1 error:

  1. App\Waypoint\Tests\Api\ClientAdmin\BomaClientCategoryApiTest::it_can_create_boma_client_categories PHPUnit\Framework\Exception: PHP Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php:873 Stack trace: #0 /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php(725): Illuminate\Container\Container->notInstantiable(‘Illuminate\Cont…’) #1 /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php(598): Illuminate\Container\Container->build(‘Illuminate\Cont…’) #2 /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php(567): Illuminate\Container\Container->resolve(‘Illuminate\Cont…’) #3 /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(708): Illuminate\Container\Container->m in /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 873 PHP Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php:873 Stack trace: #0 /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php(725): Illuminate\Container\Container->notInstantiable(‘Illuminate\Cont…’) #1 /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php(598): Illuminate\Container\Container->build(‘Illuminate\Cont…’) #2 /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php(567): Illuminate\Container\Container->resolve(‘Illuminate\Cont…’) #3 /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(708): Illuminate\Container\Container->m in /home/rof/src/bitbucket.org/waypointbuilding_team/toto/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 873

ERRORS! Tests: 12, Assertions: 237, Errors: 1.

@GrahamCampbell @taylorotwell I once again am strongly suggesting we re-open these issues, whatever is the particular problem people are having or if they’re “doing it wrong” is not the issue here. Look at everyones comments on their fixes and how there is barely any relation to the error.

The issue is the handler not properly finding/tracking/displaying what actually is going on. This is by far one of the most Achilles’ Heel’s types of bugs for any framework, and generally scares me that I have it running on production.

The solution is to upgrade your PHP to PHP 7 or later. I encountered this same issue and I resolved by upgrading to PHP 7.1

I encountered this issue when running phpunit tests. I turned off processIsolation in phpunit.xml and the issue resolved. Not sure why…

I am having this issue too. I have tried different phpunit versions from 4.8 to 5.7. Couldn’t fix it. In my local machine. I removed completely vendor folder and re-install everything. Laravel 5.4

My bad, I forgot to call parent::setUp() on a TestCase, so all test setup was not being initialized. Maybe it helps someone.

Note to others who happen to land here:

check your phpunit version, L5.2 supports phpunit 4.8 if you use phpunit 5 you will get this error

https://github.com/laravel/framework/issues/10808

I encountered this problem when running tests with phpunit in laravel 5.0 and seems to be specific to laravel + phpunit.
My solution, which might be wrong, was to modify the provided TestCase::setUp and tearDown methods.
Now, in my tests/TestCase.php file, tearDown is empty and my setUp looks like this:

public function setUp()
{
    if ( ! $this->app)
    {
        $this->refreshApplication();
    } else {
        $this->app->flush();
    }
}

It works because tearDown flushed the app (with the ExceptionHandler binding from bootstrap/app.php) so when phpunit actually got around to displaying the error it didn’t have a concrete implementation of the handler with which to display the errors.

Anyway, this only happens in 5.0 and not in 5.1 (as far as a I can tell)

I’m surprised nothing has been done about this for 10 years, and yes, I’ve been experiencing this issue on/off for that long, but anyways here is a new discovery that hopefully leads to someone else fixing this bug.

I made this change:

+        try {
            Assert::assertEquals($expected, $actual, 'message');
+        } catch (\Throwable $e) {
+            dump([
+                '$expected' => $expected,
+                 '$actual' => $actual,
+            ]);
+            // if I don't wrap this, I get BindingResolutionException
+            throw new \Exception($e->getMessage(), 0, $e);
+        }

This leads to my expected Exception stack being thrown in addition to the “Caused by PHPUnit\Framework\ExpectationFailedException” stack under it. It’s not desired to replace phpunit’s assertions with an exception, but there’s no explanation for why here and only here assertions cause ExceptionHandler] is not instantiable..

The main issue with the BindingResolutionException ExceptionHandler is not instantiable. bug is that it produces a stack that only includes laravel’s vendor code, so there is almost no way to figure out which test is causing it. Also, why does laravel try to instanciate ExceptionHandler for a failed phpunit assertion?

My problem was with “codedungeon/phpunit-result-printer 0.20.1”, downgrade library to 0.19.* fix the problem.

@GrahamCampbell good talk!

Like every day I setup a fresh new test project from laravel and diff the app files…

What I did is the following (no idea at all if this is OK or best practice or if it even works):

Added an ErrorServiceProvider to App\Providers:

<?php namespace App\Providers;

    use Illuminate\Support\ServiceProvider;
    use Whoops\Handler\PrettyPageHandler;
    use Whoops\Run;

    class ErrorServiceProvider extends ServiceProvider {

        /**
         * Register any error handlers.
         *
         * @return void
         */
        public function boot() {
            // This is only if you use the Whoops error handler (install via composer).
            /*$whoops = new Run;
            $whoops->pushHandler( new PrettyPageHandler );
            $whoops->register();*/
        }


        /**
         * Register the service provider.
         *
         * @return void
         */
        public function register() {
            $this->app->bind( 'Illuminate\Contracts\Debug\ExceptionHandler', 'App\Library\Exception\ExceptionHandler' );
        }

    }

Added an ExceptionHandler to App\Library\Exception:

<?php
    namespace App\Library\Exception;

    use Exception;
    use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
    use Illuminate\Http\Response;

    class ExceptionHandler implements ExceptionHandlerContract {

        protected $handlers = array();


        /**
         * Report or log an exception.
         *
         * @param  \Exception $e
         *
         * @return void
         */
        public function report( Exception $e ) {

        }


        /**
         * Render an exception into an HTTP response.
         *
         * @param  \Illuminate\Http\Request $request
         * @param  \Exception               $e
         *
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function render( $request, Exception $e ) {
            return new Response( $e->getMessage(), 500 );
        }


        /**
         * Render an exception to the console.
         *
         * @param  \Symfony\Component\Console\Output\OutputInterface $output
         * @param  \Exception                                        $e
         *
         * @return void
         */
        public function renderForConsole( $output, Exception $e ) {
            echo $e->getMessage();
        }

    }

And then I registered my service provider in config/app.php in the providers array:

    'providers' => [
        'App\Providers\ErrorServiceProvider',
        ...
    ]