Skip to main content
added 2 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

This depends on the exceptions you want to catch.

Example for 5xx HTTP exceptions:

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
 * Exception subscriber for handling 5xx exceptions
 */
class MymoduleExceptionSubscriber extends DefaultExceptionHtmlSubscriber {

  protected static function getPriority() {
    return -129;
  }

  protected function getHandledFormats() {
    return ['html'];
  }

  /**
   * Handles a 5xx error for HTML.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function on5xx(ExceptionEvent $event) {
    if (($exception = $event->getThrowable()) && $exception instanceof HttpExceptionInterface) {
      $this->makeSubrequest($event, '/my5xxcontroller', $exception->getStatusCode());
    }
  }

}

Example for all exceptions (only for demonstration purpose, better check for the exceptions you want to catch):

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
 * Exception subscriber for handling all exceptions
 */
class MymoduleExceptionSubscriber extends DefaultExceptionHtmlSubscriber {

  protected static function getPriority() {
    return -129;
  }

  protected function getHandledFormats() {
    return ['html'];
  }

  /**
   * Handles all exceptions for HTML
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function onException(ExceptionEvent $event) {
    $request = $event->getRequest();
    $handled_formats = $this->getHandledFormats();
    $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat());
    if (empty($handled_formats) || in_array($format, $handled_formats)) {
      $this->makeSubrequest($event, '/my5xxcontroller', 500);
    }
  }

}

mymodule.services.yml

services:
  mymodule.5xx_subscriber:
    class: Drupal\mymodule\EventSubscriber\MymoduleExceptionSubscriber
    tags:
      - { name: event_subscriber }
    arguments: ['@http_kernel', '@logger.channel.php', '@redirect.destination', '@router.no_access_checks']

This depends on the exceptions you want to catch.

Example for 5xx HTTP exceptions:

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
 * Exception subscriber for handling 5xx exceptions
 */
class MymoduleExceptionSubscriber extends DefaultExceptionHtmlSubscriber {

  protected static function getPriority() {
    return 129;
  }

  protected function getHandledFormats() {
    return ['html'];
  }

  /**
   * Handles a 5xx error for HTML.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function on5xx(ExceptionEvent $event) {
    if (($exception = $event->getThrowable()) && $exception instanceof HttpExceptionInterface) {
      $this->makeSubrequest($event, '/my5xxcontroller', $exception->getStatusCode());
    }
  }

}

Example for all exceptions (only for demonstration purpose, better check for the exceptions you want to catch):

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
 * Exception subscriber for handling all exceptions
 */
class MymoduleExceptionSubscriber extends DefaultExceptionHtmlSubscriber {

  protected static function getPriority() {
    return 129;
  }

  protected function getHandledFormats() {
    return ['html'];
  }

  /**
   * Handles all exceptions for HTML
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function onException(ExceptionEvent $event) {
    $request = $event->getRequest();
    $handled_formats = $this->getHandledFormats();
    $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat());
    if (empty($handled_formats) || in_array($format, $handled_formats)) {
      $this->makeSubrequest($event, '/my5xxcontroller', 500);
    }
  }

}

mymodule.services.yml

services:
  mymodule.5xx_subscriber:
    class: Drupal\mymodule\EventSubscriber\MymoduleExceptionSubscriber
    tags:
      - { name: event_subscriber }
    arguments: ['@http_kernel', '@logger.channel.php', '@redirect.destination', '@router.no_access_checks']

This depends on the exceptions you want to catch.

Example for 5xx HTTP exceptions:

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
 * Exception subscriber for handling 5xx exceptions
 */
class MymoduleExceptionSubscriber extends DefaultExceptionHtmlSubscriber {

  protected static function getPriority() {
    return -129;
  }

  protected function getHandledFormats() {
    return ['html'];
  }

  /**
   * Handles a 5xx error for HTML.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function on5xx(ExceptionEvent $event) {
    if (($exception = $event->getThrowable()) && $exception instanceof HttpExceptionInterface) {
      $this->makeSubrequest($event, '/my5xxcontroller', $exception->getStatusCode());
    }
  }

}

Example for all exceptions (only for demonstration purpose, better check for the exceptions you want to catch):

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
 * Exception subscriber for handling all exceptions
 */
class MymoduleExceptionSubscriber extends DefaultExceptionHtmlSubscriber {

  protected static function getPriority() {
    return -129;
  }

  protected function getHandledFormats() {
    return ['html'];
  }

  /**
   * Handles all exceptions for HTML
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function onException(ExceptionEvent $event) {
    $request = $event->getRequest();
    $handled_formats = $this->getHandledFormats();
    $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat());
    if (empty($handled_formats) || in_array($format, $handled_formats)) {
      $this->makeSubrequest($event, '/my5xxcontroller', 500);
    }
  }

}

mymodule.services.yml

services:
  mymodule.5xx_subscriber:
    class: Drupal\mymodule\EventSubscriber\MymoduleExceptionSubscriber
    tags:
      - { name: event_subscriber }
    arguments: ['@http_kernel', '@logger.channel.php', '@redirect.destination', '@router.no_access_checks']
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

This depends on the exceptions you want to catch.

Example for 5xx HTTP exceptions:

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
 * Exception subscriber for handling 5xx exceptions
 */
class MymoduleExceptionSubscriber extends DefaultExceptionHtmlSubscriber {

  protected static function getPriority() {
    return 129;
  }

  protected function getHandledFormats() {
    return ['html'];
  }

  /**
   * Handles a 5xx error for HTML.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function on5xx(ExceptionEvent $event) {
    if (($exception = $event->getThrowable()) && $exception instanceof HttpExceptionInterface) {
      $this->makeSubrequest($event, '/my5xxcontroller', $exception->getStatusCode());
    }
  }

}

Example for all exceptions (only for demonstration purpose, better check for the exceptions you want to catch):

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
 * Exception subscriber for handling all exceptions
 */
class MymoduleExceptionSubscriber extends DefaultExceptionHtmlSubscriber {

  protected static function getPriority() {
    return 129;
  }

  protected function getHandledFormats() {
    return ['html'];
  }

  /**
   * Handles all exceptions for HTML
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function onException(ExceptionEvent $event) {
    $request = $event->getRequest();
    $handled_formats = $this->getHandledFormats();
    $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat());
    if (empty($handled_formats) || in_array($format, $handled_formats)) {
      $this->makeSubrequest($event, '/my5xxcontroller', 500);
    }
  }

}

mymodule.services.yml

services:
  mymodule.5xx_subscriber:
    class: Drupal\mymodule\EventSubscriber\MymoduleExceptionSubscriber
    tags:
      - { name: event_subscriber }
    arguments: ['@http_kernel', '@logger.channel.php', '@redirect.destination', '@router.no_access_checks']