3

In Laravel, whenever there error, even minor NOTICES, WARNINGS and DEPRECATED erros, I got the full debug info which kills the application. In my App.config I've turned debug => false and I get the message of 'Whoops, looks like something went wrong.'

How can I turn off all error handling but Laravel to just get normal PHP errors that do not interrupt the entire flow of application?

Devin Dixon
  • 11,553
  • 24
  • 86
  • 167

3 Answers3

3

If you don't want to interrupt your workflow for certain PHP error types, you will need to disable the error handler registered by Laravel for those errors.

Laravel registers its error handling in Illuminate/Foundation/Bootstrap/HandleExceptions.php. This bootstrapper is one of several that is called when your Http kernel handles a request.

While there are a couple ways to do what you want to do, I think the easiest is to handle the event that is fired after this bootstrapper is called. In the event handler, you can reset the error handler for the errors you don't want Laravel to process.

In your bootstrap/app.php file, add the following line right before $app is returned:

$app->afterBootstrapping(
    'Illuminate\Foundation\Bootstrap\HandleExceptions',
    function ($app) {
        set_error_handler(function ($level, $message, $file = '', $line = 0, $context = []) {
            // Check if this error level is handled by error reporting
            if (error_reporting() & $level) {
                // Return false for any error levels that should
                // be handled by the built in PHP error handler.
                if ($level & (E_WARNING | E_NOTICE | E_DEPRECATED)) {
                    return false;
                }

                // Throw an exception to be handled by Laravel for all other errors.
                throw new ErrorException($message, 0, $level, $file, $line);
            }
        });
    }
);
patricus
  • 59,488
  • 15
  • 143
  • 145
2

The App\Exceptions\Handler.php file is built just for this.

In the public function render() method, you can catch applications and perform certain redirects/page views if you so choose:

For instance, you can capture HttpException's in your application and then return an error page if you wished:

public function render($request, Exception $e)
{
    //other stuff

    if ($e instanceof HttpException) {
        return view('errors.general')->withErrors([
            'message' => 'The application encountered an error!'
        ]);
    }

    return parent::render($request, $exception);
}
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • 1
    Reading the question and then your nickname made me giggle because it was exactly what I had thought after reading it :) anyway, +1 from me for correct answer. – N.B. Dec 11 '16 at 22:57
1

That up-voted answer is the opposite of what he asked. As far as I can tell, there isn't a way to separate error reporting and laravel taking over rendering of the screen. I've been looking through the Laravel 5 code and haven't found a way to split them apart using their setup yet.

You could write your own library to totally take over all error handling and remove all of laravels internal tracking, but then you'd have to make sure to pass it back to laravel in the cases where you need the orig page error handling. Easiest way would be find a 3rd part error handler vendor then modify it to take over all error handlers and not block rendering.