DefaultExceptionSubscriber.php 3.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
<?php

namespace Drupal\serialization\EventSubscriber;

use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Serializer\SerializerInterface;

/**
 * Handles default error responses in serialization formats.
 */
class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase {

  /**
   * The serializer.
   *
   * @var \Symfony\Component\Serializer\Serializer
   */
  protected $serializer;

  /**
   * The available serialization formats.
   *
   * @var array
   */
  protected $serializerFormats = [];

  /**
   * DefaultExceptionSubscriber constructor.
   *
   * @param \Symfony\Component\Serializer\SerializerInterface $serializer
   *   The serializer service.
   * @param array $serializer_formats
   *   The available serialization formats.
   */
  public function __construct(SerializerInterface $serializer, array $serializer_formats) {
    $this->serializer = $serializer;
    $this->serializerFormats = $serializer_formats;
  }

  /**
   * {@inheritdoc}
   */
  protected function getHandledFormats() {
    return $this->serializerFormats;
  }

  /**
   * {@inheritdoc}
   */
  protected static function getPriority() {
    // This will fire after the most common HTML handler, since HTML requests
    // are still more common than HTTP requests.
    return -75;
  }

  /**
   * Handles a 400 error for HTTP.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on400(GetResponseForExceptionEvent $event) {
    $this->setEventResponse($event, Response::HTTP_BAD_REQUEST);
  }

  /**
   * Handles a 403 error for HTTP.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on403(GetResponseForExceptionEvent $event) {
    $this->setEventResponse($event, Response::HTTP_FORBIDDEN);
  }

  /**
   * Handles a 404 error for HTTP.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on404(GetResponseForExceptionEvent $event) {
    $this->setEventResponse($event, Response::HTTP_NOT_FOUND);
  }

  /**
   * Handles a 405 error for HTTP.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on405(GetResponseForExceptionEvent $event) {
    $this->setEventResponse($event, Response::HTTP_METHOD_NOT_ALLOWED);
  }

  /**
   * Handles a 406 error for HTTP.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on406(GetResponseForExceptionEvent $event) {
    $this->setEventResponse($event, Response::HTTP_NOT_ACCEPTABLE);
  }

  /**
   * Handles a 422 error for HTTP.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on422(GetResponseForExceptionEvent $event) {
    $this->setEventResponse($event, Response::HTTP_UNPROCESSABLE_ENTITY);
  }

  /**
   * Sets the Response for the exception event.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The current exception event.
   * @param int $status
   *   The HTTP status code to set for the response.
   */
  protected function setEventResponse(GetResponseForExceptionEvent $event, $status) {
    $format = $event->getRequest()->getRequestFormat();
    $content = ['message' => $event->getException()->getMessage()];
    $encoded_content = $this->serializer->serialize($content, $format);
    $response = new Response($encoded_content, $status);
    $event->setResponse($response);
  }

}