3 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
下面是我最終用來(lái)在異常通知電子郵件中獲得我想要的結(jié)果的代碼。我之前缺少的主要部分是我沒(méi)有將真實(shí)值傳遞給 HtmlErrorRender 類來(lái)引發(fā)調(diào)試標(biāo)志。更正后的行如下所示:
new HtmlErrorRenderer(true);
這是我現(xiàn)在用于 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>

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
測(cè)試答案后,我得到一些錯(cuò)誤“解析到異常類”,所以如果你使用 Laravel 7,你需要使用“create”的“createFromThrowable”代替“create”來(lái)與 Throwable 對(duì)象兼容。

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
要獲得完整的 html 響應(yīng),只需使用:
$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);
}
}
}
- 3 回答
- 0 關(guān)注
- 149 瀏覽
添加回答
舉報(bào)