Ошибка 404 symfony

Edit this page

How to Customize Error Pages

In Symfony applications, all errors are treated as exceptions, no matter if they
are a 404 Not Found error or a fatal error triggered by throwing some exception
in your code.

In the development environment,
Symfony catches all the exceptions and displays a special exception page
with lots of debug information to help you discover the root problem:

A typical exception page in the development environment

Since these pages contain a lot of sensitive internal information, Symfony won’t
display them in the production environment. Instead, it’ll show a minimal and
generic error page:

A typical error page in the production environment

Error pages for the production environment can be customized in different ways
depending on your needs:

  1. If you only want to change the contents and styles of the error pages to match
    the rest of your application, override the default error templates;
  2. If you want to change the contents of non-HTML error output,
    create a new normalizer;
  3. If you also want to tweak the logic used by Symfony to generate error pages,
    override the default error controller;
  4. If you need total control of exception handling to run your own logic
    use the kernel.exception event.

Overriding the Default Error Templates

You can use the built-in Twig error renderer to override the default error
templates. Both the TwigBundle and TwigBridge need to be installed for this. Run
this command to ensure both are installed:

When the error page loads, TwigErrorRenderer
is used to render a Twig template to show the user.

This renderer uses the HTTP status code and the following
logic to determine the template filename:

  1. Look for a template for the given status code (like error500.html.twig);
  2. If the previous template doesn’t exist, discard the status code and look for
    a generic error template (error.html.twig).

To override these templates, rely on the standard Symfony method for
overriding templates that live inside a bundle and
put them in the templates/bundles/TwigBundle/Exception/ directory.

A typical project that returns HTML pages might look like this:

Example 404 Error Template

To override the 404 error template for HTML pages, create a new
error404.html.twig template located at templates/bundles/TwigBundle/Exception/:

In case you need them, the TwigErrorRenderer passes some information to
the error template via the status_code and status_text variables that
store the HTTP status code and message respectively.

Tip

You can customize the status code of an exception by implementing
HttpExceptionInterface
and its required getStatusCode() method. Otherwise, the status_code
will default to 500.

Additionally you have access to the HttpException
object via the exception Twig variable. For example, if the exception sets a
message (e.g. using throw $this->createNotFoundException('The product does not exist')),
use {{ exception.message }} to print that message. You can also output the
stack trace using {{ exception.traceAsString }}, but don’t do that for end
users because the trace contains sensitive data.

Tip

PHP errors are turned into exceptions as well by default, so you can also
access these error details using exception.

Security & 404 Pages

Due to the order of how routing and security are loaded, security information will
not be available on your 404 pages. This means that it will appear as if your
user is logged out on the 404 page (it will work while testing, but not on production).

Testing Error Pages during Development

While you’re in the development environment, Symfony shows the big exception
page instead of your shiny new customized error page. So, how can you see
what it looks like and debug it?

Fortunately, the default ErrorController allows you to preview your
error pages during development.

To use this feature, you need to load some special routes provided by FrameworkBundle
(if the application uses Symfony Flex they are loaded
automatically when installing symfony/framework-bundle):

