X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fserialization%2Fsrc%2FEventSubscriber%2FDefaultExceptionSubscriber.php;fp=web%2Fcore%2Fmodules%2Fserialization%2Fsrc%2FEventSubscriber%2FDefaultExceptionSubscriber.php;h=80b8730308cb344df32d6538ce02bfd1695d140a;hp=a1e7bad2f8e070f91f63c875fde9fa0ecb3c298b;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/web/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php b/web/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php index a1e7bad2f..80b873030 100644 --- a/web/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php +++ b/web/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php @@ -2,6 +2,8 @@ namespace Drupal\serialization\EventSubscriber; +use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Cache\CacheableResponse; use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; @@ -51,8 +53,12 @@ class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase { */ 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; + // are still more common than HTTP requests. But it has a lower priority + // than \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber::on4xx(), so + // that this also handles the 'json' format. Then all serialization formats + // (::getHandledFormats()) are handled by this exception subscriber, which + // results in better consistency. + return -70; } /** @@ -67,14 +73,22 @@ class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase { $request = $event->getRequest(); $format = $request->getRequestFormat(); - $content = ['message' => $event->getException()->getMessage()]; + $content = ['message' => $exception->getMessage()]; $encoded_content = $this->serializer->serialize($content, $format); $headers = $exception->getHeaders(); // Add the MIME type from the request to send back in the header. $headers['Content-Type'] = $request->getMimeType($format); - $response = new Response($encoded_content, $exception->getStatusCode(), $headers); + // If the exception is cacheable, generate a cacheable response. + if ($exception instanceof CacheableDependencyInterface) { + $response = new CacheableResponse($encoded_content, $exception->getStatusCode(), $headers); + $response->addCacheableDependency($exception); + } + else { + $response = new Response($encoded_content, $exception->getStatusCode(), $headers); + } + $event->setResponse($response); }