Symfony 5 更新后 Laravel 7 电子邮件异常中断

我已经升级到 Laravel 7.1,现在有了 Symfony 5,这些类不再存在:


use Symfony\Component\Debug\Exception\FlattenException;

use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;

我在我的 app\Exceptions\Handler.php 文件中使用它们来在抛出异常时发送电子邮件通知,并且它们在 Laravel 6 中运行良好,但是当我从 6.x 升级到 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);

这可行,但现在我不再像以前那样在电子邮件中获取调试内容,而是收到一条更基本的错误消息,因为它是针对公众的。


您可以在此处查看不同格式的示例: https ://symfony.com/doc/current/controller/error_pages.html


我确信我缺少一些简单的东西,但我还没有想出如何让它像我在升级之前得到的那样向我发送详细的异常数据。


有什么建议么?


猛跑小猪
浏览 119回答 3
3回答

繁星coding

下面是我最终用来在异常通知电子邮件中获得我想要的结果的代码。我之前缺少的主要部分是我没有将真实值传递给 HtmlErrorRender 类来引发调试标志。更正后的行如下所示:new HtmlErrorRenderer(true);这是我现在用于 app/Exceptions/Handler.php 文件的完整代码<?phpnamespace 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{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* A list of the exception types that are not reported.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $dontReport = [&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* A list of the inputs that are never flashed for validation exceptions.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $dontFlash = [&nbsp; &nbsp; &nbsp; &nbsp; 'password',&nbsp; &nbsp; &nbsp; &nbsp; 'password_confirmation',&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Report or log an exception.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Throwable&nbsp; $exception&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @throws \Exception&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function report(Throwable $exception)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($this->shouldReport($exception)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->sendEmail($exception); // sends an email&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; parent::report($exception);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Render an exception into an HTTP response.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @param&nbsp; \Throwable&nbsp; $exception&nbsp; &nbsp; &nbsp;* @return \Symfony\Component\HttpFoundation\Response&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @throws \Throwable&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function render($request, Throwable $exception)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($exception instanceof \Illuminate\Session\TokenMismatchException) {&nbsp; //https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->back()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withInput($request->except('password'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->with('errorMessage', 'This form has expired due to inactivity. Please try again.');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return parent::render($request, $exception);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Sends an email to the developer about the exception.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function sendEmail(Throwable $exception)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $e = FlattenException::create($exception);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $handler = new HtmlErrorRenderer(true); // boolean, true raises debug flag...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $css = $handler->getStylesheet();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $content = $handler->getBody($e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \Mail::send('emails.exception', compact('css','content'), function ($message) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $message&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->to('youremailhere@gmail.com')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->subject('Exception: ' . \Request::fullUrl())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; } catch (Throwable $ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log::error($ex);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}$css 和 $content 被传递到 resources/views/emails/exception.blade.php 的视图中。我在该文件中的代码如下:<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="UTF-8" />&nbsp; &nbsp; &nbsp; &nbsp; <style>{!! $css ?? '' !!}</style>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; {!! $content ?? '' !!}&nbsp; &nbsp; </body></html>

交互式爱情

测试答案后,我得到一些错误“解析到异常类”,所以如果你使用 Laravel 7,你需要使用“create”的“createFromThrowable”代替“create”来与 Throwable 对象兼容。

杨魅力

要获得完整的 html 响应,只需使用:$html = ExceptionHandler::convertExceptionToResponse($e);这是完整的 Handler.php 代码<?phpnamespace 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{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* A list of the exception types that should not be reported.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $dontReport = [&nbsp; &nbsp; &nbsp; &nbsp; HttpException::class,&nbsp; &nbsp; &nbsp; &nbsp; ModelNotFoundException::class,&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Report or log an exception.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* This is a great spot to send exceptions to Sentry, Bugsnag, etc.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Exception&nbsp; $e&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function report(Throwable $e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($this->shouldReport($e)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->sendEmail($e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return parent::report($e);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Render an exception into an HTTP response.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @param&nbsp; \Exception&nbsp; $e&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function render($request, Throwable $e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($e instanceof ModelNotFoundException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $e = new NotFoundHttpException($e->getMessage(), $e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return parent::render($request, $e);&nbsp; &nbsp; }&nbsp; &nbsp; public function sendEmail(Throwable $e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $html = ExceptionHandler::convertExceptionToResponse($e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mail::to('youremailhere@gmail.com')->send(new ErrorNotification($html));&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception $ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log::error($ex);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP