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/
Looks like we need to register our own ExceptionHandler (in our own ErrorServiceProvider) now. Would be great if something like this could be documented.
Also, as default behavior (if it's not a bug but rather an intended feature) this is very end-use unfriendly.
Which means what if you could tell me exactly what to do? :p I agree that it's very end-use unfriendly as of now
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',
...
]
Ixx... very ugly problem! My app is down too... Does someone have a best practice solution?
I may have hit the same error as my error message looks pretty similar. I got this while trying to add HTML class to L5, but obviously the error is absolutely not related to HTML service provider.
I installed XDebug and dig into the code, my current assumption is that the exception handler that is suppose to write something intelligent on the console/log fails and the error we get, comes from an error in the exception handler itself
In my case, in order to debug my HTML::xxx I added and brutal "$a= new HtmlBuilder(); error_log(sprintf("****** a=%s", $a));" which created a new error as in PHP object cannot be printed as string directly. But while I should have got something: ** like: "ErrorException' with message 'Object of class Illuminate\Html\HtmlBuilder could not be converted to string'" ** I got: "Uncaught exception 'Illuminate\Container\BindingResolutionException' with message 'Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable."
Tracing with the XDebug, I find out that if 1st handler collects correctly the error message "file Kernel.php:37" the next one "HandleExceptions.php:73, Illuminate\Foundation\Bootstrap\HandleExceptions->handleException()" never returns from GetExpectionHandler()->report method. I try to trace deeper, but got lost :(
Reproducing the bug should be simple. In a controller do something stupid like I did [converting an object in String], this will raise an exception and as today it fail with "[Illuminate\Contracts\Debug\ExceptionHandler] is not instantiate" with an error in the core of L5 when it should not.
You're doing it wrong.
Example of the right way to do this:
Bind your exception handler here: https://github.com/GrahamCampbell/Bootstrap-CMS/blob/c09201d361921b985d99727c64cd444ef51afc1c/bootstrap/app.php#L51.
Write your exception hander: https://github.com/GrahamCampbell/Bootstrap-CMS/blob/c09201d361921b985d99727c64cd444ef51afc1c/app/Debug/ExceptionHandler.php.
In this case, I've abstracted the juicy bits for my debug displayer, and my own plain displayer to separate classes: https://github.com/GrahamCampbell/Bootstrap-CMS/blob/c09201d361921b985d99727c64cd444ef51afc1c/app/Debug/DebugDisplayer.php, https://github.com/GrahamCampbell/Bootstrap-CMS/blob/c09201d361921b985d99727c64cd444ef51afc1c/app/Debug/PlainDisplayer.php.
My example actually uses whoops too, so it should suite you. You may want to simply return the symfony plain error response instead of what I've done.
Thank you GrahamCampbell the lack of binding in bootstrap was the error was. When I downloaded L5 few days ago the exception handler binding was missing from bootstrap/app.php and unfortunately composer update does not warn you about it app.php when you try to update. Might be a good idea to shift to git for all those L5 test.
The bad news is that I lost 1/2 day tracking a no existing bug. The good new is that I now have a XDebug in place with PhpStorm and that's really cool.
@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.
@darkwinternight My "arrogant" comment was as it was because the laravel issues aren't meant for discussion of how to do things - they're meant for discussion of bugs (and new features). This is something more for the forums. My comment was meant to close this up quickly. When using Laravel 5, you're going to have to follow the changes in laravel/framework and laravel/laravel very closely, as am I. Taylor is the one making the big changes and making the decisions.
@GrahamCampbell good talk!
Like every day I setup a fresh new test project from laravel and diff the app files...
It is through that Graham answer was a little raw :)
For me two lines missing in a config file should be conciser as a bug. I agree that it's an normal bug in a beta live cycle, but it's still a bug.
This being said, Graham provided me with the answer. A the end of the day, this is what was really important to me.
@fulup-bzh And to me :) Thanks again @GrahamCampbell.
In Ubuntu Client this is the app I use to see the diff between dev releases: https://apps.ubuntu.com/cat/applications/raring/meld/
They are many good tools for compare/diff. The most impressive one I used being Netbeans. I'm in the process of moving from NB to PhpStorm but diff/history is one area where NB really out perform anyone else. This being said, when you do not know what you're looking for, even the best tool cannot not help much :)
Laravel 5 has been released to the masses and I am leading a conversion from Slim / Eloquent 4 to Laravel 5.
However, I am seeing this exception being thrown and rethrown as I'm implementing tests to debug the code that we are moving over.
Is there anything in the works to provide some default handling around this so that it works out of the box?
Can anyone help me (new to Laravel not php or phpunit) and others who are not experienced with Laravel to know how to quickly overcome this error in Unit Tests and elsewhere so that we're not getting hit with it every time there is an exception thrown?
^^^^ +1
+1
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)
Had the same problem the other day. Apparently the default Handler shipped with L5.2 has to be combined with the ExceptionHandler interface:
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);