Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable

I'm using Eloquent in combination with Slim Framework to build a small API. For some reason, I am getting this error, and I can't locate the problem:

Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.

Database settings are correct. This error is thrown in my staging environment on the server. Locally, with MySQL 5.6.29, it works perfectly. Although I don't think it's related to MySQL 5.7 because I have another app running on the same server with the same version of Eloquent.

Can anyone point me in the right direction?

I'm using:

  • Eloquent Version: 5.3.23
  • PHP Version: 5.6.28
  • Database Driver & Version: MySQL 5.7.16

Backtrace

/path/to/app/shared/vendor/illuminate/container/Container.php:644 
Illuminate\Container\Container -> make
/path/to/app/shared/vendor/illuminate/database/Connectors/ConnectionFactory.php:130 
call_user_func 
/path/to/app/shared/vendor/illuminate/database/Connection.php:964 
Illuminate\Database\Connection -> getPdo 
/path/to/app/shared/vendor/illuminate/database/Connection.php:832 
Illuminate\Database\Connection -> reconnectIfMissingConnection 
/path/to/app/shared/vendor/illuminate/database/Connection.php:717 
Illuminate\Database\Connection -> run 
/path/to/app/shared/vendor/illuminate/database/Connection.php:350 
Illuminate\Database\Connection -> select 
/path/to/app/shared/vendor/illuminate/database/Query/Builder.php:1648 
Illuminate\Database\Query\Builder -> runSelect 
/path/to/app/shared/vendor/illuminate/database/Query/Builder.php:1634 
Illuminate\Database\Query\Builder -> get 
/path/to/app/shared/vendor/illuminate/database/Eloquent/Builder.php:632 
Illuminate\Database\Eloquent\Builder -> getModels 
/path/to/app/shared/vendor/illuminate/database/Eloquent/Builder.php:327 
Illuminate\Database\Eloquent\Builder -> get 
/path/to/app/shared/vendor/illuminate/database/Eloquent/Builder.php:297 
Illuminate\Database\Eloquent\Builder -> first 
...

I'm suspecting it has something to do with an unregistered error handler:

github link

But how can I register the custom error handler when not using Laravel?

The solution of @patricus can be implemented as follows:

// bootstrap Eloquent ORM
$container = new Container();
$container->singleton(
  'Illuminate\Contracts\Debug\ExceptionHandler'
, 'My\Custom\DatabaseExceptionHandler'
);

$factory    = new ConnectionFactory( $container );
$connection = $factory->make([
  'driver'    => 'mysql'
, 'host'      => App::config( 'database_host' )
, 'database'  => App::config( 'database_name' )
, 'username'  => App::config( 'database_user' )
, 'password'  => App::config( 'database_password' )
, 'charset'   => App::config( 'database_charset' )
, 'collation' => App::config( 'database_collate' )
, 'prefix'    => ''
]);

$resolver = new ConnectionResolver();
$resolver->addConnection( 'default', $connection );
$resolver->setDefaultConnection( 'default' );

// initialize connection
Model::setConnectionResolver( $resolver );
like image 915
wout Avatar asked Nov 27 '22 03:11

wout


1 Answers

You should be able to access the container to register your own exception handler through the capsule.

$capsule = new \Illuminate\Database\Capsule\Manager();

$capsule->getContainer()->singleton(
    \Illuminate\Contracts\Debug\ExceptionHandler::class,
    \Your\ExceptionHandler\Implementation::class
);

If you want a real quick, very dumb, exception handler, you can use this:

class DumbExceptionHandler implements \Illuminate\Contracts\Debug\ExceptionHandler
{
    public function report(Exception $e)
    {
        //
    }

    public function render($request, Exception $e)
    {
        throw $e;
    }

    public function renderForConsole($output, Exception $e)
    {
        throw $e;
    }
}

NB: untested. I believe this should work looking at the code.

like image 167
patricus Avatar answered Nov 29 '22 04:11

patricus