Laravel 7 исключений электронной почты прервались после Symfony 5 обновления - PullRequest
3 голосов
/ 14 марта 2020

Я обновился до 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);

, который работает, но теперь вместо получения содержимого отладки в В электронном письме, которое я использовал раньше, вместо этого я получаю более простое c сообщение об ошибке, так как оно предназначено для публикации c.

Примеры различных форматов вы можете увидеть здесь: https://symfony.com/doc/current/controller/error_pages.html

Я уверен, что есть кое-что простое, что мне не хватает, но я еще не выяснил, как заставить его отправить мне подробные данные об исключениях, которые я использовал до обновление.

Есть предложения?

Ответы [ 2 ]

1 голос
/ 18 марта 2020

Ниже приведен код, который я использовал для получения желаемых результатов в электронном письме с уведомлением об исключении. Основной элемент, который я пропустил ранее, это то, что я не передавал истинное значение в класс HtmlErrorRender, чтобы поднять флаг отладки. Исправленная строка показана здесь:

new HtmlErrorRenderer(true);

Вот полный код, который я сейчас использую для приложения / Исключения / Обработчик. php file

<?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>
0 голосов
/ 16 марта 2020

Чтобы получить полный ответ как html, просто используйте:

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

Вот полный обработчик. 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);
        }
    }

}
...