Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable error reporting entirely in Laravel production? [duplicate]

Tags:

I would like to disable error reporting entirely on production, because we have some very old code we still need to fix but for now does work (yes I don't like it either). We cannot fix everything in a few days, so we need to just supress the warnings and exceptions like we always did.

The real problem is that it already throws an exception on a simple lazy bug like (because var is not defined)

if(!$var) {
     // do whatever
}

tried

APP_DEBUG=false

APP_LOG_LEVEL=emergency

display_errors(false);
set_error_handler(null);
set_exception_handler(null);

But it still shows an ErrorException

Undefined variable: script_name_vars_def

edit: The code works like this

web.php

Route::any('/someroute', 'somecontroller@controllerFunc');

somecontroller.php

public controllerFunc() {
    ob_start();
    require '/old_index.php';
    $html = ob_get_clean();

    return response($html);
}

This way we use Laravel routing without having to rewrite the old code immediately.

I know I can fix this warning very easy, but there are many, many more of these errors and we need to use Laravel routing now. Fix the problems later.

ideas

  • Use some wildcard in $dontReport.
  • Use a @ suppression at the right place
  • Can it be http://php.net/manual/en/scream.examples-simple.php

edit to explain after which steps middleware didn't work

1) create midddleware

php artisan make:middleware SuppressExceptions

2) Write it

SuppressExceptions.php

public function handle($request, Closure $next)
{
    error_reporting(0);
    return $next($request);
}

3) Register

laravel/app/Http/Kernel.php

protected $middlewareGroups = [
   'web' => [
       \App\Http\Middleware\SuppressExceptions::class,
],
like image 836
online Thomas Avatar asked Jun 28 '17 15:06

online Thomas


People also ask

How do I turn off error reporting in laravel?

You may turn off error details by setting the debug option in your app/config/app. php file to false .

How do I turn on error reporting in laravel?

Through your config/app. php , set 'debug' => env('APP_DEBUG', false), to true . Or in a better way, check out your . env file and make sure to set the debug element to true.

What is error exception in laravel?

When you start a new Laravel project, error and exception handling is already configured for you. The App\Exceptions\Handler class is where all exceptions thrown by your application are logged and then rendered to the user.


1 Answers

Yes you can change the error reporting. In fact, the framework provides a place to intercept the exceptions: App\Exceptions\Handler. By default the render method will convert the exception thrown to a HTML response. The APP_ENV and APP_DEBUG values will only change how this error response will render (details on the exception stack trace or not, basically).

Try changing the render method to

public function render($request, Exception $exception)
{
    if ($exception instanceof ErrorException) {
        error_reporting(0);

        $kernel = app(\Illuminate\Contracts\Http\Kernel::class);
        $response = $kernel->handle($request)->send();
        return $kernel->terminate($request, $response);
    }

    return parent::render($request, $exception);
}

This basically turns reporting off and then attempts to re-handle the request. In the if clause you may check for any condition you want (the class of the exception, the severity, etc.). Catching ErrorException will probably cover your needs, but notice that you may not be able to recover from a fatal error this way.

Anyway, you should take that as a "proof of concept"... For non-idempotent requests, this "re-handle" approach is not good. Instead, just create a Middleware with

public function handle($request, Closure $next)
{
    error_reporting(0);
    return $next($request);
}

Same as before, fatal errors can't be recovered this way. But you can show a custom error message combining this middleware with the exception handler approach from before:

public function render($request, Exception $exception)
{
    if ($exception instanceof FatalErrorException) {
        return view('fatal-error', ['exception' => $exception]);
    }

    return parent::render($request, $exception);
}
like image 165
alepeino Avatar answered Sep 19 '22 04:09

alepeino