Matheus Benedet
Matheus Benedet

Reputation: 75

Laravel not reporting all exceptions to report method of Handler.php

I added a send email function to the report method of app/Exceptions/Handler.php

But some exceptions the method is called and others not, the $dontReport array is empty

error not reported

This error above, for example, is not being reported.

Here is the handler

    
class Handler extends ExceptionHandler
{
    protected $dontReport = [
        //
    ];

    public function report(Throwable $exception)
    {
        if ($this->shouldReport($exception)) {
            $this->sendEmail($exception);
        }
        parent::report($exception);
    }

    public function sendEmail(Throwable $exception)
    {
        try {
            $e = FlattenException::create($exception);
            $handler = new HtmlErrorRenderer(true);
            $css = $handler->getStylesheet();
            $content = $handler->getBody($e);
            
            \Mail::send('emails.exception', compact('css', 'content'), function ($message) {
                $message->to(['[email protected]'])
                    ->subject('Exception: ' . \Request::fullUrl());
            });
        } catch (Throwable $exception) {
            Log::error($exception);
        }
    }

}

Upvotes: 2

Views: 4315

Answers (1)

thursday_dan
thursday_dan

Reputation: 581

Laravel automatically filters out certain types of exceptions automatically.

Behind the scenes, Laravel already ignores some types of errors for you, such as exceptions resulting from 404 HTTP "not found" errors or 419 HTTP responses generated by invalid CSRF tokens

Try updating your report method to this, this should send an email for every exception, regardless of the type.

Link to the docs for reporting exceptions.

public function report(Throwable $exception)
{
    $this->sendEmail($exception);

    parent::report($exception);
}

Upvotes: 3

Related Questions