Laravel 没有向 Handler.php 的报告方法报告所有异常

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

我在app/Exceptions/Handler的举报方法中添加了发送邮件功能。php

但有些异常该方法被调用,有些则没有,$dontReport 数组为空

例如,上面的这个错误没有被报告。

这是处理程序

    
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@myemail.com'])
                    ->subject('Exception: ' . \Request::fullUrl());
            });
        } catch (Throwable $exception) {
            Log::error($exception);
        }
    }

}

Laravel自动过滤掉某些类型的异常。

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

尝试将您的报告方法更新为此,这应该为每个异常发送一封电子邮件,无论类型如何。

Link to the docs for reporting exceptions.

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

    parent::report($exception);
}