Raz Weizman Raz Weizman - 20 days ago 8
PHP Question

Get error severity in Laravel 5

How can I find the severity of errors when dealing with exceptions?
I've added an email notification whenever there is an error but for some reason I'm also getting notifications on failing validations.

public function report(\Exception $e)
{
if ($e instanceof \Exception) {
$top = $e->getMessage().' on line '.$e->getLine();
$body = $e->getTraceAsString();

Mail::queue('emails.general', compact('top','body'), function($message) {
$message->from('abc@abc.com','abc');
$message->to('abc@gmail.com','abc')
->subject('An error on Abc');
});

}

parent::report($e);
}

Answer

In your report function of App\Exceptions\Handler class you can do as:

if ($this->shouldReport($e)) {
    // send email
}

This will not report the exception listed in the $dontReport variable.

So your final code will look as:

public function report(\Exception $e)
{
    if ($this->shouldReport($e)) {
        $top = $e->getMessage().' on line '.$e->getLine();
        $body = $e->getTraceAsString();

        Mail::queue('emails.general', compact('top','body'), function($message) {
            $message->from('abc@abc.com','abc');
            $message->to('abc@gmail.com','abc')
                ->subject('An error on Abc');
        });
    }

    parent::report($e);
}

And also if you want to send the full stack trace of exception then you can use this github repo.

Comments