Disable exceptions for E_USER_WARNING

Laravel returns 500 when I do:

trigger_error("Some message", E_USER_WARNING);

I need this to not be a mistake, but I want it to work through \App\Exceptions\Handler::report, which logs a warning for Sentry.

How to disable the inclusion of warnings and errors in exceptions from Laravel 5.2?

+4
source share
3 answers

You can edit the laravel error handler only for warning messages in HandleExceptions.php

public function handleError($level, $message, $file = '', $line = 0, $context = [])
{
    $exception = new ErrorException($message, 0, $level, $file, $line);

    if (E_USER_WARNING & $level) {
        $this->getExceptionHandler()->report($exception);
    }
    elseif (error_reporting() & $level) {
        throw $exception;
    }
}

User warning. Changing the vendor code is usually bad.

You can avoid changing the provider code by extending the class HandleExceptionsand registering a new class inKernel.php

MyHandleExceptions extends HandleExceptions {
    public function handleError($level, $message, $file = '', $line = 0, $context = [])
    {   
        if (E_USER_WARNING & $level) {
            $this->getExceptionHandler()->report(new ErrorException($message, 0, $level, $file, $line));
        }
        else {
            return parent::handleError($level, $message, $file, $line, $context);
        }
    }
}
+4
source

, ErrorHandler, :

Route::get('error', function() {
    $client = new Raven_Client(env("SENTRY_DSN"));
    $client->captureMessage("Some Warning", ['log'], [
        'level' => 'warning'
    ]);
});

, php, .

function trigger_sentry_warning($message) {
    $client = new Raven_Client(env("SENTRY_DSN"));
    $client->captureMessage($message, ['log'], [
        'level' => 'warning'
   ]);
}
+1

, php. error_reporting()? set_error_handler

:

, Laravel , , syslog errorlog. , Laravel, config/app.php. , ,

laravel

0

Source: https://habr.com/ru/post/1679766/


All Articles