With this route added, you can use URLs like these to preview the error page
for a given status code as HTML or for a given status code and format (you might
need to replace http://localhost/ by the host used in your local setup):

  • http://localhost/_error/{statusCode} for HTML
  • http://localhost/_error/{statusCode}.{format} for any other format

Overriding Error output for non-HTML formats

To override non-HTML error output, the Serializer component needs to be installed.

The Serializer component has a built-in FlattenException normalizer
(ProblemNormalizer) and
JSON/XML/CSV/YAML encoders. When your application throws an exception, Symfony
can output it in one of those formats. If you want to change the output
contents, create a new Normalizer that supports the FlattenException input:

Overriding the Default ErrorController

If you need a little more flexibility beyond just overriding the template,
then you can change the controller that renders the error page. For example,
you might need to pass some additional variables into your template.

To do this, create a new controller anywhere in your application and set
the framework.error_controller
configuration option to point to it:

The ErrorListener
class used by the FrameworkBundle as a listener of the kernel.exception event creates
the request that will be dispatched to your controller. In addition, your controller
will be passed two parameters:

exception
The original Throwable instance being handled.
logger
A DebugLoggerInterface
instance which may be null in some circumstances.

Working with the kernel.exception Event

When an exception is thrown, the HttpKernel
class catches it and dispatches a kernel.exception event. This gives you the
power to convert the exception into a Response in a few different ways.

Working with this event is actually much more powerful than what has been explained
before, but also requires a thorough understanding of Symfony internals. Suppose
that your code throws specialized exceptions with a particular meaning to your
application domain.

Writing your own event listener
for the kernel.exception event allows you to have a closer look at the exception
and take different actions depending on it. Those actions might include logging
the exception, redirecting the user to another page or rendering specialized
error pages.

Note

If your listener calls setThrowable() on the
ExceptionEvent
event, propagation will be stopped and the response will be sent to
the client.

This approach allows you to create centralized and layered error handling:
instead of catching (and handling) the same exceptions in various controllers
time and again, you can have just one (or several) listeners deal with them.

Tip

See ExceptionListener
class code for a real example of an advanced listener of this type. This
listener handles various security-related exceptions that are thrown in
your application (like AccessDeniedException)
and takes measures like redirecting the user to the login page, logging them
out and other things.

Как настроить страницы ошибок

Дата обновления перевода: 2023-01-18

Как настроить страницы ошибок

В приложениях Symfony все ошибки воспринимаются, как исключения, вне зависимости
от того, являются ли они простой ошибкой 404 «Не найдено» или фатальной ошибкой,
запущенной вызовом какого-то исключения в вашем коде.

В окружении разработки, Symfony ловит все
исключения и отображаеть специальную страницу исключений со множеством информации
по отладке, чтобы помочь вам быстро обнаружить основную проблему:

Типичная страница исключений в окружении разработки

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

Типичная страница ошибки в окружении производства

Страницы ошибок в окружении производства можно настроить разными способами, в
зависимости от ваших потребностей:

  1. Если вы просто хотите изменить содержание и стили страниц ошибок так, чтобы
    они совпадали с остальным вашим приложением,
    переопределите шаблоны ошибок по умолчанию ;
  2. Если вы хотите изменить содержание вывода ошибки не в HTML,
    создайте новый нормализатор ;
  3. Если вы также хотите настроить логику, используемую Symfony для генерирования ваших
    страниц ошибок, то переопределите контроллер ошибок по умолчанию ;
  4. Если вам нужен полный контроль над работой с исключениями, выполните вашу
    собственную логику — используйте событие the kernel.exception .

Переопределение шаблонов ошибок по умолчанию

Вы можете использовать встроенное средство отображения ошибок Twig, чтобы переопределять
шаблоны ошибок по умолчанию. Для этого должны быть установлены как TwigBundle, так и TwigBridge.
Выполните эту команду, чтобы убедиться, что они оба установлены:

Когда загружается страница ошибки, для отображения шаблона twig и демонастрации
пользователю, используется TwigErrorRenderer.

Этот отображатель использует статус-код HTTP, формат запроса и следующую логику,
чтобы определить имя файла шаблона:

  1. Ищет шаблон для заданного статус-кода (вроде error500.html.twig);
  2. Если предыдущий шаблон не существует, отбросьте статус-код и ищет общий
    шаблон ошибок (error.html.twig).

Чтобы переопределить эти шаблоны, просто положитесь на стандартный метод Symfony
для пеоепределения шаблонов, живущих внутри пакета:
поместите их в каталоге templates/bundles/TwigBundle/Exception/.

Типичный проект, возвращающий страницы HTML и JSON, может выглядеть так:

Пример шаблона ошибки 404

Чтобы переопределить шаблон ошибки 404 для HTML-страниц, создайте новый шаблон
error404.html.twig, находящийся в templates/bundles/TwigBundle/Exception/:

Если они вам понадобятся, TwigErrorRenderer передаёт некоторую информацию в шаблон
ошибок через переменные status_code и status_text, которые хранят HTTP статус-код
и сообщение соотвественно.

Tip

Вы можете настроить статус-код, реализовав
HttpExceptionInterface
и его обязательный метод getStatusCode(). В обратном случае, status_code
по умолчанию будет500.

Кроме этого у вас есть доступ к Исключениям с помощью exception, который, к примеру,
позволяет вам выводить отслеживание стека, используя {{ exception.traceAsString }}, или
получить доступ к любому другому методу объекта. Однако будьте аккуратны, так как это
с большой вероятностью может обнажить кофиденциальную информацию.

Tip

PHP-ошибки также по умолчанию превращаются в исключения, поэтому вы также можете
получить доступ к деталям этих ошибок, используя exception.

Безопасность и страницы 404

В связи с порядком, в котором загружаются маршрутизация и безопасность, конфиденциальна информация
не будет доступна на ваших страницах 404. Это означает, что будет казаться, что ваш пользователь
не залогинен в систему на странице 404 (это будет работать при тестировании, но не в производстве).

Тестирование страниц ошибок во время разработки

В то время, как вы находитесь в окружении разработки, Symfony отображает
большую страницу исключений вместо вашей новой блестящей страницы ошибок.
Так как вам увидеть, как она выглядит изнутри и отладить её?

К счастью, ExceptionController по умолчанию разрешает вам предпросмотр
вашей страницы ошибки во время разработки.

Чтобы использовать эту функцию, вам загрузить специальные маршруты, предоставленные
TwigBundle (если приложение использует Symfony Flex, то они
загружаются автоматически при установке symfony/framework-bundle):

  • YAML
  • XML
  • PHP

C добавлением этого маршрута, вы можете использовать такие URL для предпросмотра
страницы ошибки для заданного статус-кода в виде HTML или для заданного статус-кода
и формата (вам может понадобиться заменить http://localhost/ на хостинг, используемый
в ваших локальных настройках):

  • http://localhost/_error/{statusCode} для HTML
  • http://localhost/_error/{statusCode}.{format} для любого другого формата

Переопределение вывода ошибок для не-HTML форматов

Чтобы переопределить не-HTML вывод ошибки, необходимо установить компонент Serializer.

Компонент Serializer имеет встроенный нормализатор FlattenException
(ProblemNormalizer) и кодировщики
JSON/XML/CSV/YAML. Когда ваше приложение вызывает исключение, Symfony может вывести его
в один из этих форматов. Если вы хоите изменить содержание вывода, создайте Нормализатор,
который поддерживает ввод FlattenException:

Переопределение ExceptionController по умолчаию

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

Чтобы сделать это, просто создайте новый контролер где угодно в вашем приложении,
и установите опцию конфигурации framework.error_controller ,
чтобы указать на неё:

  • YAML
  • XML
  • PHP

Класс ExceptionListener,
используемый TwigBundle в качестве слушателя события kernel.exception, создаёт
запрос, который будет развёрнут в вашем контроллере. В дополнение, вашему контроллеру
будут переданы два параметра:

exception
Обработка первоначального экземпляра Throwable.
logger
Экемпляр DebugLoggerInterface,
который может в некоторых случаях быть null.

Tip

Предпросмотр страницы ошибки также работает
с вашими собственными контроллерами, нвстроенными как угодно.

Работа с событием kernel.exception

Когда вызывается исключение, класс HttpKernel
ловит его и развёртывает событие kernel.exception. Это даёт вам возможность
конвертировать исключение в Response несколькими разными способами.

Работа с этим событием на самом деле значительн более мощная, чем объяснялось раньше,
но также требует тщательного понимания внутренних процессов Symfony. Представьте, что
ваш код вызывает специальные исключения с конкретным значением в вашем домене приложения.

Написание вашего собственного слушателя событий для события
kernel.exception позволяет вам ближе рассмотретьисключение и предпринять по отношению
к нему разные действия. Эти действия могут включать в себя запись лога исключения,
перенаправление пользователя на другую страницу или отображение специальных страниц ошибки.

Note

Если ваш слушатель вызывает setResponse() в событии
ExceptionEvent,
распространение будет остановлено и клиенту будет отправлен ответ.

Этот подход позволяет вам создавать централизованную и многослойную обработку
ошибок: вместо того, чтобы ловить (и обрабатывать) одни и те же исключения в
разных контроллерах снова и снова, вы просто можете иметь одного (или нескольких)
слушателей, чтобы разбираться с ними.

Tip

Смотрите код класса ExceptionListener
для реального примера продвинутого слушателя такого типа. Этот слушатель обрабатывает
различные исключения, связанные с безопасностью, которые вызываются в вашем приложении
(как AccessDeniedException) и
предпринимает действия вроде перенаправления пользователей на страницу входа, выполняет
их вход в систему и др.

How to Customize Error Pages

In Symfony applications, all errors are treated as exceptions, no matter if they
are a 404 Not Found error or a fatal error triggered by throwing some exception
in your code.

In the :ref:`development environment <configuration-environments>`,
Symfony catches all the exceptions and displays a special exception page
with lots of debug information to help you discover the root problem:

A typical exception page in the development environment

Since these pages contain a lot of sensitive internal information, Symfony won’t
display them in the production environment. Instead, it’ll show a minimal and
generic error page:

A typical error page in the production environment

Error pages for the production environment can be customized in different ways
depending on your needs:

  1. If you only want to change the contents and styles of the error pages to match
    the rest of your application, :ref:`override the default error templates <use-default-error-controller>`;
  2. If you want to change the contents of non-HTML error output,
    :ref:`create a new normalizer <overriding-non-html-error-output>`;
  3. If you also want to tweak the logic used by Symfony to generate error pages,
    :ref:`override the default error controller <custom-error-controller>`;
  4. If you need total control of exception handling to run your own logic
    :ref:`use the kernel.exception event <use-kernel-exception-event>`.

Overriding the Default Error Templates

You can use the built-in Twig error renderer to override the default error
templates. Both the TwigBundle and TwigBridge need to be installed for this. Run
this command to ensure both are installed:

$ composer require symfony/twig-pack

When the error page loads, :class:`Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer`
is used to render a Twig template to show the user.

This renderer uses the HTTP status code and the following
logic to determine the template filename:

  1. Look for a template for the given status code (like error500.html.twig);
  2. If the previous template doesn’t exist, discard the status code and look for
    a generic error template (error.html.twig).

To override these templates, rely on the standard Symfony method for
:ref:`overriding templates that live inside a bundle <override-templates>` and
put them in the templates/bundles/TwigBundle/Exception/ directory.

A typical project that returns HTML pages might look like this:

templates/
└─ bundles/
   └─ TwigBundle/
      └─ Exception/
         ├─ error404.html.twig
         ├─ error403.html.twig
         └─ error.html.twig      # All other HTML errors (including 500)

Example 404 Error Template

To override the 404 error template for HTML pages, create a new
error404.html.twig template located at templates/bundles/TwigBundle/Exception/:

{# templates/bundles/TwigBundle/Exception/error404.html.twig #}
{% extends 'base.html.twig' %}

{% block body %}
    <h1>Page not found</h1>

    <p>
        The requested page couldn't be located. Checkout for any URL
        misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
    </p>
{% endblock %}

In case you need them, the TwigErrorRenderer passes some information to
the error template via the status_code and status_text variables that
store the HTTP status code and message respectively.

Tip

You can customize the status code of an exception by implementing
:class:`Symfony\Component\HttpKernel\Exception\HttpExceptionInterface`
and its required getStatusCode() method. Otherwise, the status_code
will default to 500.

Additionally you have access to the :class:`Symfony\Component\HttpKernel\Exception\HttpException`
object via the exception Twig variable. For example, if the exception sets a
message (e.g. using throw $this->createNotFoundException('The product does not exist')),
use {{ exception.message }} to print that message. You can also output the
stack trace using {{ exception.traceAsString }}, but don’t do that for end
users because the trace contains sensitive data.

Tip

PHP errors are turned into exceptions as well by default, so you can also
access these error details using exception.

Security & 404 Pages

Due to the order of how routing and security are loaded, security information will
not be available on your 404 pages. This means that it will appear as if your
user is logged out on the 404 page (it will work while testing, but not on production).

Testing Error Pages during Development

While you’re in the development environment, Symfony shows the big exception
page instead of your shiny new customized error page. So, how can you see
what it looks like and debug it?

Fortunately, the default ErrorController allows you to preview your
error pages during development.

To use this feature, you need to load some special routes provided by FrameworkBundle
(if the application uses :ref:`Symfony Flex <symfony-flex>` they are loaded
automatically when installing symfony/framework-bundle):

.. configuration-block::

    .. code-block:: yaml

        # config/routes/framework.yaml
        when@dev:
            _errors:
                resource: '@FrameworkBundle/Resources/config/routing/errors.xml'
                prefix:   /_error

    .. code-block:: xml

        <!-- config/routes/framework.xml -->
        <?xml version="1.0" encoding="UTF-8" ?>
        <routes xmlns="http://symfony.com/schema/routing"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://symfony.com/schema/routing
                https://symfony.com/schema/routing/routing-1.0.xsd">

            <when env="dev">
                <import resource="@FrameworkBundle/Resources/config/routing/errors.xml" prefix="/_error"/>
            </when>
        </routes>

    .. code-block:: php

        // config/routes/framework.php
        use SymfonyComponentRoutingLoaderConfiguratorRoutingConfigurator;

        return function (RoutingConfigurator $routes) {
            if ('dev' === $routes->env()) {
                $routes->import('@FrameworkBundle/Resources/config/routing/errors.xml')
                    ->prefix('/_error')
                ;
            }
        };

With this route added, you can use URLs like these to preview the error page
for a given status code as HTML or for a given status code and format (you might
need to replace http://localhost/ by the host used in your local setup):

  • http://localhost/_error/{statusCode} for HTML
  • http://localhost/_error/{statusCode}.{format} for any other format

Overriding Error output for non-HTML formats

To override non-HTML error output, the Serializer component needs to be installed.

$ composer require symfony/serializer-pack

The Serializer component has a built-in FlattenException normalizer
(:class:`Symfony\Component\Serializer\Normalizer\ProblemNormalizer`) and
JSON/XML/CSV/YAML encoders. When your application throws an exception, Symfony
can output it in one of those formats. If you want to change the output
contents, create a new Normalizer that supports the FlattenException input:

# src/Serializer/MyCustomProblemNormalizer.php
namespace AppSerializer;

use SymfonyComponentErrorHandlerExceptionFlattenException;
use SymfonyComponentSerializerNormalizerNormalizerInterface;

class MyCustomProblemNormalizer implements NormalizerInterface
{
    public function normalize($exception, string $format = null, array $context = []): array
    {
        return [
            'content' => 'This is my custom problem normalizer.',
            'exception'=> [
                'message' => $exception->getMessage(),
                'code' => $exception->getStatusCode(),
            ],
        ];
    }

    public function supportsNormalization($data, string $format = null, array $context = []): bool
    {
        return $data instanceof FlattenException;
    }
}

Overriding the Default ErrorController

If you need a little more flexibility beyond just overriding the template,
then you can change the controller that renders the error page. For example,
you might need to pass some additional variables into your template.

To do this, create a new controller anywhere in your application and set
the :ref:`framework.error_controller <config-framework-error_controller>`
configuration option to point to it:

.. configuration-block::

    .. code-block:: yaml

        # config/packages/framework.yaml
        framework:
            error_controller: AppControllerErrorController::show

    .. code-block:: xml

        <!-- config/packages/framework.xml -->
        <?xml version="1.0" encoding="UTF-8" ?>
        <container xmlns="http://symfony.com/schema/dic/services"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://symfony.com/schema/dic/services
                https://symfony.com/schema/dic/services/services-1.0.xsd">

            <framework:config>
                <framework:error-controller>AppControllerErrorController::show</framework:error-controller>
            </framework:config>

        </container>

    .. code-block:: php

        // config/packages/framework.php
        use SymfonyConfigFrameworkConfig;

        return static function (FrameworkConfig $framework): void {
            // ...
            $framework->errorController('AppControllerErrorController::show');
        };

The :class:`Symfony\Component\HttpKernel\EventListener\ErrorListener`
class used by the FrameworkBundle as a listener of the kernel.exception event creates
the request that will be dispatched to your controller. In addition, your controller
will be passed two parameters:

exception
The original :phpclass:`Throwable` instance being handled.
logger
A :class:`\Symfony\Component\HttpKernel\Log\DebugLoggerInterface`
instance which may be null in some circumstances.

Tip

The :ref:`error page preview <testing-error-pages>` also works for
your own controllers set up this way.

Working with the kernel.exception Event

When an exception is thrown, the :class:`Symfony\Component\HttpKernel\HttpKernel`
class catches it and dispatches a kernel.exception event. This gives you the
power to convert the exception into a Response in a few different ways.

Working with this event is actually much more powerful than what has been explained
before, but also requires a thorough understanding of Symfony internals. Suppose
that your code throws specialized exceptions with a particular meaning to your
application domain.

:doc:`Writing your own event listener </event_dispatcher>`
for the kernel.exception event allows you to have a closer look at the exception
and take different actions depending on it. Those actions might include logging
the exception, redirecting the user to another page or rendering specialized
error pages.

Note

If your listener calls setThrowable() on the
:class:`Symfony\Component\HttpKernel\Event\ExceptionEvent`
event, propagation will be stopped and the response will be sent to
the client.

This approach allows you to create centralized and layered error handling:
instead of catching (and handling) the same exceptions in various controllers
time and again, you can have just one (or several) listeners deal with them.

Tip

See :class:`Symfony\Component\Security\Http\Firewall\ExceptionListener`
class code for a real example of an advanced listener of this type. This
listener handles various security-related exceptions that are thrown in
your application (like :class:`Symfony\Component\Security\Core\Exception\AccessDeniedException`)
and takes measures like redirecting the user to the login page, logging them
out and other things.

В приложениях Symfony все ошибки рассматриваются как исключения Exception, независимо от того, являются ли они просто ошибкой 404 Not Found или фатальной ошибкой, вызванной возникновением некоторого исключения в вашем коде.

В среде разработки Symfony перехватывает все исключения и отображает специальную страницу исключений со множеством отладочной информации, которая поможет вам обнаружить корневую проблему:

Поскольку такие страницы содержат много конфиденциальной внутренней информации, Symfony не будет отображать их в production среде. Вместо этого он покажет простую и общую страницу ошибок:

Страницы ошибок для production среды могут быть настроены по-разному в зависимости от ваших потребностей:

  1. Если вы просто хотите изменить содержимое и стили страниц ошибок, чтобы они соответствовали остальной части вашего приложения, переопределите шаблоны ошибок по умолчанию;
  2. Если вы хотите изменить содержимое вывода ошибок, отличных от HTML, создайте новый нормализатор;
  3. Если вы также хотите настроить логику, используемую Symfony для генерации страниц ошибок, переопределите контроллер ошибок по умолчанию;
  4. Если вам нужен полный контроль над обработкой исключений для выполнения вашей собственной логики, используйте событие kernel.exception.

Как переопределить шаблон страницы ошибок по умолчанию?

Вы можете использовать встроенный обработчик ошибок Twig, чтобы переопределить шаблоны ошибок по умолчанию. Для этого должны быть установлены как TwigBundle, так и TwigBridge. Запустите эту команду, чтобы убедиться, что оба установлены:

Когда страница ошибки загружается, TwigErrorRenderer используется для визуализации шаблона Twig для отображения пользователю.

Этот рендерер использует код состояния HTTP и следующую логику для определения имени файла шаблона:

  1. Находится шаблон для данного кода состояния (например, error500.html.twig);
  2. Если предыдущий шаблон не существует, то игнорируется код состояния и находится общий шаблон ошибки (error.html.twig).

Чтобы переопределить эти шаблоны, используйте стандартный метод Symfony для переопределения шаблонов, которые находятся внутри пакета, и поместите их в каталог templates/bundles/TwigBundle/Exception/.

Типичный проект, который возвращает HTML-страницы, может выглядеть так:

templates/
└─ bundles/
   └─ TwigBundle/
      └─ Exception/
         ├─ error404.html.twig
         ├─ error403.html.twig
         └─ error.html.twig      # All other HTML errors (including 500)

Пример 404 Шаблона ошибки

Чтобы переопределить шаблон ошибки 404 для страниц HTML, создайте новый шаблон error404.html.twig, расположенный в шаблонах /bundles/TwigBundle/Exception /:

{# templates/bundles/TwigBundle/Exception/error404.html.twig #}
{% extends 'base.html.twig' %}

{% block body %}
    <h1>Page not found</h1>

    <p>
        The requested page couldn't be located. Checkout for any URL
        misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
    </p>
{% endblock %}

Если вам потребуется, TwigErrorRenderer передает некоторую информацию в шаблон ошибки через переменные status_code и status_text, которые хранят код состояния HTTP и сообщение соответственно.

Вы можете настроить код состояния исключения, реализовав HttpExceptionInterface и его обязательный метод getStatusCode(). В противном случае код_состояния по умолчанию будет равен 500.

Тестирование страниц ошибок в режиме develop.

Пока вы находитесь в среде develop, Symfony показывает большую страницу исключений вместо вашей новой блестящей настраиваемой страницы ошибок. Итак, как вы можете увидеть, как это выглядит и отладить его?

К счастью, ErrorController по умолчанию позволяет вам просматривать страницы ошибок во время разработки.

Чтобы использовать эту функцию, вам нужно загрузить некоторые специальные маршруты, предоставляемые FrameworkBundle (если приложение использует Symfony Flex, они загружаются автоматически при установке symfony/framework-bundle):

# config/routes/dev/framework.yaml
_errors:
    resource: '@FrameworkBundle/Resources/config/routing/errors.xml'
    prefix:   /_error

После добавления этого маршрута вы можете использовать подобные URL-адреса для предварительного просмотра страницы ошибки для заданного кода состояния в виде HTML или для заданного кода состояния и формата.

http://localhost/index.php/_error/{statusCode}
http://localhost/index.php/_error/{statusCode}.{format}

Переопределение вывода ошибок для не-HTML форматов.

Чтобы переопределить вывод ошибок, в формате отличном от HTML, необходимо установить компонент Serializer.

 composer require serializer

Компонент Serializer имеет встроенный нормализатор FlattenException (ProblemNormalizer) и енкодеры JSON / XML / CSV / YAML. Когда ваше приложение выдает исключение, Symfony может вывести его в одном из этих форматов. Если вы хотите изменить содержимое вывода, создайте новый нормализатор, который поддерживает вход FlattenException:

# src/App/Serializer/MyCustomProblemNormalizer.php
namespace AppSerializer;

use SymfonyComponentSerializerNormalizerNormalizerInterface;

class MyCustomProblemNormalizer implements NormalizerInterface
{
    public function normalize($exception, string $format = null, array $context = [])
    {
        return [
            'content' => 'This is my custom problem normalizer.',
            'exception'=> [
                'message' => $exception->getMessage(),
                'code' => $exception->getStatusCode(),
            ],
        ];
    }

    public function supportsNormalization($data, string $format = null)
    {
        return $data instanceof FlattenException;
    }
}

Переопределение стандартного ErrorController.

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

Для этого создайте новый контроллер в любом месте вашего приложения и установите параметр конфигурации framework.error_controller, чтобы он указывал на него:

# config/packages/framework.yaml
framework:
    error_controller: AppControllerErrorController::showAction

Класс ErrorListener, используемый FrameworkBundle в качестве прослушивателя события kernel.exception, создает запрос, который будет отправлен вашему контроллеру. Кроме того, вашему контроллеру будут переданы два параметра:

  • exception — Экземпляр FlattenException, созданный из обрабатываемого исключения.
  • logger — Экземпляр DebugLoggerInterface, который может быть null в некоторых случаях.

Вместо создания нового контроллера исключений с нуля вы также можете расширить ExceptionController по умолчанию. В этом случае вы можете переопределить один или оба метода showAction() и findTemplate(). Последний находит шаблон для использования.

В случае расширения ExceptionController вы можете настроить сервис для передачи окружения Twig и флага отладки в конструктор.

# config/services.yaml
services:
    _defaults:
        # ... be sure autowiring is enabled
        autowire: true
    # ...

    AppControllerCustomExceptionController:
        public: true
        arguments:
            $debug: '%kernel.debug%'

Работа с событием kernel.exception

Когда бросается exception, класс HttpKernel перехватывает его и отправляет событие kernel.exception. Это дает вам возможность преобразовать исключение в ответ несколькими различными способами.

Работа с этим событием на самом деле намного эффективнее, чем было объяснено ранее, но также требует глубокого понимания внутренних возможностей Symfony. Предположим, что ваш код выдает специализированные исключения с особым значением в область вашего приложения.

Написание собственного слушателя событий для события kernel.exception позволяет вам ближе рассмотреть исключение и предпринять различные действия в зависимости от него. Эти действия могут включать логирование исключения, редирект пользователя на другую страницу или отображение специализированных страниц с ошибками.

Если ваш слушатель вызывает setResponse() для ExceptionEvent, событие, дальнейшая обработка другими слушателями будет остановлено, и ответ будет отправлен клиенту.

Этот подход позволяет вам создавать централизованную и многоуровневую обработку ошибок: вместо того, чтобы снова и снова перехватывать (и обрабатывать) одни и те же исключения в различных контроллерах, вы можете иметь дело только с одним (или несколькими) слушателями.

With a Subscription, click any sentence in the script to jump to that part of the video!

Login
Subscribe

What should the structure of a 404 response from our API look like? It’s obvious: we’ll want to return that same API Problem JSON response format. We want to return this whenever anything goes wrong.

Planning the Response

Start by planning out how the 404 should look with a new test method — test404Exception. Let’s make a GET request to /api/programmers/fake and assert the easy part: that the status code is 404. We also know that we want the nice application/problem+json Content-Type header, so assert that too:


… lines 1 — 5
class ProgrammerControllerTest extends ApiTestCase
{

… lines 8 — 165
public function test404Exception()
{
$response = $this->client->get(‘/api/programmers/fake’);
$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals(‘application/problem+json’, $response->getHeader(‘Content-Type’));

… lines 172 — 173
}
}

We know the JSON will at least have type and title properties. So what would be good values for those? This is a weird situation. Usually, type conveys what happened. But in this case, the 404 status code already says everything we need to. Using some type value like not_found would be fine, but totally redundant.

Look back at the Problem Details Spec. Under «Pre-Defined Problem Types», it says that if the status code is enough, you can set type to about:blank. And when you do this, it says that we should set title to whatever the standard text is for that status code. A 404 would be «Not Found».

Add this to the test: use $this->asserter()->assertResponsePropertyEquals() to assert that type is about:blank. And do this all again to assert that title is Not Found:


… lines 1 — 165
public function test404Exception()
{
$response = $this->client->get(‘/api/programmers/fake’);
$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals(‘application/problem+json’, $response->getHeader(‘Content-Type’));
$this->asserter()->assertResponsePropertyEquals($response, ‘type’, ‘about:blank’);
$this->asserter()->assertResponsePropertyEquals($response, ‘title’, ‘Not Found’);
}

… lines 175 — 176

How 404’s Work

A 404 happens whenever we call $this->createNotFoundException() in a controller. If you hold cmd or ctrl and click that method, you’ll see that this is just a shortcut to throw a special NotFoundHttpException. And all of the other errors that might happen will ultimately just be different exceptions being thrown from different parts of our app.

The only thing that makes this exception special is that it extends that very-important HttpException class. That’s why throwing this causes a 404 response. But otherwise, it’s equally as exciting as any other exception.

Handling all Errors

In ApiExceptionSubscriber, we’re only handling ApiException’s so far. But if we handled all exceptions, we could turn everything into the nice format we want.

Reverse the logic on the if statement and set the $apiProblem variable inside:


… lines 1 — 12
class ApiExceptionSubscriber implements EventSubscriberInterface
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$e = $event->getException();
if ($e instanceof ApiProblemException) {
$apiProblem = $e->getApiProblem();
} else {

… lines 22 — 26
}

… lines 28 — 35
}

… lines 37 — 43
}

Add an else. In all other cases, we’ll need to create the ApiProblem ourselves. The first thing we need to figure out is what status code this exception should have. Create a $statusCode variable. Here, check if $e is an instanceof HttpExceptionInterface: that special interface that lets an exception control its status code. So if it is, set the status code to $e->getStatusCode(). Otherwise, we have to assume that it’s 500:


… lines 1 — 14
public function onKernelException(GetResponseForExceptionEvent $event)
{

… lines 17 — 18
if ($e instanceof ApiProblemException) {

… line 20
} else {
$statusCode = $e instanceof HttpExceptionInterface ? $e->getStatusCode() : 500;

… lines 23 — 26
}

… lines 28 — 35
}

… lines 37 — 45

Now use this to create an ApiProblem: $apiProblem = new ApiProblem() and pass it the $statusCode:


… lines 1 — 14
public function onKernelException(GetResponseForExceptionEvent $event)
{

… lines 17 — 18
if ($e instanceof ApiProblemException) {

… line 20
} else {
$statusCode = $e instanceof HttpExceptionInterface ? $e->getStatusCode() : 500;
$apiProblem = new ApiProblem(
$statusCode
);
}

… lines 28 — 35
}

… lines 37 — 45

For the type argument, we could pass about:blank — that is what we want. But then in ApiProblem, we’ll need a constant for this, and that constant will need to be mapped to a title. But we actually want the title to be dynamic based on whatever the status code is: 404 is «Not Found», 403 is «Forbidden», etc. So, don’t pass anything for the type argument. Let’s handle all of this logic inside ApiProblem itself.

In there, start by making the $type argument optional:


… lines 1 — 8
class ApiProblem
{

… lines 11 — 26
public function __construct($statusCode, $type = null)
{

… lines 29 — 47
}

… lines 49 — 75
}

And if $type is exactly null, then set it to about:blank. Make sure the $this->type = $type assignment happens after all of this:


… lines 1 — 26
public function __construct($statusCode, $type = null)
{

… lines 29 — 30
if ($type === null) {
$type = ‘about:blank’;

… lines 35 — 43
}
$this->type = $type;

… line 47
}

… lines 49 — 77

For $title, we just need a map from the status code to its official description. Go to Navigate->Class — that’s cmd+o on a Mac. Look for Response and open the one inside HttpFoundation. It has a really handy public $statusTexts map that’s exactly what we want:


… lines 1 — 11
namespace SymfonyComponentHttpFoundation;

… lines 13 — 20
class Response

… lines 22 — 124
public static $statusTexts = array(

… lines 126 — 150
403 => ‘Forbidden’,
404 => ‘Not Found’,

… lines 153 — 185
);

… lines 187 — 1274
}

Set the $title variable — but use some if logic in case we have some weird status code for some reason. If it is in the $statusTexts array, use it. Otherwise, well, this is kind of a weird situation. Use Unknown Status Code with a frowny face:


… lines 1 — 26
public function __construct($statusCode, $type = null)
{

… lines 29 — 30
if ($type === null) {

… lines 32 — 33
$type = ‘about:blank’;
$title = isset(Response::$statusTexts[$statusCode])
? Response::$statusTexts[$statusCode]
: ‘Unknown status code :(‘;

… lines 38 — 43
}

… lines 45 — 47
}

… lines 49 — 77

If the $type is set — we’re in the normal case. Move the check up there and add $title = self::$titles[$type]. After everything, assign $this->title = $title:


… lines 1 — 26
public function __construct($statusCode, $type = null)
{

… lines 29 — 30
if ($type === null) {

… lines 32 — 37
} else {
if (!isset(self::$titles[$type])) {
throw new InvalidArgumentException(‘No title for type ‘.$type);
}
$title = self::$titles[$type];
}
$this->type = $type;
$this->title = $title;
}

… lines 49 — 77

Now the code we wrote in ApiExceptionSubscriber should work: a missing $type tells ApiProblem to use all the about:blank stuff. Time to try this: copy the test method name, then run:

./bin/phpunit -c app --filter test404Exception

Aaaand that’s green. It’s so nice when things work.

What we just did is huge. If a 404 exception is thrown anywhere in the system, it’ll map to the nice Api Problem format we want. In fact, if any exception is thrown it ends up with that format. So if your database blows, an exception is thrown. Sure, that’ll map to a 500 status code, but the JSON format will be just like every other error.

Понравилась статья? Поделить с друзьями:
  • Ошибка 404 soft
  • Ошибка 404 как обойти
  • Ошибка 404 как зайти на сайт
  • Ошибка 404 как вызвать
  • Ошибка 404 история появления