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



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

use SymfonyComponentDebugExceptionFlattenException;
use SymfonyComponentDebugExceptionHandler as SymfonyExceptionHandler;

当异常再次抛出时,我在我的app\Exceptions\Handler.php文件中使用它们来发送电子邮件通知,它们在Laravel 6中运行良好,但当我从6.x升级到7.1.2时坏了,也升级到Symfony 5。

我用以下类替换了前面提到的类:

use SymfonyComponentErrorHandlerErrorRendererHtmlErrorRenderer;
use SymfonyComponentErrorHandlerExceptionFlattenException;

然后替换为:

$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

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

有什么建议吗?

下面是我最终使用的代码,用于在异常通知电子邮件中获得我想要的结果。我之前缺少的主要部分是,我没有向HtmlErrorRender类传递一个true值来引发调试标志。校正后的行如下所示:

new HtmlErrorRenderer(true);

以下是我现在为应用程序/Exceptions/Handler.php文件使用的完整代码

<?php
namespace AppExceptions;
use IlluminateFoundationExceptionsHandler as ExceptionHandler;
use Log;
use Throwable;
use SymfonyComponentErrorHandlerErrorRendererHtmlErrorRenderer;
use SymfonyComponentErrorHandlerExceptionFlattenException;
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  IlluminateHttpRequest  $request
* @param  Throwable  $exception
* @return SymfonyComponentHttpFoundationResponse
*
* @throws Throwable
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof IlluminateSessionTokenMismatchException) {  //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,你需要使用"create"的"createFromThrowable"instade来与Throwable Object兼容。

要获得html格式的完整响应,只需使用:

$html = ExceptionHandler::convertExceptionToResponse($e);

以下是完整的Handler.php代码

<?php
namespace AppExceptions;
use Log;
use Mail;
use Exception;
use Throwable;
use AppMailErrorNotification;
use IlluminateDatabaseEloquentModelNotFoundException;
use SymfonyComponentHttpKernelExceptionHttpException;
use IlluminateFoundationExceptionsHandler as ExceptionHandler;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;

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  IlluminateHttpRequest  $request
* @param  Exception  $e
* @return IlluminateHttpResponse
*/
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);
}
}
}

相关内容

最新更新