use App\Mail\ExceptionOccurred;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Throwable;
use App\Mail\ExceptionOccured;
use Illuminate\Support\Facades\Log;
use Mail;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
/**
* Register the exception handling callbacks for the application.
*/
public function register(): void
{
$this->reportable(function (Throwable $e) {
$enableEmailExceptions = config('exceptions.emailExceptionEnabled');
if ($enableEmailExceptions) {
$this->sendEmail($e);
}
});
}
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Throwable $exception
*
* @return void
*/
public function report(Throwable $exception)
{
$enableEmailExceptions = config('exceptions.emailExceptionEnabled');
if ($enableEmailExceptions === '') {
$enableEmailExceptions = config('exceptions.emailExceptionEnabledDefault');
}
if ($enableEmailExceptions && $this->shouldReport($exception)) {
$this->sendEmail($exception);
}
parent::report($exception);
}
/**
* Sends an email upon exception.
*
* @param \Throwable $exception
*
* @return void
*/
public function sendEmail(Throwable $exception)
{
try {
$e = FlattenException::create($exception);
$handler = new SymfonyExceptionHandler();
$html = $handler->getHtml($e);
Mail::send(new ExceptionOccured($html));
} catch (Throwable $exception) {
Log::error($exception);
}
}