Disable laravel error handler

Do I need to disable the laravel error handler at all?

I want to just display standard PHP errors , not Whoops, looks like something went wrong errors Whoops, looks like something went wrong .

+25
php error-handling laravel laravel-4
source share
7 answers

Not without a significant violation of the principles of structure (I will tell you how to do it below if you are still interested).

There are several things that make this difficult to achieve. It is simple enough to disable the default error and exception handlers

 set_error_handler(null); set_exception_handler(null); 

but that leaves you with two major hurdles.

First, Laravel registers a shutdown handler as part of its self-tuning, and this break function will look for the last error, and if it was a fatal error, manually call the exception handling code. There is no easy way to uninstall the shutdown function .

Secondly, the main Laravel application handler is as follows:

 #File: vendor/laravel/framework/src/Illuminate/Foundation/Application.php public function handle(SymfonyRequest $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { try { $this->refreshRequest($request = Request::createFromBase($request)); $this->boot(); return $this->dispatch($request); } catch (\Exception $e) { if ($this->runningUnitTests()) throw $e; return $this['exception']->handleException($e); } } 

That is, if the application code throws an exception, Laravel catches it and manually calls the handleException exception handleException (which triggers the standard Laravel exception handling). Unable to let PHP handle the fatal exception that occurs in your application, Laravel blocks this from ever happening.

The part where I will tell you how to do what you want

All of this means that we must replace the main Laravel application with our own. In bootstrap/start.php following line exists

 #File: bootstrap/start.php $app = new Illuminate\Foundation\Application; 

Replace it with the following

 ini_set('display_errors','1'); class MyApplication extends Illuminate\Foundation\Application { function startExceptionHandling() { //do nothing } public function handle(Symfony\Component\HttpFoundation\Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { $this->refreshRequest($request = Request::createFromBase($request)); $this->boot(); return $this->dispatch($request); } } $app = new MyApplication; 

The first thing we do is set the ini mapping display errors to 1 . This ensures that errors are displayed in the browser.

Next, we define a new application class that extends the class of the real application.

Finally, we replace the real Laravel $app object with the object created by our class.

In our application class, we will close startExceptionHandling . This prevents Laravel from setting custom exceptions, errors, and shutdowns. We also define handle to remove loading / sending the application from try / catch. This is the most fragile part of the process and may look different depending on your version of Laravel.

Final Warnings

If the handle method changes in a future version of Laravel, it will break.

If custom packages rely on the addition of custom exception handlers, they can break.

I would recommend staying away from this like nothing but a temporary debugging technique.

+38
source share

Then set 'debug' => false in the file \ config \ local \ app.php

 <?php return array( 'debug' => false, ); 
+10
source share

In laravel 5, to disable debugging you just need to comment

 //'debug' => env('APP_DEBUG'), 

in the file \ config \ app.php

+5
source share

Exception handling is hardcoded in the Application class. You can override classes in bootstrap/start.php :

 class ExceptionHandler { public function handleException($exception) { throw $exception; } public function handleConsole($exception) { throw $exception; } } class MyApplication extends Illuminate\Foundation\Application { public function registerExceptionProvider() {} public function startExceptionHandling() {} } $app = new MyApplication; 

Of course, this is definitely not recommended.

+2
source share

On the question: Laravel has a built-in method for disabling exceptions in unit tests:

$this->withoutExceptionHandling()

Related Laracast: https://laracasts.com/series/whats-new-in-laravel-5-5/episodes/17

0
source share

It will bring you closer. Perhaps this will be the more correct way. It replaces the current Laravel exception handler with one method class. I have not tested this, besides the main route below, so you may need to add other methods to suit different situations.

 class ExceptionHandler { public function handleException($e) { echo $e; die; } } App::bind('exception', App::share(function($app) { return new ExceptionHandler; })); Route::get('/', function() { throw new Exception('Testing'); }); 
-one
source share

In the .env file .env just change:

 APP_DEBUG=true 

To:

 APP_DEBUG=false 
-2
source share

All Articles