Laravel 7 Symfony 5 更新后出现电子邮件异常
Laravel 7 email exceptions broke after Symfony 5 update
我已经升级到 Laravel 7.1,现在使用 Symfony 5 这些 类 不再存在:
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
我在我的 app\Exceptions\Handler.php 文件中使用它们在异常重新抛出时发送电子邮件通知并且它们在 Laravel 6 中运行良好,但是当我从 [=35] 升级时出现故障=] 到 7.1.2 也升级到 Symfony 5.
我用这些替换了前面提到的 类:
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
然后替换为:
$e = FlattenException::create($exception);
$handler = new SymfonyExceptionHandler();
$html = $handler->getHtml($e);
有了这个:
$e = FlattenException::create($exception);
$handler = new HtmlErrorRenderer();
$content = $handler->getBody($e);
有效,但现在我不再像以前那样在电子邮件中获取调试内容,而是收到一条更基本的错误消息,因为它是针对 public.
您可以在此处查看不同格式的示例:
https://symfony.com/doc/current/controller/error_pages.html
我确定我遗漏了一些简单的东西,但我还没有想出如何让它向我发送详细的异常数据,就像我在升级之前获得的那样。
有什么建议吗?
要获得 html 的完整响应,只需使用:
$html = ExceptionHandler::convertExceptionToResponse($e);
这是完整的Handler.php代码
<?php
namespace App\Exceptions;
use Log;
use Mail;
use Exception;
use Throwable;
use App\Mail\ErrorNotification;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
HttpException::class,
ModelNotFoundException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Throwable $e)
{
if ($this->shouldReport($e)) {
$this->sendEmail($e);
}
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Throwable $e)
{
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
return parent::render($request, $e);
}
public function sendEmail(Throwable $e)
{
try {
$html = ExceptionHandler::convertExceptionToResponse($e);
Mail::to('youremailhere@gmail.com')->send(new ErrorNotification($html));
} catch (Exception $ex) {
Log::error($ex);
}
}
}
下面是我最终用来在异常通知电子邮件中获取我想要的结果的代码。我之前遗漏的主要部分是我没有将真实值传递到 HtmlErrorRender class 以引发调试标志。此处显示更正后的行:
new HtmlErrorRenderer(true);
这是我现在为 app/Exceptions/Handler.php 文件
使用的完整代码
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Log;
use Throwable;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
if ($this->shouldReport($exception)) {
$this->sendEmail($exception); // sends an email
}
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof \Illuminate\Session\TokenMismatchException) { //https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e
return redirect()
->back()
->withInput($request->except('password'))
->with('errorMessage', 'This form has expired due to inactivity. Please try again.');
}
return parent::render($request, $exception);
}
/**
* Sends an email to the developer about the exception.
*
* @return void
*/
public function sendEmail(Throwable $exception)
{
try {
$e = FlattenException::create($exception);
$handler = new HtmlErrorRenderer(true); // boolean, true raises debug flag...
$css = $handler->getStylesheet();
$content = $handler->getBody($e);
\Mail::send('emails.exception', compact('css','content'), function ($message) {
$message
->to('youremailhere@gmail.com')
->subject('Exception: ' . \Request::fullUrl())
;
});
} catch (Throwable $ex) {
Log::error($ex);
}
}
}
$css 和 $content 被传递到位于 resources/views/emails/exception.blade.php 的视图中。我在该文件中的代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>{!! $css ?? '' !!}</style>
</head>
<body>
{!! $content ?? '' !!}
</body>
</html>
测试答案后,我得到一些错误 "Parssing to Exception class",所以如果您使用 Laravel 7,您需要使用 "createFromThrowable" 代替 "create" 以获得兼容使用 Throwable 对象。
我已经升级到 Laravel 7.1,现在使用 Symfony 5 这些 类 不再存在:
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
我在我的 app\Exceptions\Handler.php 文件中使用它们在异常重新抛出时发送电子邮件通知并且它们在 Laravel 6 中运行良好,但是当我从 [=35] 升级时出现故障=] 到 7.1.2 也升级到 Symfony 5.
我用这些替换了前面提到的 类:
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
然后替换为:
$e = FlattenException::create($exception);
$handler = new SymfonyExceptionHandler();
$html = $handler->getHtml($e);
有了这个:
$e = FlattenException::create($exception);
$handler = new HtmlErrorRenderer();
$content = $handler->getBody($e);
有效,但现在我不再像以前那样在电子邮件中获取调试内容,而是收到一条更基本的错误消息,因为它是针对 public.
您可以在此处查看不同格式的示例: https://symfony.com/doc/current/controller/error_pages.html
我确定我遗漏了一些简单的东西,但我还没有想出如何让它向我发送详细的异常数据,就像我在升级之前获得的那样。
有什么建议吗?
要获得 html 的完整响应,只需使用:
$html = ExceptionHandler::convertExceptionToResponse($e);
这是完整的Handler.php代码
<?php
namespace App\Exceptions;
use Log;
use Mail;
use Exception;
use Throwable;
use App\Mail\ErrorNotification;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
HttpException::class,
ModelNotFoundException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Throwable $e)
{
if ($this->shouldReport($e)) {
$this->sendEmail($e);
}
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Throwable $e)
{
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
return parent::render($request, $e);
}
public function sendEmail(Throwable $e)
{
try {
$html = ExceptionHandler::convertExceptionToResponse($e);
Mail::to('youremailhere@gmail.com')->send(new ErrorNotification($html));
} catch (Exception $ex) {
Log::error($ex);
}
}
}
下面是我最终用来在异常通知电子邮件中获取我想要的结果的代码。我之前遗漏的主要部分是我没有将真实值传递到 HtmlErrorRender class 以引发调试标志。此处显示更正后的行:
new HtmlErrorRenderer(true);
这是我现在为 app/Exceptions/Handler.php 文件
使用的完整代码<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Log;
use Throwable;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
if ($this->shouldReport($exception)) {
$this->sendEmail($exception); // sends an email
}
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof \Illuminate\Session\TokenMismatchException) { //https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e
return redirect()
->back()
->withInput($request->except('password'))
->with('errorMessage', 'This form has expired due to inactivity. Please try again.');
}
return parent::render($request, $exception);
}
/**
* Sends an email to the developer about the exception.
*
* @return void
*/
public function sendEmail(Throwable $exception)
{
try {
$e = FlattenException::create($exception);
$handler = new HtmlErrorRenderer(true); // boolean, true raises debug flag...
$css = $handler->getStylesheet();
$content = $handler->getBody($e);
\Mail::send('emails.exception', compact('css','content'), function ($message) {
$message
->to('youremailhere@gmail.com')
->subject('Exception: ' . \Request::fullUrl())
;
});
} catch (Throwable $ex) {
Log::error($ex);
}
}
}
$css 和 $content 被传递到位于 resources/views/emails/exception.blade.php 的视图中。我在该文件中的代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>{!! $css ?? '' !!}</style>
</head>
<body>
{!! $content ?? '' !!}
</body>
</html>
测试答案后,我得到一些错误 "Parssing to Exception class",所以如果您使用 Laravel 7,您需要使用 "createFromThrowable" 代替 "create" 以获得兼容使用 Throwable 对象。